In this article, I am going to share how thread operates in NodeJS and how it is different from other server-side technologies which support thread like JAVA, DotNet etc. I am going to clear all your doubts about the thread in NodeJS.
All tasks are completed in NodeJS using one thread except such as I/O and the main advantage is that the overhead of frequently switching threads is naturally eliminated and reduce resource grabs.
However, NodeJS is powerless when it comes to CPU-intensive models. Although NodeJS has an asynchronous mechanism, it can throw some time-consuming algorithms into the event loop waiting for the next event loop to do it. But because it is still a single-threaded model, it will eventually cause congestion.
What is Threads?
Threads are executed parallel at the same time. Thread share the memory of the main process. When one thread is locked at a certain time, it will not affect the execution of the main thread and other threads. However, in order to implement this model, It consumes more memory and CPU overhead for thread switching. At the same time, there is the problem that multiple threads may read and write the same memory unit and cause the program to crash.
- Processes vs Threads
- Synchronous vs Asynchronous Operation
Process vs Threads
Event-driven programming controls the flow of an application through changes in events or states. It is usually implemented through event listeners. Once an event is detected, the corresponding callback function is called. In fact, this is the basic working principle of the Node.js event loop. If you want to learn more about Event loop, read my previous article.
Synchronous vs Asynchronous Operation
In Other technologies like asp.net, you can perform asynchronous architecture but you have to do some extra work whereas in NodeJs application is asynchronous by default so you don’t have to do anything extra.
Asynchronous: Each task has one or more callback functions. After the previous task is completed, instead of executing the next task, a callback function is executed. The next task is executed without waiting for the previous task to finish. Therefore, the execution sequence of the program and the task The sort order is inconsistent and asynchronous.
Synchronous: The next task waits for the previous task to complete, and then executes again. The execution sequence of the program is the same as that of the task.
Node JS is single threaded
- Node JS is single threaded but there is also a internal thread pool that it uses sometimes.
- C++ backed synchronous code runs in the main thread.
- C++ backed asynchronous code sometimes runs in the main thread and sometimes it doesn’t
Libev implements a time loop and encapsulates the underlying technologies used.
Database API takes more time than the Math API
Math API takes more time than the Database API
These requests come in various orders.
NodeJS has a single thread to handle all requests, the linear request arrives that single thread is used to handle that request. If you want to query a database, a thread doesn’t have to wait for database return a data while the database is executing a query, a thread will be used serve another client request.
When database prepares the results, it put the message to the event queue. NodeJS is continuously monitoring the queue in the background. When it finds any event in this queue, it will take it out and process it. This kind of architecture makes NodeJS ideal for building an application that includes a lot of disks or network access for I/O intensive application.
In Contrast, NodeJS should not be used for CPU intensive applications like an example here a Math API which requires a lot of calculations that should be done by CPU. NodeJS is single threaded so when performing the calculation for one client, other clients have to wait.
Let’s understand the scenario in below diagrams:
Here, DB API takes 11.2 seconds and Math API takes 5.28 seconds to complete.so if there is more than one request, DB API will be handled parallelly but Math API perform calculations so it will be handled one by one.
DB API and Math API results also depend on the order of execution started
If you start DB API first and then Math API, here is the performance chart:
The Thread Pool
Node itself depends on multiple libraries. One of them is libuv, which handles asynchronous event queues and execution of libraries.
You may have heard that Node has a thread pool, and you may be wondering: “If Node handles tasks in order, why do you need a thread pool?” Because in the kernel, not all tasks are executed asynchronously. In this case, Node JS must be able to lock the thread during operation so that it can continue executing the event loop without being blocked.
Loop over these functions
So, what was happening earlier?
C++ backed methods use C++ asynchronous primitives whenever possible
So, how does it all relate back to eventloop?
So, it all depends on the nature of the incoming request whether it is synchronous or asynchronous, if asynchronous it depends on whether or not it can be handled by the C++ asynchronous primitives and so on…
This article briefly discusses the internal operating mechanism of NodeJS related to Thread. It is Suitable for high concurrency, IO-intensive operations.