In this article, I’d like to have a look at what Node.js is for, its major benefits, and why you might want to think about adding yet another tool to your web development kit.
What Is Node.js?
What Makes Node Different?
The Node.js site describes it like this:
I’ve already explained about the runtime and the sort of applications you can build with Node.js, but the crucial part of this description is the bit about the “event-driven non-blocking I/O model”. Node.js is fast and resource efficient because it is event-driven, and doesn’t use a more traditional thread-based system. What that means can be quite difficult to understand, and to explain it I’m going to borrow an analogy from developer Dan York.
Think about a doctor’s reception. There is one receptionist and a long queue of patients. Each patient has to fill in several forms before they can be seen by the doctor. There are two ways of handling this situation. The receptionist can give each patient the forms and wait until they are completed before dealing with the next patient. This is analogous to a thread-driven system. The receptionist is blocked while the forms are filled in. The only way to scale this system is to add more receptionists (threads). The Apache web server works like this, which is why it’s quite inefficient, especially under heavy load. It essentially duplicates itself for each request, taking up a lot of resources. Thread-based systems are also difficult to think about and code.
The second way of handing the queue at the reception would be to give each patient their forms, and then send them away while they are competed. When the forms are filled, the patient rejoins the queue. This way, the receptionist can immediately deal with the next patient — he isn’t blocked by the form-filling patient. The system is event-driven — the receptionist deals with events — patients requesting forms, patients returning filled-in forms — rather than being blocked or needing a new receptionist for each patient.
Node.js is event-driven, which makes it perfect for building network applications. I/O requests are not blocking, which means that Node.js doesn’t sit idle while waiting for I/O and it doesn’t have to spawn multiple threads to deal with multiple requests.
If this has whetted your appetite for Node.js, check out these resources to learn more.