Skip to content

Lesson 7 of 9 · 24m

Measuring Event-Loop Lag and Runtime Health

Instrument event-loop delay, active handles, garbage collection pressure, and request latency so runtime symptoms lead to actionable diagnosis.

If you run Node in production without event-loop metrics, you are flying with the altimeter covered. CPU can look fine while request latency collapses because one synchronous parser or a microtask storm prevents the loop from servicing sockets. The trick is to measure delay directly and correlate it with what the process was doing.

Measure delay, not vibes

Node exposes event-loop delay histograms through perf_hooks:

import { monitorEventLoopDelay } from 'node:perf_hooks';
const h = monitorEventLoopDelay({ resolution: 20 });
h.enable();
setInterval(() => {
  console.log({ meanMs: h.mean / 1e6, p99Ms: h.percentile(99) / 1e6, maxMs: h.max / 1e6 });
  h.reset();
}, 10_000).unref();

This tells you how late event-loop callbacks were. It does not tell you why. Treat it as a smoke detector: essential, but not the fire marshal's report.

Build the correlation matrix

Runtime health is a bundle of signals:

event-loop p99       scheduling delay
process CPU          user/system execution pressure
heapUsed             JS object memory
external             buffers and native-associated memory
active handles       sockets, timers, watchers
gc pause count/time  collection pressure
request latency      user-visible consequence
queue depth          application backlog

A latency spike with high loop lag and normal CPU often means scheduling starvation. High loop lag with high CPU suggests synchronous work or GC. High latency with low loop lag may mean downstream dependencies rather than local blocking.

GC metrics need context

You can observe GC events through perf_hooks or production APM tooling. Short frequent scavenges are normal. Repeated long major GCs with rising heap occupancy usually mean allocation churn or retention pressure. Do not start with heap size tuning because the dashboard is red; establish whether objects should exist at all.

Health checks can lie

A /healthz endpoint answered by the same event loop you are testing is a biased witness. If the loop stalls badly, the health check times out even though the process is alive. That is sometimes correct — the service is unavailable — but liveness and readiness should be distinct:

liveness    process is not deadlocked beyond recovery
readiness   service can accept and process traffic now

A container orchestrator that restarts on every transient readiness dip can amplify a partial outage. Measure before choosing probe behavior.

Include libuv pool saturation

Thread-pool exhaustion does not look like a normal thread dump problem. fs calls queue, DNS lookups stall, crypto operations back up, and suddenly unrelated endpoints time out. Instrument pool-heavy dependencies and expose work-queue metrics where libraries support them. If UV_THREADPOOL_SIZE is raised, record it as a deployment fact.

Field notes

  • Event-loop lag percentiles matter more than mean; the mean hides short catastrophic stalls.
  • Container CPU throttling can produce loop delay that looks like application blocking. Check cgroup metrics before rewriting code.
  • Export histograms, not just averages. A single 500 ms pause is operationally different from fifty 10 ms pauses.
  • Keep metric labels low-cardinality. Per-user or per-request IDs in metrics create memory problems inside the observability system itself.

Was this lesson helpful?

Sign in to rate