Node.js Internals #2: libuv and the Thread Pool
Part 2 of the Node.js Internals series. Part 1 covered the event loop. This one is about what the loop hands work to. Every output below was run on Node v26.8.1 on a 12-core Mac; your millisecond numbers will differ, the shape will not.
"Node is single-threaded" is half true. Your JavaScript runs on one thread. But a Node process also has a pool of helper threads, and knowing what runs there (and what does not) explains a lot of "why is my server slow" mysteries.
What libuv is
libuv is the C library under Node. It owns the event loop and the bridge to the operating system. It does two very different things:
- Network I/O on the loop thread. Sockets are set to non-blocking mode and watched with the best OS mechanism available (epoll on Linux, kqueue on macOS and BSDs, IOCP on Windows). No extra threads involved.
- Everything the OS cannot do without blocking goes to a thread pool. File system calls, DNS lookups through
getaddrinfo, and CPU-heavy built-ins like crypto and zlib.
type Where = "loop thread (epoll/kqueue)" | "libuv thread pool";
const whereItRuns: Record<string, Where> = {
"http / net / tls sockets": "loop thread (epoll/kqueue)",
"fs.readFile, fs.stat, ...": "libuv thread pool",
"dns.lookup": "libuv thread pool",
"dns.resolve4": "loop thread (epoll/kqueue)", // c-ares, a separate DNS library
"crypto.pbkdf2, scrypt, ...": "libuv thread pool",
"zlib (async APIs)": "libuv thread pool",
};
The pool's default size is 4 threads, configurable through the UV_THREADPOOL_SIZE environment variable up to 1024. It is global: every task from every one of those APIs queues into the same four slots.
Experiment 1: the pool is a queue of 4
Eight password hashes, started at the same time:
const crypto = require("crypto");
const start = Date.now();
for (let i = 1; i <= 8; i++) {
crypto.pbkdf2("pw", "salt", 600000, 64, "sha512", () =>
console.log(`hash ${i} done at ${Date.now() - start} ms`));
}
# default pool (4) # UV_THREADPOOL_SIZE=1
hash 4 done at 125 ms hash 1 done at 110 ms
hash 1 done at 145 ms hash 2 done at 220 ms
hash 3 done at 146 ms hash 3 done at 330 ms
hash 2 done at 166 ms hash 4 done at 440 ms
hash 5 done at 249 ms ...
hash 6 done at 278 ms hash 8 done at 929 ms
hash 7 done at 283 ms
hash 8 done at 298 ms
With 4 threads you see two waves: four finish near 150 ms, the next four near 280 ms. With 1 thread they finish one at a time, about 110 ms apart. With UV_THREADPOOL_SIZE=8 all eight finished within about 30 ms of each other (194 to 222 ms): all eight run at once, though each one is slower than when only four ran together.
Experiment 2: a full pool delays unrelated work
Fill the pool with four long hashes, then read a file and make an HTTP request to a local server:
for (let i = 0; i < 4; i++) crypto.pbkdf2("pw", "salt", 1200000, 64, "sha512", () => {});
fs.readFile(__filename, () => console.log("readFile finished after", ms, "ms"));
http.get({ host: "127.0.0.1", port }, ...); // round trip
http round trip 5 ms (no pool)
fs.readFile finished after 225 ms (queued behind hashes)
A tiny file read waited 225 ms because all four threads were busy hashing. The HTTP round trip, which never touches the pool, took 5 ms. That is the practical rule: slow pool tasks slow down fs and DNS, not sockets.
A trap I hit while testing
My first version of that experiment used http.get({ port }) with no host, and the HTTP request took 238 ms, apparently contradicting "network does not use the pool". The reason: with no host, Node connects to localhost, and resolving a hostname goes through dns.lookup, which is a pool task. The socket was never the slow part; the name lookup queued behind the hashes. Using the IP literal 127.0.0.1 skips the lookup and the delay disappears.
dns.lookup("localhost", ...) // 6 ms, goes through the thread pool (getaddrinfo)
dns.resolve4("localhost", ...) // 13 ms, c-ares, does not use the pool
Why does dns have two APIs?
dns.lookup calls the operating system's resolver (getaddrinfo), so it honours /etc/hosts and system settings, but that call blocks, so Node runs it on a pool thread. dns.resolve* speaks to DNS servers directly through a separate library and uses no pool thread. Most of Node, including http and net, uses lookup by default. Under a busy pool, DNS can therefore become the hidden bottleneck of outgoing requests.
Cheatsheet
| API | Runs on | Pool pressure |
|---|---|---|
Sockets: http, net, tls | Loop thread | None |
fs.* (async) | Pool | 1 slot per call |
dns.lookup | Pool | 1 slot per call |
dns.resolve* | Loop thread | None |
crypto.pbkdf2 / scrypt / randomBytes | Pool | CPU-heavy, holds a slot |
zlib (async) | Pool | CPU-heavy, holds a slot |
| Your own JS | Loop thread | Blocks everything |
- Default pool: 4 threads. Max: 1024. Set
UV_THREADPOOL_SIZEbefore the process starts. - More threads helps only up to your CPU core count for CPU-bound tasks; past that you just add context switching.
- If fs or DNS latency spikes while sockets are fine, check what else is hashing or compressing.
- Pool threads cannot run your JavaScript. For that you need worker threads (Part 4).
Conclusion
Two paths, one loop. Sockets are watched by the OS and cost nothing per connection. Blocking work is shipped to four threads by default. Almost every "Node got slow for no reason" story is one of these paths being full: your JS blocking the loop, or the pool queue backed up.
References
- libuv: Thread pool work scheduling (default size 4, max 1024)
- libuv: Design overview (network I/O always on the loop thread, file I/O in the pool)
- Don't Block the Event Loop (or the Worker Pool), Node.js docs (the list of APIs that use the pool)
- Node.js dns module (lookup vs resolve)
- Node.js CLI docs (
UV_THREADPOOL_SIZE)
Node.js Internals: Part 1 · Part 2 · Part 3 · Part 4 · Part 5