Node.js › Event Loop & Non-blocking IO
What is 'event loop lag' (or 'event loop delay'), and how would you monitor it in a production Node.js application?
AdvancedAnswer
Event loop lag measures the delay between when a scheduled callback (like a timer) should ideally fire and when it actually does, serving as a proxy for how 'busy' or blocked the event loop currently is -- rising lag indicates the event loop is spending too much time on synchronous work (or too many queued callbacks) to process new events promptly, directly translating into slower response times for every request being handled by that process, making it a critical production health metric.
const { monitorEventLoopDelay } = require('node:perf_hooks');
const histogram = monitorEventLoopDelay({ resolution: 20 });
histogram.enable();
setInterval(() => {
console.log('Mean lag (ms):', histogram.mean / 1e6);
histogram.reset();
}, 10000);Real-world example A team notices their API's response times degrade under load despite CPU usage looking reasonable, and adds event-loop-delay monitoring which reveals significant lag spikes correlating exactly with a specific synchronous data-transformation function, leading them to move that work to a worker thread.