Subscribe via RSS Feed

Node.js Non-Blocking IO Model

December 9, 2013 7 Comments

Now-a-days developers know about Node.js as a server -side javascript execution platform.

According to nodejs.org,- “Node.js is a platform built on Chrome’s JavaScript runtime for easily building fast, scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.”

Now node.js opens a new way of performing or serving to the web requests or any other type of server requests – which is called Non-blocking IO Operation.

We have came across the following link - http://code.danyork.com/2011/01/25/node-js-doctors-offices-and-fast-food-restaurants-understanding-event-driven-programming/,  which is an excellent way of showing the non-blocking concepts of node.js with relate to real-life working scenarios.

As my background for last 5-6 years as J2ee Programmer, I knew and understood the Servlet Programming Model in J2ee Environment, which is a multi-threaded programming model i.e. when a web request come to java based web server, the request hit to the servlet with the request and the servlet opens a new thread to handle the request and response the data back to the client. All the database related methods or any server resource intensive call, which are shared in nature, are to be handled carefully from the servlet to avoid any deadlock or other unwanted situations which will be related to shared objects.

Though through this way a process parts in threads and serves the web request within thread which are having relatively small overhead as compared to server processes. But still if some database call amounts to a significant time within the execution of request and response which is within a thread call, it will hold the client for this with their synchronous activity which is blocking IO operation.

Now it is time to understand the non-blocking I/O, on which node.js platform is defined.

First of all, node.js application environment will run on a single threaded programming model.

All the web requests and server requests call in node.js are of non-blocking I/Os. An web request comes in node.js web server and the web server accepts this request and handle this request to a listener object to process the response. Also this web server will remain ready to accept any new web request. And the previous response handling will remain in a queue for performing the rest of operations. Now the node.js environment takes care of preparing the web response which again can be a database call from the actual application. Also this database query can also consists of callback functions with the response from database as being ready. Those operations are executed in a queue and also called as event driven programming and all of these operations are asynchronous in nature.

So the whole point to discuss here is, all the function calls within node.js context are non-blocking input-output operations. In node.js environment, the performing operation is executed by one object and the results of the operation are handled by the callback functions.

We will talk more about the node.js execution model, event-driven programming and asynchronous operations as we work further in these line of application architecture.

That is all for now. Comments are well come.

Sign Up to read the rest of the content

Email will be used only for updates of our site

No Thanks