Understanding Node.js and concept of non-blocking I/O

Posted by Saif Bechan on Stack Overflow See other posts from Stack Overflow or by Saif Bechan
Published on 2010-03-31T06:09:01Z Indexed on 2010/03/31 6:13 UTC
Read the original article Hit count: 642

Recently I became interested in using Node.js to tackle some of the parts of my web-application. I love the part that its full JavaScript and its very light weight so no use anymore to call an JavaScript-PHP call but a lighter JavaScript-JavaScript call. I however do not understand all the concepts explained.

Basic concepts

Now in the presentation for Node.js Ryan Dahl talks about non-blocking IO and why this is the way we need to create our programs. I can understand the theoretical concept.

You just don't wait for a response, you go ahead and do other things. You make a callback for the response, and when the response arrives millions of clock-cycles later, you can fire that.

If you have not already I recommend to watch this presentation. It is very easy to follow and pretty detailed. There are some nice concepts explained on how to write your code in a good manner. There are also some examples given and I am going to work with the basic example given.

Examples

The way we do thing now:

puts("Enter your name: ");
var name = gets();
puts("Name: " + name);

Now the problem with this is that the code is halted at line 1. It blocks your code.

The way we need to do things according to node

puts("Enter your name: ");
gets(function (name) {
  puts("Name: " + name);
});

Now with this your program does not halt, because the input is a function within the output. So the programs continues to work without halting.

Questions

Now the basic question I have is how does this work in real-life situations. I am talking here for the use in web-applications. The application I am writing does I/O, bit is still does it in am blocking matter.

I think that most of the time, if not all, you need to block, because you have to wait on what the response is you have to work with. When you need to get some information from the database, most of the time this data needs to be verified before you can further with the code.

Example 1
If you take a login for example. You have to wait for the database to response to return, because you can not do anything else. I can't see a way around this without blocking.

Example 2
Going back to the basic example. The use just request something from a database which does not need any verification. You still have to block because you don't have anything to do more.

I can not come up with a single example where you want to do other things while you wait for the response to return.

Possible answers

I have read that this frees up recourses. When you program like this it takes less CPU or memory usage. So this non-blocking IO is ONLY meant to free up recourses and does not have any other practical use. Not that this is not a huge plus, freeing up recourses is always good.

Yet I fail to see this as a good solution. because in both of the above examples, the program has to wait for the response of the user. Whether this is inside a function, or just inline, in my opinion there is a program that wait for input.

Resources I looked at

I have looked at some recourses before I posted this question. They talk a lot about the theoretical concept, which is quite clear. Yet i fail to see some real-life examples where this is makes a huge difference.

Stackoverflow:

Other recources:

© Stack Overflow or respective owner

Related posts about node.js

Related posts about examples