Architecture of Nodejs: How does it work technically?

By Haribabu | Posted on January 13, 2020

The-perfect-architecture-of-Node.js

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.

Build the best eCommerce platform with Nodejs today!!!

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

Architecture of Nodejs: Overview

Overview of an 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 execute a background thread that doesn’t wait for any asynchronous task to get terminated. Then the main thread is free to allow other requests for the execution process. Node’s main thread continuously switches between requests to execute its synchronous Part.

Read More: How To Install Node Version Manager NVM?

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 is 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 misconceptions 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 consists of a series of phases. Each phase works with its own specific tasks, and processes in a circular repetitive way.

Conclusion

If you still have any Nodejs development queries, simply contact us to get informed.

Comments

Leave a Reply

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