Beginner Node.js Interview QuestionsBeginnerConcept
Node.js · Question 9
What is the Node.js thread pool?
Direct answer
libuv maintains a fixed-size thread pool (default 4 threads, UV_THREADPOOL_SIZE) for work that cannot use fully non-blocking OS APIs — including fs.* on some systems, crypto, compression, and dns.lookup.
The main JavaScript thread stays free, but pool threads do blocking work on Node's behalf. If all four are busy, fifth crypto.hash waits in queue — latency spikes under load.
| Often uses thread pool | Usually true async (no pool) |
|---|---|
| fs.readFile / write on some platforms | TCP net.Server accept/read |
| crypto.pbkdf2, scrypt, randomBytes (some paths) | net.connect on modern OS |
| dns.lookup (not dns.resolve) | fs.read on Linux io_uring paths (evolving) |
| zlib compression | Most timer and setImmediate scheduling |
- UV_THREADPOOL_SIZE — set env var before process start; max 1024; tune for crypto-heavy APIs.
- dns.lookup vs dns.resolve — resolve uses network (no pool); lookup uses getaddrinfo (pool) — common perf interview detail.
- Not unlimited parallelism — more concurrent bcrypt than pool size queues — use worker_threads for isolation (Q69).
thread-pool-crypto.js
import { pbkdf2 } from "node:crypto";
console.log("Starting 6 password hashes on 4 pool threads…");
for (let i = 0; i < 6; i++) {
pbkdf2("password", "salt", 100000, 64, "sha512", () => {
console.log(`hash ${i} done`);
});
}
// First 4 run in parallel; 5th and 6th wait for a free thread