- August 2010 (1)
- September 2010 (4)
- October 2010 (7)
- December 2010 (1)
- January 2011 (1)
- April 2011 (1)
- May 2011 (2)
Mayank Gupta
13 Oct 2010Node.js or Node being event driven powers the programmers to use an Evented, non-blocking I/O (click here to read What is Evented, Non-blocking I/O in Node) driven approach to code their web applications. Node is built on Google's flashing fast V8 JavaScript Engine. This JavaScript engine is well known for very fast dynamic property access, lightning fast dynamic generation of the machine code and very highly efficient garbage collection. This JavaScript engine allows Node.js to execute at notoriously fast speeds. Thus, Node is really fast in doing asynchronous Input and Output (I/O) - be it to or from the network, server's hard disk, its database or during file uploads.
Apart from Node being lightning fast, and providing a system for non-blocking input and output (I/O), Node is based on single thread / event loop which is yet another major factor that adds to the power of Node.js.
Lets try and understand understand how the two most popular web servers - Apache and NGINX handle concurrent connections.
Here is a graphical benchmark to compare the two servers -

The above benchmark may not be a real life example but nevertheless it shows shows the performance statistics of the two servers very well. It can be seen that both Apache and NGINX can serve a huge number of requests per second, but as the number of concurrent requests increases it can be seen that there is a huge drop in Apache's performance when compared to NGINX, which still maintains almost the same speed even when the number of concurrent connections increase without any drop in its performance.
Let us examine the same situation with a different type of plot, that is the same benchmark, plotted against memory consumption of the the two servers -

The above plot explains the memory consumption by the two servers with increasing number of concurrent requests. Though, both the servers can serve a huge number of concurrent requests but there is a remarkable difference in the way they use the server resources that is - memory. From the graph, it is clear that Apache uses a much larger rather huge tons of memory for the same number of concurrent request than NGINX. NGINX handles concurrent requests in a very efficient event loop - single thread way, unlike Apache that spawns your server with multiple threads (one for each connection). These threads use a large amount of memory, and thus leads to Apache being really in-efficient when a large number of concurrent requests are made to the server whereas, NGINX remains very stable as it makes the use of event Loop, instead of executing a new thread per connection.
Context switching is not free, it takes both the server resources and increased response time when tested in a real time situation. Thus, for massive concurrency new threads cannot be spawned on the operating system (OS) . To handle this Node as based on a single thread execution, uses event loop to handle all the new requests made to the server instead of executing multiple threads which makes Node.js very powerful as it does not waste any resources in context switching.
Thus, Node.js is highly efficient when it comes from handling massive concurrency to providing the lowest level base so that the programmers can stream over Node and they do not have to buffer data. Not just this, Node.js allows programmers to code their way with an event based algorithm. Node.js also powers the developers to execute their code and its tasks in parallel and providing it with an non-blocking input and output (I/O). Apart from having architectural advantages Node built on flashing fast Google's V8 JavaScript Engine makes Node.js lightning fast.
Before taking this into account let us take this the other way round and know why an event based approach is not popular among the programmers today. There are three basic reasons for this :
//Output the string
puts("Please Enter Your Green Name");
//wait for and take input
var green_name=gets();
//output the string
puts("Your Green Name is: "+green_name);
The above code is based on demand input algorithm. This approach to writing code blocks the code. In the above code the user is asked to input his green name and till the time the user inputs, the code haults! The code is not able to do anything till the user is done with entering his green name,
If we write the same code this way -
//Output the string
puts("Please Enter Your Green Name");
//declare a call back function
gets( function (green_name) {
puts("Your Green Name is: "+green_name);
});
//do not wait, do something else
This programming approach is event based. Using this programing approach the programmers can achieve: evented non-blocking input and output(I/O). The only reason this type of programming approach is not as popular as the blocking - haulting - coding approach because people think coding applications this way is difficult and more complicated (which is not true). So, there exists a cultural bias against coding applications in an event driven way even though it .
Fortunately, developing event based applications is no more difficult with the availability of Node. Node provides the infrastructure to build highly efficient, purely evented, non-blocking web applications, that can handle concurrencies very efficiently. Node.js makes event based programming really easy, highly efficient and lightning fast as Node uses commonJS module system, provides non-blocking input and output (I/O) and is built on V8 JavaScript Engine developed by Google, which roars speed.
Get started with Node.js, Read how to Install Node.js
No Responses yet to Why Node.js?
Post new comment