Is NodeJS multi-threaded ? How does NodeJS handle multiple requests ?

514 viewsProgramming
0
nodejs architecture

nodejs architecture – source – medium.com

Changed status to publish
0

Main question:- Is NodeJS multi-threaded ?

 

 

Answer is :- Yes & No

 

 

Yes, because NodeJS internally manages a limited thread pool which handles multiple client requests based on which thread is avaiable in the pool to process the request.

 

 

No, because programmers cannot create threads on the fly to handle new incoming requests.

 

 

How does NodeJS server work ?

 

nodejs architecture

nodejs architecture

 

NodeJS server uses an EventQueue, which queues incoming client requests and an EventLoop which is an infinite loop that receives requests and processes them. This EventLoop is single threaded and acts as a listener for the EventQueue which processes incoming requests based on FIFO policy.

 

 

When a new request comes in, NodeJS checks if it requires any blocking IO operations, if not, the EventLoop processes it and sends the response back to the client directly. If yes, then the request is forwarded to the thread manager, which then based on an algorithm, picks up an idle thread from the pool and lets it process the request. After completion of the request processing operation, the thread, returns the response back to the EventLoop which is then forwarded to the client. Thus, even if an incoming request needs blocking IO, the thread pool allows it to run asynchronously in the background without blocking the EventLoop and it gives a seamless experience of NodeJS handling multiple incoming requests concurrently without any persistent delays or bottlenecks.

 

Changed status to publish
Write your answer.

Categories