Node.js is Important. An Introduction
Once in a while you come across a new technology and are just blown away by it. You feel that something like this should have been around much earlier and that it is (gonna be) a significant milestone, not just in your own live as a developer but in general.
What is node.js?
It’s actually not too easy to explain, but basically node.js is an:
What is Special about node.js?
A Practical Example: The Web Server
Ok, this is a bit dry and mind-bending maybe, but it all makes a lot of sense: Think about thousands of internet users entering the URL of your website causing their web browsers to hit your web server at the same time (maybe they fire Ajax requests too). Each of these requests would cause a thread in your regular web server (= Apache) to spawn that executes a script (PHP maybe) which does DB queries and such before finally returning the response to the browser. Here we have 2 things to look at: memory usage and blocking I/O.
Each of these threads uses memory. Many threads use a lot of memory. Thousands of users would cause thousands of threads to use a lot of memory…you see where this is going…you will run out of memory soon and your page or service will not respond anymore.
Blocking I/O is the cause for that problem: each one of these threads does things like DB access which takes some time to finish. This is normal because the DB tables might be huge and queries might take long to find the result you are looking for.
Now the script on your web server would usually execute a DB query, then wait until it gets a result from the DB server and then continue. While the DB server fetches your data your whole script is blocked. It does not continue to run until the result has come back and it is using memory and other resources while waiting. If this happens several thousand times at once this blocks a lot of resources and your server goes into a memory dry spell.
If it would be possible to get rid of all these threads and to put them all into a big pool this would solve some memory issues. If it on top of that would be possible to continue doing other tasks while waiting for DB queries this would surely help performance to increase – and that’s what node.js does.
Event Loop to the Rescue
A node-based server will pool all incoming connections and the underlying node framework can do things like hanging these connections to a “waiting” state while allowing the server to continue working on creating the response. The event loop and the callback paradigm in node.js is used to execute a DB query, but not wait for a response blocking a lot of resources. Rather a callback is attached to the DB result and node.js goes on with other tasks (saving CPU cycles). For the browser on the other side this results simply in a “waiting for response” situation, same as for the threaded server.
So, whenever the result from the DB comes back, node.js is executing whatever callback function was attached to the querying function and once the request is ready it will get the related connection from the pool, end its “waiting” state and return it to the browser. This is causing way less memory usage and allows for much faster response times.
This is just a tiny bit of the whole story, so make sure you get into this!
But Why is this Important?
Well, Ajax and Comet-driven web applications are generating heavy load on web servers, especially if you look at chat applications or multiplayer browser games where there are many concurrent connections and small response times are essential. Here lays the strength of node.js.
In the past to me it felt like we had reached a time where the traditional way to implement our web servers would not fit to the demands that we had on the client-side during the last months. Comet and Ajax-Push is a big hack, HTML5 Web Sockets are knocking at our door and we want to do things like real time data streaming.
Node.js can fill this need. Sure, it takes some time of getting used to for developers that have been working with “traditional” Ajax paradigms for the past years. It also seems like we would walk into a time where regular websites and Ajax driven web applications would get their own servers. But it surely feels like we would finally have a toolkit which solves many of our newly developed needs and which brings the web as a whole a big step further. It might well be the best thing since sliced bread.