Dec 3, 2026
One request held the event loop — and the whole dashboard froze
Node.js runs your JavaScript on a single thread. One request doing heavy synchronous work — a big JSON.parse, a sync password hash, a long loop — holds that thread, and every other request waits: the dashboard, the health check, the live updates. The database is idle. The fix is knowing which work blocks, measuring the lag, and moving the CPU work off the main thread.
The dashboard froze for a few seconds. Not one page — the whole thing. The live numbers stopped updating, a save spinner spun, and then everything came back at once, like nothing had happened.
The database was idle. CPU was at 100% on one core and near zero on the rest. The health check endpoint, which does nothing but return 200, had timed out too.
That combination — everything slow at the same time, database fine, one core busy — is the signature of a blocked event loop. One request was doing heavy work synchronously, and while it held the thread, nothing else in the process could run at all.
The event loop in one paragraph
Node.js runs your JavaScript on a single main thread, driven by an event loop. I/O — network, disk, timers — is handed off to the operating system and libuv, and its callback runs later, when the result is ready. That is what makes Node fast at handling many connections at once.
But the JavaScript itself runs one callback at a time, and Node never interrupts a callback. A callback that computes for three seconds holds the only JavaScript thread for three seconds. While it runs, Node cannot accept new connections, parse incoming requests, run timers, or send responses that are already prepared. The requests queue up and come back all at once when the loop finally frees up — which is exactly the freeze-then-flood I saw.
The shape of the bug is a handler like this. A shop dashboard lets a manager bulk-import a price list, and the handler does all of it inline:
// POST /api/import — one request, whole server frozen
app.post("/api/import", (req, res) => {
const products = JSON.parse(req.body.file); // multi-MB string, synchronous
for (const p of products) {
validateRow(p); // formatting, parsing, checking — all synchronous
db.insert(p);
}
res.json({ imported: products.length });
});
The database calls are async and fine. Everything around them is not. On a small test file this takes milliseconds. On a real price list it holds the thread for seconds, and every other user of the dashboard pays for it.
async does not save you
The most common misunderstanding here is that making the function async fixes it. It does not. async only lets a function pause at await points. The code between two awaits runs synchronously, on the main thread, and nothing can interrupt it:
async function importProducts(file: string) {
const products = JSON.parse(file); // still synchronous — await can't interrupt it
await saveAll(products);
}
Only I/O is handed off. CPU work — parsing, hashing, formatting, looping — stays on the thread until it returns. If you want proof, put a setInterval that logs every second next to a three-second synchronous loop: the interval fires late, once, after the loop finishes.
The usual suspects are always the same. A big JSON.parse or JSON.stringify. A synchronous password hash — crypto.pbkdf2Sync, or a bcrypt hash called the sync way — on a login route, so every login freezes the server for everyone. A loop over thousands of rows that calls toLocaleString or builds an Intl formatter per row. A regex with nested quantifiers on user input, which can turn catastrophic and hold the thread for far longer than any parse. And fs.readFileSync in a request handler, which is synchronous I/O wearing a familiar name.
Measure it before you fix it
A blocked loop is easy to suspect and easy to misdiagnose — slow queries and a slow network look similar from the browser. Node ships the measurement built in, in node:perf_hooks:
import { monitorEventLoopDelay, performance } from "node:perf_hooks";
const h = monitorEventLoopDelay({ resolution: 10 }); // sample every 10ms
h.enable();
setInterval(() => {
console.log({
lag_p99_ms: h.percentile(99) / 1e6, // values are in nanoseconds
lag_max_ms: h.max / 1e6,
elu: performance.eventLoopUtilization().utilization.toFixed(2),
});
h.reset();
}, 10_000).unref();
Event loop delay is the wait between a callback being ready to run and the loop actually running it. If p99 is in seconds while the database is idle, the loop is the problem, not the query. Event loop utilisation is the fraction of time the loop spent running code instead of waiting for I/O — near 1 means the process is compute-bound, not waiting on anything. Two things to know: the values are in nanoseconds, so divide by 1e6 for milliseconds, and there is a floor around the sampling resolution — an idle process still reports roughly the resolution, so look at the shape over time, not a single number. For a one-off hunt, node --cpu-prof shows exactly which functions held the thread.
Where the work should go instead
The fix depends on the shape of the work:
Stream it, if the output can be produced piece by piece. A large import or export should flow row by row — parse and write in chunks — instead of loading the whole thing into memory and building one giant string. Each chunk is small, so other work gets a turn between chunks. (This is the same reason the export endpoint in my earlier post streams instead of buffering.)
Chunk it, if it has to stay in-process. Break a long loop into batches and yield between them with setImmediate, so the loop gives the thread back regularly:
async function processInChunks(rows: Row[]) {
for (let i = 0; i < rows.length; i += 500) {
rows.slice(i, i + 500).forEach(validateRow);
await new Promise((resolve) => setImmediate(resolve)); // yield the thread
}
}
Move it to a worker thread, if it is CPU work that must run now. worker_threads run JavaScript in parallel, in a separate V8 instance, without blocking the main loop — password hashing, image resizing, PDF generation:
import { Worker } from "node:worker_threads";
function hashInWorker(password: string): Promise<string> {
return new Promise((resolve, reject) => {
const w = new Worker("./hash-worker.js", { workerData: password });
w.once("message", resolve);
w.once("error", reject);
});
}
And the cheapest version of the same idea: use the async API instead of the sync one. crypto.pbkdf2 runs on libuv's thread pool, off the main thread; pbkdf2Sync does the same work on it.
Queue it, if the user can wait. Work that takes seconds or minutes — a big import, a report — belongs in a background job with a 202 Accepted and a result delivered later, not in a request handler holding the thread while the user watches a spinner.
The honest limits
Chunking does not make the total work faster — it spreads it out so other requests get a turn. Worker threads copy data between threads, so they pay off for real CPU work, not for moving a small object around. Adding more processes with the cluster module helps availability, but the default round-robin scheduling can still hand a request to a busy worker. And a few milliseconds of synchronous work is fine — the rule is not "never compute on the main thread", it is "never hold it for long enough that another request notices". Measure the lag, find the callback that holds the thread, and move only that work.
The dashboard freeze is not a scaling problem. It is one callback, holding one thread, that every other request has to wait behind. Find it with the histogram, and the whole server gets faster — not because you added capacity, but because you stopped letting one request borrow everyone else's time.