The perfect architecture of Node.js

The main goal of this blog is to explain the “Architecture of Node.js” and to know how the Node.js works behind the scenes,

Generally, most of the server-side languages, like PHP, ASP.NET, Ruby, follow multi-threaded architecture. That means for each client-side request initiates a new thread or even a new process.

In Node.js, all those requests from the clients are handled in a single-thread using shared resources concurrently as  It follows “Single-Threaded Event Loop Model” architecture that runs on Google Chrome’s V8 engine.

ARCHITECTURE OF NODE.JS

What is EVENT-LOOP??

Event-Loop programming is a flow control in an application-defined by events. The basic principle of Node.js’s event-driven loop is implementing a central mechanism that hears for events and calls the callback function once an event is turned up. Node.js is an event-loop that implements a run-time environment model to achieve non-blocking asynchronous behaviour. 

An event loop which is run by Node thread goes active until a task gets completed, it activates the secondary event which signals the event-listener function to execute the task on time.

As soon as Node.js begins to work, it initializes an event loop that processes the given input script(i.e)variable initiation and function declaration which makes asynchronous API calls and schedule timers, then begins processing the event loop. Still, having a question on “How event-loop works”?? Here the sample Pseudo-code that clearly explains the model,

public class EventLoop {

while(true){

         if(Event Queue receives a JavaScript Function Call){

         ClientRequest request = EventQueue.getClientRequest();

                            If(request requires BlockingIO or takes more computation time)

                                    Assign request to Thread T1

                            Else

                                  Process and Prepare response

                  }

            }

Some other integral parts of this architecture of Node.js are:

NON-BLOCKING I/O MODEL

Node’s main thread doesn’t wait for anything literally, not even for an asynchronous process to complete.

In simple, main threads in Node executes a background thread that doesn’t wait for any asynchronous task to get terminated. And then the main thread is free to allow other requests for the execution process. Node’s main thread is continuously switching between different requests to execute its synchronous Part.

EVENT-BASED

The Background thread in Node.js uses an Event-based approach to report the main thread. Each asynchronous task consists of some Callback function associated with it, once the asynchronous task is complete, background thread raises an event to report the main thread about the completion of the asynchronous task. The main thread will be busy processing other requests, meanwhile, the request waits on the main thread callback request for the execution. 

 The various phases of the event-loop are illustrated below for a better understanding of overall architecture:

Some FAQs and misconception about the Node.js architecture:

Is Node.js completely single-threaded?

This is a very common misconception about Node.js. Node runs on a single thread, but some of its functions in the Node.js standard library doesn’t run their logic outside the Node.js single thread. This practice carried out to maintain the programs’ speed and performance.

Where are these other threads outsourced?

When using Node.js, libuv a special library module is used to carry out asynchronous operations. It also used together with the back-logic of Node in order to manage a special thread pool called the libuv thread pool.

This thread pool is composed of four threads used to delegate operations that are too heavy for the event loop. The above-mentioned long-running tasks in the event loop logic represent those operations described here as too expensive for the event loop.

Is event loop is a stack-like structure?

For an instance, stack-like structures are involved in any tedious process, a more precise output would be an event loop comprises a series of phases, each with its own specific tasks that will be processed in a circular repetitive way.

Leave a Reply

Your email address will not be published. Required fields are marked *

Facebook
Twitter
INSTAGRAM
LinkedIn