skip to content

Blocking and Lag Measurement

One CPU-bound stretch freezes every connection the process serves. Interviewers want you to name what blocks, measure the lag instead of guessing, then pick chunking, streaming, a worker or a process.

on this pageshow

explore

questions

5

In a Node.js server, why do fs.readFileSync and crypto.pbkdf2Sync in a request handler stall every other client, while fs.readFile and crypto.pbkdf2 do not?

level: juniorimportance: must knowfreq 75%

answer

  1. one main thread, many sockets
  2. callback must return first
  3. work done inside vs handed off
  4. libuv threadpool takes the async twin

basics

~20 s

Node runs every request handler on one main thread. A Sync call does its read or hashing inside the current callback, so no other client is served until it returns; the async versions hand the work to libuv's threadpool.

solid answer

~40 s

Node serves every connection from one event loop on the main thread: it runs one callback, returns to libuv, then runs the next ready one. `fs.readFileSync` and `crypto.pbkdf2Sync` do all their work inside the current callback, so while they run Node cannot run any other callback: new connections wait in the kernel, data sits unread and timers fire late, health checks included. `fs.readFile` and `crypto.pbkdf2` submit the work to libuv's threadpool (four threads by default) and return immediately; their callback runs on the main thread when the result is ready. The same logic covers `zlib.gunzipSync`, `child_process.execSync` and pure JavaScript work such as a huge `JSON.parse`, which has no async twin at all. Sync calls are fine at startup, before `server.listen()`; the cost is paying them per request.

code

javascript · 22 lines
javascript
import { createServer } from 'node:http';
import { pbkdf2, pbkdf2Sync } from 'node:crypto';

createServer((req, res) => {
  if (req.url === '/sync') {
    // Runs on the main thread: no other callback runs until it returns.
    const key = pbkdf2Sync('secret', 'salt', 1_000_000, 64, 'sha512');
    res.end(key.toString('hex'));
  } else if (req.url === '/async') {
    // Runs on a libuv threadpool thread; the main thread keeps serving.
    pbkdf2('secret', 'salt', 1_000_000, 64, 'sha512', (err, key) => {
      if (err) {
        res.statusCode = 500;
        res.end();
        return;
      }
      res.end(key.toString('hex'));
    });
  } else {
    res.end('ok');
  }
}).listen(3000);

go deeper

for a junior

Recall that every handler shares one main thread, and that a Sync call holds it until the work is done. Name the async twin that frees it.

for a middle

Explain where the work actually runs: inside the callback for Sync calls, on libuv's threadpool for fs and crypto callbacks, and why pure JavaScript work has no async twin.

for a senior

Show you can find blocking calls in a real service with --trace-sync-io and loop-delay metrics, and that you know the threadpool can saturate even when the main thread is free.

for a principal

Treat Sync-at-startup versus Sync-per-request as a rule the team can check automatically, and weigh what each offloading choice costs the service.

## One thread runs every callback A Node.js process runs all of your JavaScript on **one main thread**, driven by the **event loop** that libuv provides. The loop waits for the operating system to report that something is ready (a new connection, bytes on a socket, a finished file read, an expired timer), runs the JavaScript callback for that event, and only when the callback **returns** does it go back for the next one. Every request handler in a `node:http` server is one of those callbacks. That design is why Node can serve thousands of connections with very few threads: no thread sits idle waiting for a slow client. The price is that **fairness is your job**. If one callback takes 400 ms, nothing else in the process runs for 400 ms: - new connections pile up in the kernel's accept queue and are not handled; - data that has already arrived on other sockets waits unread; - timers fire late, including the ones that implement request timeouts; - a load balancer's health-check request waits too, and may mark the instance as down. ## Sync APIs do their work inside your callback The `*Sync` APIs exist for scripts and startup code. `fs.readFileSync` performs the whole read, and `crypto.pbkdf2Sync` performs every iteration of the key derivation, **inside the callback that called them**. For that whole time the main thread is busy, so every other client waits. The cost scales with the input and the environment: reading a small file from a warm cache returns in microseconds, which is why the problem often appears only in production, where files are larger, disks are slower or a network file system adds unpredictable latency. Their asynchronous twins work differently. `fs.readFile`, `fsPromises.readFile` and `crypto.pbkdf2` hand the work to **libuv's threadpool** (four threads unless `UV_THREADPOOL_SIZE` says otherwise), return immediately, and Node runs your callback (or settles your promise) on the main thread once the result is ready. The main thread spends microseconds submitting the task instead of the whole duration doing it. | Call | Where the expensive part runs | Main thread blocked? | |---|---|---| | `fs.readFileSync(path)` | main thread | yes, for the whole read | | `fs.readFile` / `fsPromises.readFile` | libuv threadpool | no | | `crypto.pbkdf2Sync(...)` | main thread | yes, for every iteration | | `crypto.pbkdf2(..., callback)` | libuv threadpool | no | | `zlib.gunzipSync(buffer)` | main thread | yes | | `child_process.execSync(cmd)` | a child process | yes, the main thread waits for it | | `JSON.parse(hugeString)` | main thread (V8) | yes, and there is no async version | The last row matters: **not every blocking call has an async twin**. Pure JavaScript work (a large `JSON.parse`, a loop over a million items, a regular expression that backtracks) always runs on the main thread, and the remedies there are bounding the input, chunking the work, or moving it to a worker thread. ## Async does not mean free The threadpool is shared and small. A burst of `crypto.pbkdf2` calls can occupy all of its threads, and then file reads and `dns.lookup` calls queue behind them. The main thread stays responsive, but anything that needs the pool waits. How the pool is sized is covered with libuv's threadpool; for this question, the point is that the async version moves the cost **off the main thread**, not out of existence. ## When Sync is fine Sync APIs are not banned. Reading a config file, a TLS certificate or a template with `fs.readFileSync` **before** `server.listen()` costs nothing at request time, because no client is connected yet. Command-line tools and build scripts are a similar case: a process that serves no other client loses nothing by blocking, which is why the Sync APIs exist at all. The problem is paying the cost **per request** in a server. Node can help you find the ones that slipped into request paths: started with `--trace-sync-io`, it prints a stack trace whenever synchronous I/O is detected after the first turn of the event loop. ## How to answer in an interview 1. Node runs all request handlers on one main thread through the event loop. 2. A `*Sync` call does its I/O or CPU work inside the current callback, so every other connection waits until it returns. 3. The async versions submit the work to libuv's threadpool and return at once; the callback runs later. 4. Sync is acceptable at startup, never in a hot request path; pure CPU work with no async twin needs chunking or a worker thread.

  • Is fs.readFileSync ever acceptable in a Node.js server?
    Yes, at startup before `server.listen()`: loading config, TLS certificates or templates costs nothing per request because no client is connected yet. The problem is a Sync call inside a request path. Starting Node with `--trace-sync-io` prints a stack trace whenever synchronous I/O happens after the first turn of the event loop, which finds the calls that slipped into handlers.
  • If crypto.pbkdf2 runs off the main thread, can it still hurt latency?
    Yes. The libuv threadpool is shared and has four threads by default. A burst of password hashes can occupy all of them, so file reads and `dns.lookup` calls queue behind the hashes. The main thread stays responsive, but anything that needs the pool waits; sizing the pool is a separate decision.

A bank with one teller window: a customer who counts a jar of coins at the window holds up the whole line, while one who hands the jar to the back office (the threadpool) lets the teller call the next customer.

  • Wrapping readFileSync in an async function moves it off the main thread.
  • Node starts a thread per request, so one slow handler only hurts its own client.
  • The threadpool runs your JavaScript callbacks in parallel.
  • Sync APIs must never be used, not even to load config at startup.
  • JSON.parse is non-blocking because the request body arrived asynchronously.
open as a page

In Node.js, what do perf_hooks.monitorEventLoopDelay() and performance.eventLoopUtilization() each measure, and how do you read them to spot a blocked loop?

level: middleimportance: should knowfreq 45%

basics

~20 s

monitorEventLoopDelay() records, in nanoseconds, how long the loop takes to reach a sampling timer, so a max or p99 spike exposes one long callback. eventLoopUtilization() reports the share of time the loop is busy rather than waiting for I/O.

open as a page

When clients upload 50 MB JSON bodies to one route, a Node.js API's p99 latency jumps on every endpoint; how do you confirm the cause and fix it?

level: seniorimportance: should knowfreq 55%

basics

~20 s

The route ends in one JSON.parse of the whole body on the main thread, freezing every other request. Confirm with loop-delay spikes aligned with uploads and a CPU profile; fix with a body cap, NDJSON parsed per line, or a worker thread.

open as a page

In Node.js, a validation regex hangs on one crafted input; why can't a setTimeout or Promise.race deadline stop it, and what can contain it?

level: seniorimportance: should knowfreq 40%

basics

~20 s

The match runs synchronously on the main thread, and Node runs timer callbacks only after the current callback returns, so no deadline fires mid-match. Contain it with an input-length cap, a fixed pattern, or a worker thread you terminate.

open as a page

As a lead, what rule would you set for CPU-heavy work in a Node.js service's request path, and how would you enforce it across the team?

level: principalimportance: should knowfreq 30%

basics

~20 s

Set a main-thread CPU budget per callback, bound every input-dependent cost, and tier heavier work: slice cheap loops, use a worker-thread pool for per-request CPU, move bulk jobs to a separate service. Enforce with lint, --trace-sync-io and loop-delay alerts.

open as a page