Skip to main content
<FrontBackGeek/>

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

FrontBackGeek 3 years ago

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.

© FrontBackGeek.com 2021 All Rights Reserved. DMCA.com Protection Status