AceDevHub
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.

SignalTool / APIIndicates
Request durationFastify hooks + logSlow routes, N+1 queries
Event loop delayperf_hooks.monitorEventLoopDelayCPU block, sync fs
Memoryprocess.memoryUsage()Leaks (Q72), cache too large
ErrorssetErrorHandler + counter5xx spike, bad deploy
External depspg pool waitingCountDB 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();