libuv and the Thread Pool: What Runs Off the Main Thread
Learn which Node.js operations use the libuv thread pool, which use the operating system's async I/O, how to size the pool and why it matters for performance.

Node.js is often described as single-threaded, and that is true of your JavaScript. Behind it, though, other threads do real work. File reads, password hashing, compression and DNS lookups are handed to a pool of worker threads managed by libuv, the C library under the event loop. Network sockets are handled differently, by the operating system's own notification system. Knowing which operations go where explains why a server can slow down under a burst of hashing while its network handling is fine, why UV_THREADPOOL_SIZE exists and why adding more concurrent file reads does not always make them faster. This post explains the model on Node.js 24, shows which APIs use the pool, and covers sizing and the common mistakes. Samples show their output.
It continues from the event loop phases and fits into the overview in Node.js architecture: V8, libuv and the event loop explained.
Two kinds of asynchronous work
When you call an asynchronous API in Node.js, the work is done in one of two ways.
Operating system asynchronous I/O. For network sockets and pipes, libuv asks the operating system to notify it when data is ready. On Linux that is epoll, on macOS and BSD kqueue, and on Windows I/O completion ports. No thread waits for each socket. One thread can watch thousands of connections, and this is why a Node.js server holds many idle connections cheaply.
The thread pool. Some operations have no good non-blocking interface from the operating system, or are CPU-heavy by nature. For those, libuv runs the blocking call on a worker thread from a fixed-size pool, and posts the result back to the event loop when it is done. Your code sees an ordinary callback or promise.
The distinction matters because the pool is small and shared. When it is full, new jobs queue, no matter how idle the main thread is.
What uses the thread pool
The main users in the standard library:
File system operations. Almost all of
node:fs, includingreadFile,writeFile,statandreaddir, in both callback and promise forms. File reads have no portable non-blocking mode, so each call occupies a pool thread while it waits on the disk.**DNS lookups through
dns.lookup.** This is the function used by default when you connect to a host name, and it calls the system resolvergetaddrinfo, which blocks. By contrast,dns.resolveuses a separate DNS library, c-ares, over the network and does not use the pool.**Some
node:cryptofunctions.**pbkdf2,scrypt,randomBytes,randomFillandgenerateKeyPairin their asynchronous forms.**
node:zlib.** Asynchronous compression and decompression.
Network sockets, HTTP servers, pipes, timers and child processes do not use the pool for their I/O.
Seeing the difference in order
An asynchronous file read runs off the main thread, so your code continues, while the synchronous version blocks.
import { readFile, readFileSync } from "node:fs";
readFile(import.meta.filename, () => console.log("async read finished"));
console.log("after readFile call");
readFileSync(import.meta.filename);
console.log("sync read finished");after readFile call
sync read finished
async read finishedThe async call returns at once, and its callback is queued for a later tick. The synchronous read blocks the main thread until it completes, and the line after it runs before the loop gets to the callback. Under load, every Sync call in a request path stops all other requests for as long as it takes.
The pool is small by default
The pool has four threads unless you change it. Five asynchronous jobs submitted at once do not run in parallel: four start, and the fifth waits for one to finish.
import { pbkdf2 } from "node:crypto";
import { promisify } from "node:util";
const hash = promisify(pbkdf2);
const jobs = [];
for (let i = 1; i <= 5; i++) {
console.log(`queued ${i}`);
jobs.push(hash("password", "salt", 200_000, 32, "sha512"));
}
await Promise.all(jobs);
console.log("all done");queued 1
queued 2
queued 3
queued 4
queued 5
all doneThe printed lines are the same every run, and the time is not. On a machine with enough cores, jobs one to four finish together after roughly one hash duration, and job five finishes after about two, because it had to wait for a free thread. Time the sample and the total will be close to twice a single hash, not the same as one, which is the signature of a saturated pool. A thread pool of four is a throughput ceiling for those operations, however many requests arrive.
Setting the size
The environment variable UV_THREADPOOL_SIZE sets the number of threads, from the default of 4 up to a maximum of 1024. It must be set before the pool is first used, so set it in the environment that starts the process.
UV_THREADPOOL_SIZE=8 node server.mjsDo not assign it from inside the program after other code has run, as the pool may already exist and the change would not apply on every platform. Putting it in the service manager or container configuration is the dependable approach.
More threads help only when jobs wait on something, and the number of useful threads is bounded by the work. CPU-heavy jobs such as hashing gain nothing beyond the number of cores, since extra threads share the same cores. Jobs that wait on slow disks or on DNS can benefit from more threads, and too many cause contention and memory use, since each thread has its own stack. You can find out a reasonable upper bound for CPU work with os.availableParallelism().
import { availableParallelism } from "node:os";
console.log(typeof availableParallelism(), availableParallelism() >= 1);number trueThe exact number depends on the machine. Use it as a guide for the pool size for CPU-bound work, and test with a realistic load before changing the default.
Symptoms of pool starvation
The pool is shared, so one kind of work can delay another. A server that hashes passwords on every login, with the default four threads, can see ordinary file reads and DNS lookups slow down during a burst of logins, though the main thread is nearly idle and the network is fine. Metrics show request latency rising while CPU use on the main thread stays low. The clue is that unrelated operations slow down together.
Check by measuring event loop delay and by timing the individual operations. If the event loop is healthy and the asynchronous operations are slow, suspect the pool. Remedies are a larger pool where the work is I/O-bound, moving heavy CPU work to worker threads, or limiting concurrency so that hashing cannot occupy every thread.
Worker threads are different
The libuv pool runs native operations that Node.js provides. It does not run your JavaScript. For your own CPU-heavy JavaScript, node:worker_threads starts additional threads, each with its own V8 isolate and event loop, and communicates through messages. The two are independent: workers do not consume pool threads for their JavaScript, though any file or crypto calls they make use the pool like any other code in the process.
Practical rules
Prefer asynchronous APIs in anything that serves many users, and reserve synchronous ones for start-up code and short scripts. Do not assume more concurrency means more speed: limit how many pool-backed operations run at once, for example hashing at most a few passwords at a time. Remember that DNS lookups for outgoing connections use the pool, so a slow resolver can tie up threads. And measure before tuning, since the right size depends on the workload.


