visit
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 thepublic 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
}
}
}
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.