Senior LevelNode JS
How do you measure and alert on Event Loop Lag in high-throughput Node.js microservices?
❓ Interview Prompt: Explain Event Loop Lag, main thread CPU blockage, measuring lag via `monitorEventLoopDelay()` in `node:perf_hooks`, and p99 thresholds.
Event Loop Lag is the delay between when a timer callback is scheduled and when the event loop actually executes it:
Diagnosis:
If Event Loop Lag p99 exceeds 50ms, the main JS thread is blocked by synchronous CPU operations or large JSON parses!
•
Use 'monitorEventLoopDelay()' from 'node:perf_hooks' to continuously measure p50, p90, and p99 lag histograms.
Code Implementation Example
javascript
import { monitorEventLoopDelay } from 'node:perf_hooks';
const histogram = monitorEventLoopDelay({ resolution: 20 });
histogram.enable();
setInterval(() => {
const p99Ms = (histogram.percentile(99) / 1e6).toFixed(2);
console.log('Event Loop Lag p99: ${p99Ms}ms');
if (parseFloat(p99Ms) > 50) {
console.warn('WARNING: High Event Loop Lag detected!');
}
histogram.reset();
}, 5000);💡Monitoring Event Loop Lag histograms using node:perf_hooks.
🎯 Interview Key Takeaway
Monitor Event Loop Lag p99 metrics to detect main-thread CPU bottlenecks before system failure occurs.
#Event Loop Lag#perf_hooks#Metrics#Monitoring#Senior
Back to all Node JS Questions→