As far as I know, all IO requests and other asynchronous tasks are done by libuv
in nodejs
.
I want to know if libuv
is using threading. If it is, is it using all available core or not?
-
1Yes, libuv has a thread pool. β Ry- Oct 15 '17 at 09:37
-
is it using all core or not if system is multicore? β Rishabh Oct 15 '17 at 09:45
3 Answers
First of all, what is libuv
. As mentioned in the documentation, it's a multi-platform support library with a focus on asynchronous I/O.
libuv
doesn't use thread for asynchronous tasks, but for those that aren't asynchronous by nature.
As an example, it doesn't use threads to deal with sockets, it uses threads to make synchronous fs calls asynchronous.
When threads are involved, libuv
uses a thread pool the size of which you can change at compile-time using UV_THREADPOOL_SIZE
.
node.js
is provided with a precompiled version of libuv
and thus a fixed UV_THREADPOOL_SIZE
parameter.
It goes without saying that it has nothing to do with the number of cores of your chip.
I'm tempted to affirm that you can safely ignore the topic, for libuv
and thus node.js
don't use threads intensively for their purposes (unless you are using them in a really perverse way or if you are running an high number of libuv
work requests).
Feel free to run an instance of node.js
per core if you need as most of the users do.
The design overview section of libuv
is also clear enough about this point:
The I/O (or event) loop is the central part of libuv. It establishes the content for all I/O operations, and itβs meant to be tied to a single thread. One can run multiple event loops as long as each runs in a different thread.
The libuv module has a responsibility that is relevant for some particular functions in the standard library. for SOME standard library function calls, the node C++ side and libuv decide to do expensive calculations outside of the event loop entirely.They make something called a thread pool that thread pool is a series of four threads that can be used for running computationally intensive tasks such as hashing functions.
By default libuv creates four threads in this thread pool. Thread Pool
in the picture is organized by the Libuv
So that means that in addition to that thread used for the event loop there are four other threads that can be used to offload expensive calculations that need to occur inside of our application. Many of the functions include in the node standard library will automatically make use of this thread pool.
Network
(Network IO) is responsible for api requests, File system
(File IO) is fs
module. so node.js single thread delegates those heavy work to the libuv
If you have too many function calls, It will use all of the cores. CPU cores do not actually speed up the processing function calls, they just allow for some amount of concurrency inside of the work that you are doing.

- 35,338
- 10
- 157
- 202
-
so that , there is no way to happen any multi thread problems ? , what i'm asking is was we don't need to worry about this libuv's thread pool when using node ? β Inshaf Ahmed Dec 29 '21 at 16:55
From here:
A single instance of Node.js runs in a single thread. To take advantage of multi-core systems the user will sometimes want to launch a cluster of Node.js processes to handle the load.
The cluster module allows easy creation of child processes that all share server ports.
Multiple processes could be better than multithreading in some cases. Some people even think theads are evil. Maybe node.js is designed in such a way to take advantage of processes better than threads.

- 5,654
- 28
- 44