Yousef AldabbasSystems & backend engineering
● ENGINEERING NOTESAMMAN, JORDAN
← back to posts

Node.js Internals #2: libuv and the Thread Pool

2026-11-09 · nodejs

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:

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

APIRuns onPool pressure
Sockets: http, net, tlsLoop threadNone
fs.* (async)Pool1 slot per call
dns.lookupPool1 slot per call
dns.resolve*Loop threadNone
crypto.pbkdf2 / scrypt / randomBytesPoolCPU-heavy, holds a slot
zlib (async)PoolCPU-heavy, holds a slot
Your own JSLoop threadBlocks everything

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

Node.js Internals: Part 1 · Part 2 · Part 3 · Part 4 · Part 5