What blocks a Node server
Your health check normally answers in 3 ms, then one user asks for a report and every request takes two seconds. Node runs all your JavaScript on one thread, so a single handler that computes instead of waiting stalls everyone. Here is why, how to spot it, and how to move the work off that thread.
01 A health check that takes two seconds
Here is a tiny server with one slow route and one fast one. The slow route does two seconds of CPU work, standing in for a big report, image resize or password hash.
That is a real run on Node 24. /health does no work at all, yet it waited almost 1.9 seconds: the 2 seconds of the report, minus the 0.1 seconds it started late.
02 One thread runs all your JavaScript
A Node process has one main thread that runs every callback you write: request handlers, timers, promise continuations. The event loop hands that thread one callback at a time, and only takes the next when the current one returns.
Most server work is waiting: for the database, for a file, for another service. Node does that waiting without the thread. You start the I/O, the callback returns, and the thread is free to run other requests until the result comes back. That is how one thread serves thousands of connections.
03 What happens to the other requests
Follow the three requests from the measurement: one report, then two health checks that arrive while it is computing. The timings are examples that match the run above.
- just arrived or started
- still running
- waiting or done
The server is listening. No request has arrived, the main thread is idle, and the event loop is waiting for something to happen.
Marking a function async does not move its work anywhere. An async handler that loops for two seconds still loops on the main thread; await only frees the thread while it waits for a promise that something else will settle.
04 What counts as blocking
Anything that keeps one callback running for a long time blocks. A rough guide: a few milliseconds per request is fine; tens of milliseconds start to show up in tail latency under load; hundreds stall the server.
| Culprit | Why it blocks | Non-blocking option |
|---|---|---|
| JSON.parse / JSON.stringify | Linear in payload size; a body of many MB can take tens of ms or more | Limit body size; stream large data |
| crypto.pbkdf2Sync, scryptSync | Deliberately slow hashing, on the main thread | crypto.pbkdf2 / scrypt (thread pool) |
| fs.readFileSync and friends | Waits for the disk on the main thread | fs.promises.readFile |
| zlib.gzipSync | Compression is CPU work | zlib.gzip or a stream |
| Loops over large arrays, big sorts | Plain computation | Paginate, or a worker thread |
| A regex with nested quantifiers | Backtracking can take exponential time on bad input | Rewrite the pattern; limit input length |
The *Sync functions are fine at start-up, before the server listens. Inside a request handler, they block every other request for as long as they take.
05 Measure the stall yourself
Node can tell you how late the event loop is running. monitorEventLoopDelay samples how long callbacks wait beyond when they should have run. Here a 300 ms busy loop runs inside a timer:
In production, export h.percentile(99) as a metric. A p99 event-loop delay that climbs with traffic is the clearest sign of a blocking handler. Now compare a sync and an async password hash, each with a 10 ms timer set just before:
setTimeout(() => log('timer'), 10);crypto.pbkdf2Sync('pw', 'salt', 600000, 32, 'sha256');// sync hash took 47 ms// timer fired after 47 ms
setTimeout(() => log('timer'), 10);crypto.pbkdf2('pw', 'salt', 600000, 32, 'sha256', done);// timer fired after 13 ms// async hash took 46 ms
Real runs, Node 24 on an Apple laptop; your milliseconds will differ. The hash costs the same either way. The difference is where it runs: the async version hands it to libuv’s thread pool, so the main thread is free to fire the timer on time.
06 Getting the work off the main thread
There are three standard moves, from least to most effort:
- Use the async API when one exists.
crypto,zlib,fsand DNS lookups have versions that run on libuv’s thread pool, which has 4 threads by default (setUV_THREADPOOL_SIZEto change it). Four slow hashes at once fill the pool, and the fifth waits. - Split the work into chunks and yield between them with
setImmediate. Each chunk is short, so other requests run in the gaps. The total time grows a little, but nothing stalls for long. - Run it on a worker thread with
worker_threads, or in a separate process or job queue. The computation moves to another thread with its own event loop; the main thread only waits for a message.
Here is the same server with the report on a worker thread:
A real run: the report still takes about 2 seconds, but /health answers in under 2 ms while it runs. Starting a worker costs some milliseconds and memory, so for many small jobs keep a pool of workers instead of one per request; for long jobs a queue such as the one in Background jobs is often simpler.
07 When it bites
app.post('/import', async (req, res) => {
const rows = JSON.parse(req.body);
rows.sort(byDate);
});async on a CPU-heavy handlerasync changes the return type, not the thread. The parse and the sort still run on the main thread. Bound the input size, or move the work to a worker.bcrypt.hashSync(password, 12);Sync password hashing on loginHashes are slow on purpose; a sync call blocks the whole server once per login. Use the async version so it runs off the main thread.for (const user of await db.allUsers()) {
render(user);
}Loading everything, then loopingWaiting for the query is fine; the loop over a million rows is not. Page through results, or stream them and process in chunks./^(a+)+$/.test(input);A regex that backtracksOn input like many as followed by !, this pattern can take exponential time, and a single request freezes the server. Avoid nested quantifiers on user input and cap its length.UV_THREADPOOL_SIZE=16 node server.jsA small thread pool under loadAsync crypto, zlib and fs share 4 threads by default. Under many concurrent hashes they queue for the pool: the main thread stays free, but those requests still slow down.