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
// 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 tabFixes
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.
Related reading
- ERR_BLOCKED_BY_CLIENT: how to fix it: Fix net::ERR_BLOCKED_BY_CLIENT: ad blockers and privacy extensions blocking scripts, images and API calls, and how developers avoid false blocks.
- ERR_UNSAFE_PORT: how to fix it: Fix ERR_UNSAFE_PORT: Chrome blocks web requests to ports used by other protocols (SMTP, IRC, SIP). Move your service to a safe port or allow it locally.
- Kafka consumer lag: how to measure and reduce it: Measure and reduce Kafka consumer lag: read kafka-consumer-groups output, spot rebalances and slow processing, and decide when more partitions will help.
- Apache "server reached MaxRequestWorkers": how to fix it: Fix Apache server reached MaxRequestWorkers setting: choose the right MPM, calculate workers from memory and move away from mod_php for better concurrency.
- EADDRINUSE: address already in use and how to fix it: Fix EADDRINUSE in Node.js and other servers: find which process holds the port, handle old processes after a crash and avoid race conditions in restarts.
- Kubernetes pod stuck in Pending: how to fix it: Fix Kubernetes pods stuck in Pending: insufficient CPU or memory, taints, node selectors and unbound PersistentVolumeClaims, using scheduler events.
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.