Advanced Node.js Interview QuestionsAdvancedConcept
Node.js · Question 76
How do you monitor Node.js application performance?
Direct answer
Track event loop lag, request latency p95/p99, error rates, memory/CPU, and DB pool saturation — use structured logs (pino), health endpoints, process.memoryUsage, and APM tools; alert on SLO breaches not just crashes.
Production observability spans logs, metrics, and traces — Node-specific signals include event loop delay and GC pauses that generic server metrics miss.
| Signal | Tool / API | Indicates |
|---|---|---|
| Request duration | Fastify hooks + log | Slow routes, N+1 queries |
| Event loop delay | perf_hooks.monitorEventLoopDelay | CPU block, sync fs |
| Memory | process.memoryUsage() | Leaks (Q72), cache too large |
| Errors | setErrorHandler + counter | 5xx spike, bad deploy |
| External deps | pg pool waitingCount | DB bottleneck |
- /health — liveness (process up) vs readiness (DB connected).
- Request ID — correlate logs across API → worker → DB.
- Load testing — k6/autocannon before launch; watch p99 not just avg.
perf-monitor.mjs
import { monitorEventLoopDelay } from "node:perf_hooks";
const delay = monitorEventLoopDelay({ resolution: 20 });
delay.enable();
setInterval(() => {
const mem = process.memoryUsage();
console.log(JSON.stringify({
heapUsedMb: Math.round(mem.heapUsed / 1024 / 1024),
eventLoopP99Ms: delay.percentile(99) / 1e6,
}));
delay.reset();
}, 60_000).unref();