What Are Threads In NodeJS?

KHEEM DHARMANI
3 min readAug 8, 2021

In Node architecture there are two fundamental parts are:

  • Thread pool
  • The Event Loop

So in this article, you are going to learn all about threads and a thread pool. When we use Node on a computer, it means that there is a Node process running on that computer. And the process is just a program in execution. And Node.js is basically a C++ program, which will therefore start a process when it’s running. This is important because, in Node, we actually have access to a process variable. Now in that process, Node.js runs in a so-called, single thread. And a thread is basically just a sequence of instructions. Just imagine a thread as being a box where our code is executed in a computer’s processor. Now, what is important to understand here, is the fact that Node runs in just one thread, which makes it easy to block Node applications. But it’s something really, really important to remember because this is one of the unique features that Node.js brings to the table. So, again, if you run your Node application, it’ll run in just a single thread. No matter if you have 10 users or 10 million users accessing your application at the same. And so you need to be very careful about not blocking that thread. Let’s now quickly understand exactly what happens in a single thread when you start your Node application. So when the program is initialized, all the top-level code is executed, which means all the code that is not inside any callback function. Also, all the modules that your app needs are required, and all the callbacks are registered, just like the HTTP server. Then after all that, the event loop finally starts running. What you need to know for now is that the event loop is where most of the work is done in your app. So, it’s really the heart of the entire Node architecture. Some tasks are actually too heavy. They are too expensive to be executed in the event loop because they would then block the single thread. And so, that’s where the thread pool comes in, which just like the event loop, is provided to Node.js by the libuv library.

To Learn More About Libuv

So, the thread pool gives us four additional threads that are completely separate from the main single thread. And we can actually configure it up to 128 threads. So threads together formed a thread pool. And the event loop can then automatically offload heavy tasks to the thread pool. And all this happens automatically behind the scenes.

How NodeJS Works Behind The Scene

It’s not us developers who decide what goes to the thread pool and what doesn’t. Now, the expensive tasks that do get offloaded are all operations dealing with files, everything related to cryptography, like caching passwords, then all compression stuff, and also DNS lookups, which basically match web domains to their corresponding real IP addresses. So this is the stuff that would most easily block the main thread. And so, Node takes care of automatically offloading them into the thread pool, where they don’t block our event loop. And that is the most important thing that I want you to retain from this article.

--

--

KHEEM DHARMANI
0 Followers

As a highly skilled and experienced in content writing, with more than 5 years of academic studies.