The Perfect Architecture of Nodejs: How It Works Technically?

Webnexs
3 min readJul 29, 2020

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 Nodejs follow multi-threaded architecture. That means that 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 follows the “Single-Threaded Event Loop Model”.

ARCHITECTURE OF NODEJS

ARCHITECTURE OF NODEJS
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 turns 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 the Node thread goes active until a task 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. 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, have a question on “How does event-loop work”? Here is 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 a 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 is continuously switching between different requests to execute its synchronous Part.

Webnexs Launching Headless Ecommerce In Node.js, Powered by Microservices.
Webnexs Launching Headless Ecommerce In Node.js, Powered by Microservices.

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 don’t run their logic outside the Nodejs single thread. This practice is carried out to maintain the programs’ speed and performance.

Where Are These Other Threads Outsourced?

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

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

Is An Event Loop Is A Stack-Like Structure?

For instance, whenever a stack-like structure is involved 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 specific tasks, and processes in a circular repetitive way.

CONCLUSION

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

--

--