Software Development

Node.js Non-Blocking IO Model

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.
 

Reference: Node.js Non-Blocking IO Model from our JCG partner Piyas De at the Phlox Blog blog.

Piyas De

Piyas is Sun Microsystems certified Enterprise Architect with 10+ years of professional IT experience in various areas such as Architecture Definition, Define Enterprise Application, Client-server/e-business solutions.Currently he is engaged in providing solutions for digital asset management in media companies.He is also founder and main author of "Technical Blogs(Blog about small technical Know hows)" Hyperlink - http://www.phloxblog.in
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

4 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Arjun
Arjun
9 years ago

Just a thought…
What if we have a web server which will maintain a thread pool for incoming request handling and each thread behave in asynchronous manner as it works in Node.js.. will it perform better than Node.js as Node.js is only single threaded ?

Akshay
7 years ago
Reply to  Arjun

its all about callback function and maintaining a event-loop.And making each thread in connection pool behave in asynchronous manner does not make any sense.

Arjun
Arjun
7 years ago
Reply to  Akshay

Read up async request in servlet spec before sensing my earlier comment and then think…

Xiao
Xiao
8 years ago

Hi Piyas,

I have met a problem of using the Node.js as a server proxy. The Node.js acts not the same as it promised, no-blocking response. The Node.js won’t response to new client requests until it has handled last request. I am not sure whether it is the error of my code. If you have time, please help me on this post. http://stackoverflow.com/questions/31984806/what-is-the-correct-behavior-for-node-js-while-node-js-is-requesting-a-long-time thanks a lot.

Back to top button