How to tell when synchronous work is stalling a Node.js server, measure event loop delay, find the usual culprits, and fix them with async APIs, chunking and worker threads, with a measurement endpoint on Encore.

Node.js runs your JavaScript on one thread, and everything that thread does between awaits delays every other request. A handler that spends 200 ms hashing a password synchronously makes all other requests wait 200 ms, even the ones that would have taken one millisecond.
The symptom is odd latency: spikes on all endpoints at the same moment, a health check that fails while CPU looks modest, and timers that fire late. The cure starts with measuring.
Node can sample the lag itself with monitorEventLoopDelay, which records how late a timer fires into a histogram. Healthy services show a p99 of a few milliseconds, and a p99 in the tens or hundreds points at blocking work.
import { monitorEventLoopDelay } from "node:perf_hooks";
const histogram = monitorEventLoopDelay({ resolution: 10 });
histogram.enable();
setInterval(() => {
console.log({
p50Ms: histogram.percentile(50) / 1e6, // the histogram is in nanoseconds
p99Ms: histogram.percentile(99) / 1e6,
maxMs: histogram.max / 1e6,
});
histogram.reset();
}, 10_000).unref();
Alert on p99 and on the max, since a single 2-second stall can be invisible in an average.
The Node.js guide on not blocking the event loop lists the families, and in backends the same few come up:
Run the service under the built-in profiler with node --cpu-prof, or attach Chrome DevTools with --inspect, reproduce the stall and look at the longest function in the flame chart. Production-safe options are request-level timing logs plus the delay histogram above: when the delay spikes, the request that was running at that moment is the likely cause.
Prefer the asynchronous API when one exists, since many run on the libuv thread pool and leave the loop free. When the work is JavaScript itself, split it so other requests can run in between, using setImmediate to yield:
export async function processInChunks<T>(
items: T[],
work: (item: T) => void,
chunkSize = 500,
): Promise<void> {
for (let i = 0; i < items.length; i += chunkSize) {
for (const item of items.slice(i, i + chunkSize)) work(item);
// Let queued requests and timers run before the next chunk.
await new Promise<void>((resolve) => setImmediate(resolve));
}
}
For work that is both CPU-heavy and unavoidable, such as image resizing or large report generation, use worker_threads with a pool, so each task runs on its own thread and the main thread only passes messages.
Encore is a backend framework with built-in infrastructure, and for this problem its multithreading design matters: the Rust runtime parses and validates requests, serializes responses and runs database queries, Pub/Sub and object storage calls on multiple threads, so the single JavaScript event loop only runs your business logic. When a handler is CPU-heavy anyway, the same docs describe worker pooling, enabled with "build": {"worker_pooling": true} in encore.app, which starts one Node.js isolate per CPU and load-balances requests across them.

Run in your terminal to get started locally.
// auth/auth.ts
import { api } from "encore.dev/api";
import { monitorEventLoopDelay } from "node:perf_hooks";
import { pbkdf2, pbkdf2Sync, randomBytes } from "node:crypto";
import { promisify } from "node:util";
const loopDelay = monitorEventLoopDelay({ resolution: 10 });
loopDelay.enable();
const pbkdf2Async = promisify(pbkdf2);
// Blocks the event loop for the whole hash, so every request waits behind it.
export const hashBlocking = api(
{ expose: false, method: "POST", path: "/hash/blocking" },
async (req: { password: string }): Promise<{ hash: string }> => {
const salt = randomBytes(16);
const key = pbkdf2Sync(req.password, salt, 600_000, 32, "sha256");
return { hash: `${salt.toString("hex")}:${key.toString("hex")}` };
},
);
// Runs the hash on the libuv thread pool, so the loop keeps serving requests.
export const hashAsync = api(
{ expose: false, method: "POST", path: "/hash/async" },
async (req: { password: string }): Promise<{ hash: string }> => {
const salt = randomBytes(16);
const key = await pbkdf2Async(req.password, salt, 600_000, 32, "sha256");
return { hash: `${salt.toString("hex")}:${key.toString("hex")}` };
},
);
// Compare the two under load: the blocking version raises this number.
export const delay = api(
{ expose: false, method: "GET", path: "/debug/loop-delay" },
async (): Promise<{ p50Ms: number; p99Ms: number; maxMs: number }> => {
const out = {
p50Ms: loopDelay.percentile(50) / 1e6,
p99Ms: loopDelay.percentile(99) / 1e6,
maxMs: loopDelay.max / 1e6,
};
loopDelay.reset();
return out;
},
);
encore run starts the app locally, so you can fire parallel requests at each hash endpoint and read /debug/loop-delay between runs. The local development dashboard shows each request's trace, as the debugging docs describe, which makes the stalled ones easy to spot. On deploy, the app runs in your own AWS or GCP account as the AWS infrastructure docs describe, with tracing collected from there, and a coding agent that notices a slow handler can add the delay endpoint and compare before and after in one change.
Best fit: Teams running Node.js APIs where a few CPU-heavy handlers hurt every other request.
Latency spikes across every endpoint at once while CPU sits near one core, health checks time out, and timers fire late. Measure it with perf_hooks monitorEventLoopDelay: a p99 delay above a few tens of milliseconds means something synchronous is holding the loop.
Synchronous CPU work on the main thread: large JSON.parse or JSON.stringify calls, synchronous crypto such as pbkdf2Sync, regular expressions with catastrophic backtracking, synchronous file reads and long loops over big arrays. A request that waits on I/O does not block the loop, but one that computes does.
Use the asynchronous version of the API where one exists, split long loops into chunks that yield with setImmediate, stream large payloads instead of parsing them whole, and move CPU-bound work to worker threads so the main thread keeps serving requests.
With Encore, the Rust runtime handles request parsing, validation, serialization and database access on multiple threads, leaving the JavaScript event loop for your business logic. For CPU-heavy handlers you can enable worker pooling, which runs one Node.js isolate per CPU.
Deploy to your cloud on AWS and Google Cloud
without managing infrastructure
Encore lets developers and agents define application infrastructure directly in code, then automatically provisions it from local development to production in your AWS or GCP account.