Before you go, check out these stories!

0
Hackernoon logoDecoding Nodejs by@haribabu-DM

Decoding Nodejs

Author profile picture

@haribabu-DMHaribabu G

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

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

In Nodejs, all those requests from the clients are handled in a single-thread using shared resources concurrently as It follows the “Single-Threaded Event Loop Model”.

ARCHITECTURE OF NODEJS

What Is EVENT-LOOP?

Event-Loop programming is a flow control in an application-defined by events. The basic principle of Nodejs’s event-driven loop is implementing a central mechanism that hears for events and calls the callback function once an event is turning up.

Nodejs is an event-loop that implements a run-time environment model to achieve non-blocking asynchronous behavior runs on Google Chrome’s V8 engine.

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

As soon as Nodejs begins to work, it initializes an event loop. And it processes the given input script(i.e)variable initiation and a function declaration. This eventually 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 Nodejs are:

NON-BLOCKING I/O MODEL

Node’s main thread doesn’t wait for anything literally, not even for an asynchronous process to complete. Such an integral part of Nodejs architecture is known to be Non-blocking I/O model.

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 ARCHITECTURE

The Background thread in Nodejs architecture 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, the background thread raises an event. All this 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 Nodejs architecture:

Some FAQs And Misconception About The Nodejs Architecture:

Is Nodejs Completely Single-Threaded?

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

Where Are These Other Threads Outsourced?

When using Nodejs, libuv a special library module 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.

Though the thread pool is of four threads which are to delegate operations that are too heavy for the event loop. The above-mentioned long-running tasks in the event loop logic represent are too expensive for the event loop.

Is An Event Loop Is A Stack-Like Structure?

For instance, whenever a stack-like structure involves in any tedious process. It expects a more precise output would be an event loop that consists of a series of phases. Each phase works with its own specific tasks, and processes in a circular repetitive way.

Tags

The Noonification banner

Subscribe to get your daily round-up of top tech stories!