Node JS threading model: How the Node JS is achieving multi-threading in a single thread model??

Saravanan A R
3 min readNov 1, 2018

I had a serious question in my mind when I learned Node JS, that is, how Node JS is a single-threaded? In almost all the tutorials and blog out there talks and manipulate us that Node Js is a single-threaded.

That is not entirely true.

Will you get surprised If I tell you that Node JS internally uses thread?

Yes, Node JS internally uses p_thread using the libuv c++ library. People calling it a single-threaded model because the developer doesn’t have to worry about creating and managing the thread in code. Internally, Node JS will handle the creation and management of the thread for us.

For example, for the sake of understanding, assume that if an entire Node application runs in a single thread, we can easily say that Node is not effectively using the CPU cores. Assume that you have a machine with 2 CPU core and if the Node is single-threaded, it can effectively use a single CPU core and will waste all the remaining CPU core processing power. SUCKS!!

Codingeek Image

When you run a node application, OS will create a process with a single thread(main thread) on which the event loop will run. If you want to read about the event loop and its internal behavior, read my previous blog before continuing further.

If an I/O operation performs in the event loop, let's say you are reading a file in the async mode using the fs.readFile() function from the node’s native fs module. Note the word “native” here.

While reading a file using the fs.readFile() function, the event loop won’t wait for this function to resolve. Instead, the fs.readFile() function will bind a native file where the actual reading operation happens. That native file will create a p_thread to read a file from the disk. Once, the reading operation succeeds, the appropriate callback which was given in fs.readFile() will invoke.

Hence, the event loop’s main job is to get the request (the request may be a network HTTP request or any IO operation like reading data from a file or DB) and delegate those requests to the p_thread to operate.

By default, each node instance has 4 threads in the thread pool. You can increase or decrease this size. But you should really care about your CPU core count If you are going to overwrite the default count.

Let's assume that you have a machine with 2 CPU core. (For simplicity, don’t worry about core level multithreading and assume each core will handle one operation at the time). Say, you increased the Node instance’s thread pool size to 50.

What will happen? Do you think that the requests will handle quickly? NO! Never! Because we have a machine with 2 cores alone. Actually, this will slow down our processing. If you are gonna change the thread pool size. You MUST know about our machine CPU core count.

I hope you get the Node js internal threading model architecture.

--

--