Full answer
Network sockets use the operating system's event notification (epoll on Linux), so they do not need extra threads. Some work has no such mechanism, and libuv runs it on a pool of worker threads instead. This pool is shared by the whole process.
Typical users of the pool:
- Most fs operations (readFile, stat, open and so on).
- dns.lookup (not dns.resolve, which uses the network).
- zlib compression and decompression.
- crypto functions such as pbkdf2, scrypt, randomBytes and randomFill.
The default size is 4 and the maximum is 1024. Set it with the UV_THREADPOOL_SIZE environment variable, before the first use of the pool. The best place is the command that starts the process, for example UV_THREADPOOL_SIZE=8 node server.js.
The bottleneck appears when slow tasks fill the pool. Suppose four password hashes with scrypt run at once. A fifth task, even a tiny fs.stat, waits for a free thread, so a file read can take hundreds of milliseconds even though the disk is idle. The symptom is high latency in fs or dns calls while CPU usage is moderate.
Raising the pool size helps only up to the number of CPU cores for CPU-bound jobs. Better options are limiting how many hashes run at once, using a separate worker thread pool for heavy jobs, or using a dedicated service for password hashing.
const { spawnSync } = require('node:child_process');
// The child shows what happens with a pool of one thread and a pool of four.
const childCode = `
const crypto = require('node:crypto');
const fs = require('node:fs');
const order = [];
// A heavy job takes the pool thread(s).
for (let i = 0; i < 4; i++) {
crypto.pbkdf2('password', 'salt', 300000, 64, 'sha512', () => order.push('hash ' + i));
}
// A tiny job that also needs a pool thread.
fs.stat(__filename === '[eval]' ? '/' : __filename, () => order.push('fs.stat'));
setTimeout(() => console.log('fs.stat finished ahead of all hash jobs:', order[0] === 'fs.stat'), 3000);
`;
for (const size of ['1', '4', '8']) {
const r = spawnSync(process.execPath, ['-e', childCode], {
env: { ...process.env, UV_THREADPOOL_SIZE: size },
encoding: 'utf8',
});
console.log('pool size', size + ':', r.stdout.trim());
}Each child process gets its own pool size. With four heavy jobs queued first, a pool of 1 or 4 leaves the tiny fs.stat call waiting for a free thread, while a pool of 8 has a spare thread for it right away. Save it as app.js and run it with node app.js. Output (Node.js v24.22.1, same on two runs): pool size 1: fs.stat finished ahead of all hash jobs: false pool size 4: fs.stat finished ahead of all hash jobs: false pool size 8: fs.stat finished ahead of all hash jobs: true
Key points
- Pool of 4 threads by default, shared by fs, dns.lookup, zlib and several crypto functions.
- Sockets do not use the pool.
- Slow tasks can delay unrelated fs and dns calls by queueing.
- UV_THREADPOOL_SIZE must be set before the pool is first used.
Common mistakes
- Thinking all async APIs run on the thread pool. Network I/O does not.
- Setting UV_THREADPOOL_SIZE inside the script after the pool already started.
- Setting the pool size far above the CPU core count for CPU-bound jobs.
- Confusing dns.lookup (pool) with dns.resolve (network).
Follow-ups
- How would you notice that the thread pool is saturated in production?
- Why is it risky to run many scrypt calls in an HTTP handler?
- How is this pool different from worker_threads?
Tips
- Link the symptom (slow fs or dns while CPU is calm) to the cause.
- Mention that the default size 4 is not tied to the number of cores.
- Offer two fixes: limit concurrency and isolate heavy work.