EVENT LOOP STARVATION
One long task and every other request waits
A Node service starts a synchronous JSON.parse that takes 400 ms. A GET /health request arrives 100 ms into it. When does the health handler run?
A single-threaded event loop runs one task at a time, to completion. Timers, incoming requests and I/O callbacks go into a queue and run when the current task returns. A synchronous 400 ms JSON.parse is one task, so a health check due at 100 ms waits until 400 ms. The fix is to make the long task give the loop back: process a slice, await new Promise(r => setImmediate(r)), process the next slice. A bare await on an already-resolved promise is not enough: it only runs microtasks, and the probe still waits. The queue drains between slices, and the probe waits at most one slice. If one slice is still too long, the work does not belong on this thread: post it to a worker.
app.post('/import', (req, res) => {
const rows = JSON.parse(req.body); // 400 ms of CPU. Nothing else runs.
for (const r of rows) insert(r);
res.send('ok');
});
app.get('/health', (req, res) => res.send('ok')); // queued behind the loopapp.post('/import', async (req, res) => {
const rows = JSON.parse(req.body); // still sync: move it to a worker if it is big
for (let i = 0; i < rows.length; i++) {
insert(rows[i]);
if (i % CHUNK === 0) await new Promise((r) => setImmediate(r)); // timers and I/O run here
}
res.send('ok');
});
// or: new Worker('./import-worker.js').postMessage(req.body) — main loop stays free Watch the invariant strip after the loop’s first turn in BLOCKING TASK: the probe has been in the queue longer than its timeout, so it has already failed before the handler ever runs. A few failures like that in a row and the kubelet restarts the pod. In FIX: CHUNKED, drag ROWS PER SLICE and watch the longest wait track the slice length.
The probe SLO is 25 ms, not 200. The import still has at least 400 ms of CPU to do. A probe can arrive at any moment in a slice, so find a slice size that keeps every slice, and so the worst queue wait, under 25 ms.
GOAL: every slice <= 25 ms and longest queue wait <= 25 ms, with IMPORT CPU >= 400 ms
Each item costs 1 ms of CPU. A probe arrives every 100 ms, starting at any moment, and must be served within 50 ms. Rewrite processAll so it yields to the loop often enough, and still processes every item exactly once.
Node and browser code that parses, serializes, compresses, hashes or sorts a large payload inline; a regex with catastrophic backtracking; a tight loop over a big array in a request handler. The symptom is failed health checks, a frozen UI, or p99 spikes that look like network problems.
“Why did the health check fail if the process was fine?” and “How would you make this handler not block?” Say that the loop runs one task to completion, name event-loop lag as the metric, and give both fixes: chunk with setImmediate for work you can slice, a worker thread for work you cannot.
USED IN
Chapters that build on this lesson.
- Continuous profilingObservability