Skip to content

Troubleshooting guide

Node.js event loop lag: how to measure and fix it

Node.js runs your JavaScript on one thread. When something blocks it, every other request waits, and your timings all get worse together.

3 min read10 sectionsUpdated

On this page

Key points

  • Every request shares the same thread.
  • Blocked loops look like everything is slow.
  • JSON.parse on large payloads is a frequent hidden cause.
  • CPU profiles identify the exact function.

What event loop lag is

It is the delay between when a timer should have fired and when it did. A healthy loop has a lag of a few milliseconds. If p99 reaches hundreds of milliseconds, requests queue behind the blocking work, even when database and network are fast.

Common causes

  • Synchronous functions: fs.readFileSync, crypto.pbkdf2Sync, zlib.gzipSync.
  • Parsing or stringifying huge JSON.
  • Long loops over big arrays.
  • Catastrophic regular expressions.
  • Long GC pauses, driven by heavy allocation.

Diagnose it

Measure lag in production, then capture a CPU profile under load.

bash
// Measure in your app
const { monitorEventLoopDelay } = require('node:perf_hooks');
const h = monitorEventLoopDelay({ resolution: 20 });
h.enable();
setInterval(() => {
  console.log({ p50: h.percentile(50) / 1e6, p99: h.percentile(99) / 1e6 });
  h.reset();
}, 10000);

# Capture a CPU profile for 60 seconds
node --cpu-prof --cpu-prof-dir=/tmp/prof server.js

# Open the .cpuprofile in Chrome DevTools, Performance tab

Fixes

Replace sync APIs with async ones in request paths. Move CPU work to worker_threads or a separate service. Stream large responses, and paginate. Break long loops with setImmediate. Check regular expressions that use nested quantifiers. Limit the size of bodies you accept, because an attacker's huge JSON is also a blocking attack. Test the fix under realistic concurrency, because a single request on a developer machine will never reveal a blocked loop.

Why every metric lies during a block

When the loop stalls, even the timers measuring your database calls fire late. A query that took 5 ms can be recorded as 400 ms, simply because the callback waited behind blocked code. Teams then blame the database and investigate the wrong system. If every span gets slower together, on all routes and all dependencies, suspect the event loop first. Ten or more instances will also not share the pain equally: the instance handling the expensive request stalls, while others remain fine, which appears as noisy p99 values.

Catch it automatically

Export event loop p99 as a metric, and alert at around 100 ms sustained. In APM, compare it with latency. See Node.js performance monitoring for the full set of signals.

Frequently asked questions

How much lag is normal?

A few milliseconds at p50 and tens at p99 under load.

Does clustering fix it?

It adds parallel loops but does not remove blocking from each.

Are worker threads expensive?

They use memory, so use a small pool.

Watch your own site and services with VitalRix

Uptime checks, APM, logs, real-user monitoring, alerts and status pages in one place. Every product has a free tier and no credit card is needed.