I just watched Ryan Dahl — Introduction to NodeJS on YUI Theater and I am very exited, it surprised me, it sounds really crazy without the details. Today’s web servers mostly use threads or processes for each request they serve. The problem with this is the fact that threads and processes cost resources(CPU cycles and RAM), this is justified by the fact that most requests start and end very quickly, however if we have a lot of concurrent requests things can really heat up. If a web server gets a lot of incoming requests at the same time it needs to have a lot of threads/processes running, which increase the RAM and CPU usage and decrease the response time. This is a grave problem if we have a high traffic site or a chat application which keeps a connection open for a very long time.
So the question may be asked: why do we use threads anyway?
The answer: We could use a nice event loop but the problem is that we have a lot of function calls that block the execution like this one:
result = db.query(“SELECT * FROM foo”);
This line of code will pause our current thread until we get a answer from the database, and it takes long(socket connections etc.). In an event loop we can’t afford to have these instructions pause our loop, that means that we would basically have our clients wait in line.
The resolution: Make them stop blocking us!
So If we were to turn these blocking routines into non-blocking routines we would live in a better world.
Let’s have a look at JavaScript. The browser is one threaded still we are fine with it, we don’t need no threads there: why is that?
It’s because we have instructions like these:
//do something time consuming $.getJSON('ajax/some.php', function(data) { //wake me up when you are done! //do something with the result });
So we have asynchronous, non-blocking routines in JavaScript, that’s why we don’t care about threads.
If this is better, why aren’t people using this instead of threads?
According to Ryan Dahl, this is mostly because of bad habits, it would be somewhat harder to teach non-blocking IO and basically most of the tools we have now have only blocking IO. I feel that the main reason is probably that most languages don’t make writing callbacks easy and simple, unlike JavaScript.
Solution: let’s write a one threaded web server with JavaScript.
This is what Ryan Dahl and others are doing, this is what Node.js is all about. They abstract the ugliness of the underlying libraries and make IO routines non-blocking.
One may note: JavaScript is not the fastest language…
Answer: V8(Chrome’s JavaScript engine) all the way!
Node.js is far from ready for prime time, but it is definably usable in some cases where we would really need it. I recommend those interested in this to watch the video presentation, and to go to http://nodejs.org/, download Node.js(it’s free, MIT license) and give it a try. I successfully compiled it under Debian unstable and now experimenting with it.

