What is Single thread model used in node js and how its eliminates the limitation of multi thread model

Node Js is event driven, handing all the request asynchronously from single thread.

Almost no function  in node directly performs I/O, so the process never blocks.

So in a single thread model whenever a user performs an activity a event is generated.

For Example.

If you click a button or fill a form in your application a new event is generated, you can take each new request as a event.

Now, once you generate an event, event emitters emits those events and then those events decides inside the event queue in the server, then these events are executed using event loops, which is a single thread mechanism.

In this mechanism a single thread takes event from the event queue and allocates the event to a worker thread present inside the thread pool.

The allocation is based on the operation performed by event so it could be I/O operation for example a network operation etc.

So based on that it will be handle to the thread and then this worker thread process those request asynchronously.

Now you can notice here that we have only one thread in this event loop so one thread in this event loop will be handling the event directly.

So no function in node.js can directly perform operations thus the process will never get blocked.

As this process are based on events and events are handled by events loop does this model is also called event driven model.

So our node.js follow this event driven model.

Related Posts

Difference between Multithread and single thread or event driven model

Multi thread model Request with listener worker thread | lock application. Using incoming request model. Multi threaded server might block the request which might involve multiple events….

What is Multi thread model and what are the limitations ?

In a Multi thread model the server assign incoming requests to a new thread similar as a client server architecture, the user generate a request by interaction…

Leave a Reply

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