skip to content

Scheduling Model

How one thread serves thousands of connections: libuv's phases, the nextTick and promise queues, timers, and what blocks them all. Most Node interviews include an output-ordering question from here.

on this pageshow

explore

questions

20

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

Node.js is called single-threaded, yet a slow file read does not stall other requests; which threads does a Node process actually use?

level: juniorimportance: must knowfreq 75%

basics

~20 s

All JavaScript runs on one main thread that drives the event loop. Socket I/O is watched by the kernel from that thread, while async fs calls, dns.lookup(), zlib and some crypto run on libuv's threadpool, four threads by default.

open as a page

In a CommonJS Node.js script, in what order do process.nextTick, resolved-promise .then and setTimeout(0) callbacks run, and why?

level: middleimportance: must knowfreq 70%

basics

~20 s

Synchronous code first, then every process.nextTick callback, then every promise callback, then the timeout. Node drains its own next tick queue before V8's microtask queue after each operation; setTimeout waits for the loop's timers phase.

open as a page

In Node.js, what are the phases of one libuv event loop iteration, in order, and what kind of callback runs in each?

level: middleimportance: must knowfreq 78%

basics

~10 s

Each Node.js loop iteration runs timers (due setTimeout and setInterval callbacks), pending callbacks (deferred I/O errors), idle and prepare (internal), poll (new I/O events, where the loop waits), check (setImmediate callbacks), then close callbacks.

open as a page

In Node.js, which core APIs run on libuv's threadpool rather than kernel I/O notification, and why do crypto.pbkdf2() and socket reads scale differently?

level: middleimportance: must knowfreq 65%

basics

~20 s

Async fs calls, dns.lookup(), async zlib and crypto such as crypto.pbkdf2() and crypto.scrypt() run on libuv's threadpool, four threads by default, so only four proceed at once. Socket reads use kernel readiness notification and need no pool thread.

open as a page

In Node.js, why can setTimeout(fn, 0) and setImmediate(fn) run in either order from the main module, but not inside an fs callback?

level: middleimportance: must knowfreq 70%

basics

~20 s

setTimeout(fn, 0) is really a 1 ms timer, while setImmediate runs in the check phase right after poll. From the main module, whether 1 ms has elapsed when the loop starts decides the order; inside an I/O callback the immediate always wins.

open as a page

In Node.js, what does process.nextTick() do, and why would a library emit an event from its constructor through it?

level: juniorimportance: should knowfreq 55%

basics

~20 s

process.nextTick() queues a callback to run once the current JavaScript operation completes and before the event loop continues. A constructor that emits through it lets the caller attach listeners first, so the event is not lost.

open as a page

In a Node.js job scheduler that processes records in batches, why is setImmediate the usual way to yield between batches?

level: juniorimportance: should knowfreq 55%

basics

~20 s

setImmediate queues the next batch for the check phase, after poll, so pending socket and file callbacks run between batches. Unlike setTimeout(next, 0) it waits for no 1 ms timer, and unlike process.nextTick it lets the loop poll for I/O first.

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

In Node.js 24, when should code use queueMicrotask() rather than process.nextTick(), and what behaviour changes when you swap one for the other?

level: middleimportance: should knowfreq 40%

basics

~10 s

Prefer queueMicrotask() for new code: Node 24 marks process.nextTick() Legacy. Swapping moves the callback from Node's tick queue, ahead of promise jobs, into V8's microtask queue in line with them, and drops extra-argument passing.

open as a page

In Node.js, what does the libuv poll phase wait on, and how does the loop decide how long to block there?

level: middleimportance: should knowfreq 45%

basics

~20 s

Node's poll phase waits in the kernel's I/O notifier for watched sockets to become ready and for finished threadpool tasks. It blocks until the nearest timer, indefinitely with no timers, and not at all if immediates or closing handles are pending.

open as a page

In Node.js, how do setTimeout and setInterval from node:timers/promises take an AbortSignal, and what happens when the signal aborts?

level: middleimportance: should knowfreq 35%

basics

~20 s

Both take { signal } in their options. setTimeout(delay, value, { signal }) returns a promise; aborting clears the timer and rejects it with an AbortError. setInterval returns an async iterator; aborting stops it, and the for await loop throws that AbortError.

open as a page

In Node.js, what does timeout.unref() do to a setInterval timer, and why would a socket server's idle-client sweeper call it?

level: middleimportance: should knowfreq 45%

basics

~20 s

timeout.unref() stops a timer from keeping the Node.js process alive. It still fires while other work, such as a listening server, keeps the loop running, so a housekeeping sweeper never becomes the reason the process cannot exit.

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

Why would a Node.js service whose retry helper reschedules itself with process.nextTick until it succeeds stop answering health checks and firing timers?

level: seniorimportance: should knowfreq 50%

basics

~20 s

Node drains the whole next tick queue, including callbacks added during the drain, before the event loop continues. A self-rescheduling tick keeps it non-empty, so the loop never reaches timers or the poll phase that serves sockets.

open as a page

Why would a Node.js service hashing passwords with crypto.pbkdf2() see outbound calls by hostname and file reads slow down during login bursts while event loop delay stays low, and how would you fix it?

level: seniorimportance: should knowfreq 55%

basics

~20 s

crypto.pbkdf2(), dns.lookup() and async fs calls share libuv's threadpool, four threads by default, so a burst of hashes makes lookups and reads queue behind them. Size UV_THREADPOOL_SIZE before start, keep dns.lookup() off hot paths, and cap or isolate hashing.

open as a page

After a Node.js socket server calls server.close() and every client disconnects, why might the process never exit, and how do you find the leaked timer?

level: seniorimportance: should knowfreq 40%

basics

~20 s

A ref'd timer nobody cleared, often a setInterval started at boot, keeps the event loop alive after the server has gone. process.getActiveResourcesInfo() then lists 'Timeout'; the fix is clearing that timer where the server closes, not calling process.exit().

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

Why does a Node.js script that logs process.nextTick output before promise callbacks as a .cjs file log it after them as .mjs?

level: seniorimportance: nice to knowfreq 25%

basics

~20 s

An ES module is evaluated while Node is already draining V8's microtask queue, so promise callbacks queued at its top level run before Node returns to its next tick queue. Later timer and I/O callbacks keep the CommonJS order.

open as a page