How NodeJs Single Thread mechanism Work ? Understanding Event Loop in NodeJs

                                                                              NodeJs Architecture

As a programmer it is very important that we choose our platform right but it is even more important that we do it to develop a solutions which is resource efficient and works lightening fast. We need to decide what is the actual need of a product and make a wise decision to select the front-end and back-end technology to make it a reality. It is less about competition and more about the contextual need so developers it is recommended that you always look to learn things what is right today and is equally futuristic.

In our journey of NodeJs so far , we have covered most of the basic of Node.js so it is highly recommended to refer those to set the context right for all you lovely readers:

My NodeJs articles so far :

  1. Getting Started With Node.js : A Beginners Guide
  2. All About Node.Js You Wanted To Know 
  3. 3. Node.Js Vs PHP : Battle of Supremacy & Survival

Today We will explore one of the most prominent feature of Node i.e Event Loop and how Node manages efficiently without multi-threading.

Node.js Is Single Threaded : What’s The Fuss About It ?

Yes! you heard it right, Node is single threaded and it is doing magical things with this model. Some of the popular server side technology like PHP, ASP.NET, Ruby & Java Servers all follow Multi-threaded where each client request results in the instantiation of a new thread or even a process, but Node. js, requests are run on the same thread with even shared resources. All Node JS applications uses “Single Threaded Event Loop Model” architecture to handle multiple concurrent clients. So Yes NodeJS is single threaded, but this is a half truth, actually it is event-driven and single-threaded with background workers. The main event loop is single-threaded but most of the I/O works run on separate threads, because the I/O APIs in Node.js are asynchronous/non-blocking by design, in order to accommodate the event loop.

Lets try to understand what goes inside this single threaded mechanism :

How Node.js Single Thread Mechanism Work :

NodeJs operate asynchronously and uses the Event-loop mechanism to function let’s look into the code snippet example below

Code Example 1 :

var sockets = require('websocket.io'),httpServer = sockets.listen(4000);

httpServer.on('onConnection', function (socket) {

console.log('connected……');

httpServer.send('Web socket connected.');

httpServer.on('message', function (data) {

console.log('message received:', data);

});

httpServer.on('close', function () {

console.log('socket closed!');

});

});

Here when sockets.listen(4000) executes, a Web-Socket server is created on a single thread — event loop which listens continuously on port 4000. When a web or app client connects to it, it fires the ‘onConnection’ event which the loop picks up and immediately publishes to the thread pool and is ready to receive the next request and this is the main functionality differentiation between NodeJs based servers and other IIS/ Apache based servers, NodeJs for every connection request do not create a new thread instead it receives all request on single thread and delegates it to be handled by many background workers to do the task as required. Libuv library handles this workers in collaboration with OS kernel. Libuv is the magical library that handles the queueing and processing of asynchronous events utilizing powerful kernel, today most modern kernels are multi-threaded, they can handle multiple operations executing in the background. When one of these operations completes, the kernel tells Node.js so that the appropriate callback may be added to the poll queue to eventually be executed.

To better understand we need to get into the details of how event loop works in NodeJs :

Event Loop Explained :

As we have earlier covered that event loop allows Node.js to perform non-blocking I/O operation, despite the fact that JavaScript is single-threaded & by offloads operations to the system kernel whenever possible. lets further get into the details of how event loop functions to clearly understand the Nodejs single threaded concept of handling asynchronous incoming connection requests.

Node has a pool of Thread and you must be scratching your head wondering if Node pushes all those responsibilities down to the kernel then why would a thread pool be needed?” It’s so because the kernel doesn’t support doing everything asynchronously. In those cases Node has to lock a thread for the duration of the operation so it can continue executing the event loop without blocking.

Here is a simplified diagram to explain the mechanical overview of how things run in NodeJs

┌───────────────────────┐
┌─>│        timers         │
│  └──────────┬────────────┘
│  ┌──────────┴────────────┐
│  │     I/O callbacks     │
│  └──────────┬────────────┘
│  ┌──────────┴────────────┐
│  │     idle, prepare     │
│  └──────────┬────────────┘      ┌───────────────┐
│  ┌──────────┴────────────┐      │   incoming:   │
│  │         poll          │<─────┤  connections, │
│  └──────────┬────────────┘      │   data, etc.  │
│  ┌──────────┴────────────┐      └───────────────┘
│  │        check          │
│  └──────────┬────────────┘
│  ┌──────────┴────────────┐
└──┤    close callbacks    │
   └───────────────────────┘

Fig 1 : Source : https://nodejs.org/

Note: each box here is referred to as a “phase” of the event loop.

Phases in Nutshell: (As shown above in Fig 1)

  • timers: this phase executes callbacks scheduled by setTimeout() and setInterval(). A timer specifies the threshold after which a provided callback may be executed rather than the exact time a person wants it to be executed.
  • I/O callbacks: executes almost all callbacks with the exception of close callbacks, the ones scheduled by timers, and setImmediate().
  • idle, prepare: only used internally.
  • poll: retrieve new I/O events; node will block here when appropriate.
  • check: setImmediate() callbacks are invoked here.
  • close callbacks: e.g. socket.on('close', ...).

Here every phase follows FIFO stack like queue mechanism to handle callbacks. Each phase is unique , generally, when the event loop enters a given phase, it will perform any operations specific to that phase, then execute callbacks in that phase’s queue until the queue has been exhausted or the maximum number of callbacks has executed. When the queue has been exhausted or the callback limit is reached, the event loop will move to the next phase, and so on.

What’s Next :

We will cover the Event Loop Phases in details with code snippets in our next articles in our series of NodeJS and also will try to understand the nodejs operations with a practical examples.

I would like to end this article on NodeJs with an amazingly compiled Infographics by Vizteck Solutions where it compares Node with Ruby on Rails.

We will deeply compare NodeJs With Rail in our upcoming article till then ..

If you are 💚 💚 my contribution do 👏 👏 clap , Click Here and subscribe to reach out to me for more and i would feel blessed to hear and respond you back.

For More Visit : www.techprenuer.com

Pramod Chandrayan

Founder & CEO (Mobibit Soft (P) Ltd) | Mobile App Development Consultant | Startup Mentor | Spiritual Seeker

More Posts - Website

Follow Me:
TwitterFacebookLinkedInGoogle Plus

Related posts

Leave a Comment