DEV Community

Devjeet Roy
Devjeet Roy

Posted on • Originally published at learnnodeonline.blogspot.com

The Thread Pool in node.js

When we run Node.js in our computer, it's a process which runs in our computer.The process is nothing but an executing program which gets executed in our local computer. When such a process runs, it runs in single thread. Thread is nothing but a sequence of instructions, which are dedicated to perform a given task. But let us remember that as Node.js runs on single thread we must try not to block the thread, no matter how many users or clients access our web application.
When a program is initialized, all the code gets executed apart from the codes , which are inside any callback functions. All the necessary modules are required and the callbacks are registered. Thereafter, the event loop starts running. Thought this event loop will be discussed after this, but for now keep in mind that the "Event-Loop" is the heart of the entire Node.js architecture. It is responsible for major tasks performed in our application.
But sometimes, the tasks are too heavy to be performed by the main thread of Node.js instantly and it then tends to block the execution. The solution to this is the "Thread Pool". It is basically a set of 4 additional threads provided by the LIBUV library. When a heavy task appears, the main thread offloads it to the additional threads to execute it. The heavy tasks like dealing with file system API, compression, cryptography and DNS lookups are offloaded generally. All of these are handled by Node alone. I hope now you have a little idea about the Thread Pool.
https://learnnodeonline.blogspot.com/

Top comments (0)