skip to content

Choosing a Parallelism Model

The decision question: worker thread, forked process, cluster, or more containers. Asked to hear you weigh CPU-bound vs I/O-bound work, memory per isolate, messaging cost and who already supervises.

on this pageshow

explore

questions

5

In Node.js, which kinds of work belong in worker_threads, and why do I/O-bound endpoints gain little from moving there?

level: juniorimportance: must knowfreq 72%

answer

  1. one JavaScript thread per event loop
  2. waiting is handed to the OS
  3. a busy callback delays every request
  4. async crypto and zlib use libuv's pool

basics

~20 s

Worker threads suit CPU-bound JavaScript, such as a long PDF layout or a heavy calculation, that would otherwise freeze the event loop. I/O-bound work already waits outside the JavaScript thread, so a worker adds startup and copying cost without adding throughput.

solid answer

~40 s

Node runs your JavaScript on one main thread, and the event loop can serve the next request only when the current callback returns. I/O-bound work (a database query, an HTTP call, a file read) spends its time waiting, and Node hands that waiting to the operating system or to libuv's threadpool, so the main thread stays free; the `worker_threads` docs say Node's built-in async I/O is more efficient than workers can be. CPU-bound JavaScript is different: a two-second PDF layout on the main thread can stall every other request on that process for up to two seconds. That work belongs in a small, long-lived `worker_threads` pool. One trap: async `crypto.scrypt()`, `crypto.pbkdf2()` and `zlib` calls already run on libuv's threadpool and do not block the loop; their `*Sync` forms do.

go deeper

for a junior

Recall the split: CPU-bound JavaScript blocks the single event loop and belongs in worker threads, while I/O waits already happen off the JavaScript thread.

for a middle

Explain libuv's threadpool: which built-ins use it, its default size of four, and why the async crypto and zlib calls already avoid blocking the loop.

for a senior

Show you measure before offloading, distinguishing a CPU-pinned process from one waiting on a slow dependency, and that you bound any worker pool to the cores you have.

for a principal

Frame the team rule: which request-path work must be async or offloaded, and when CPU-heavy features stop belonging in an I/O-oriented Node service at all.

## One thread runs your JavaScript A Node.js process runs application JavaScript on a single main thread driven by the **event loop**. The loop picks up an event (a request arriving, a query result coming back, a timer firing), runs its callback to completion, then moves on to the next one. While a callback is running, no other JavaScript in that process can run. That single fact decides where work should live. Node does use other threads. **libuv**, the library under the event loop, keeps a fixed-size **threadpool** (four threads unless `UV_THREADPOOL_SIZE` is set at startup) for operations the operating system cannot perform asynchronously. According to the Node CLI documentation, that pool runs: - all `fs` APIs except the file watchers and the explicitly synchronous ones; - asynchronous crypto such as `crypto.pbkdf2()`, `crypto.scrypt()`, `crypto.randomBytes()` and `crypto.generateKeyPair()`; - `dns.lookup()`; - all `zlib` APIs except the explicitly synchronous ones. Network sockets do not use the pool at all: the operating system tells libuv when a socket is ready. ## I/O-bound work: the waiting already happens elsewhere An **I/O-bound** endpoint spends most of its time waiting: for a database, for another HTTP service, for the disk. In Node that waiting costs the main thread almost nothing. The callback that sends the query returns, the loop serves other requests, and a later callback handles the result. Moving such an endpoint into a `worker_threads` Worker does not shorten the wait. The worker waits for the same database in the same way, and you have added the cost of starting a second JavaScript engine instance and of copying the request and the result between threads. The `node:worker_threads` documentation says so directly: workers "do not help much with I/O-intensive work", and Node's built-in asynchronous I/O operations are more efficient than workers can be. ## CPU-bound work: what worker_threads is for A **CPU-bound** task is one whose time goes into computing: laying out a large PDF in JavaScript, parsing a huge document, running an image filter written in JavaScript, a heavy scoring calculation. On the main thread, a two-second computation means every other request on that process waits up to two seconds. Health checks time out and latency on unrelated endpoints climbs. `worker_threads` runs JavaScript on additional threads inside the same process, each with its own engine instance and event loop. Move the computation there and the main thread keeps serving I/O while another core does the work. Because creating a worker has a real cost, the docs recommend a **pool** of long-lived workers rather than one worker per task. ## Telling the two apart | Work | Where its time goes | Does a worker help? | |---|---|---| | Awaiting a database query | The database and the network | No | | `fetch()` to another service | The remote service | No | | Async `fs.readFile()` | libuv threadpool and the disk | No | | Async `crypto.scrypt()` | libuv threadpool | Rarely; it already runs off the main thread | | `crypto.scryptSync()` | Main thread | Yes, or switch to the async form | | PDF layout in JavaScript | Main thread | Yes | A practical way to decide: 1. Measure first. A process pinned near one full core with rising latency is CPU-bound; low CPU with rising latency points at a slow dependency or an exhausted connection pool. 2. Look for synchronous calls (`*Sync` APIs, a very large `JSON.parse`) on the request path; the lesson on blocking the event loop covers how to find them. 3. Prefer the asynchronous form of a built-in, when one exists, before reaching for a worker. 4. Move what is left, real JavaScript computation, into a bounded worker pool. ## Common mistakes - **Wrapping database calls in workers.** It adds a thread, an engine instance and two copies per request, and the query is no faster. - **Treating the async crypto calls as blocking.** `crypto.scrypt()` does not freeze the loop; `crypto.scryptSync()` does. - **Forgetting the threadpool is shared and small.** Four slow hashes can make file reads and `dns.lookup()` calls wait behind them. - **One worker per request.** Startup cost can exceed the work saved, and more busy workers than cores simply take turns. - **Assuming the work disappears.** A worker moves CPU time to another core; it does not reduce it, so a saturated machine stays saturated.

  • If async crypto.scrypt() already runs off the main thread, why can a login endpoint still slow down under load?
    libuv's threadpool is fixed in size, four threads by default, and is shared by `fs` calls, `dns.lookup()`, async crypto and `zlib`. Four concurrent hashes occupy it, so file reads and DNS lookups queue behind them. You can raise `UV_THREADPOOL_SIZE` at startup (setting it from inside the process is not guaranteed to work), or give hashing a dedicated worker pool. Worker threads in the same process share that one threadpool.
  • Does running a slow database query inside a worker thread make the query faster?
    No. The time is spent in the database and on the network, and a worker waits for it exactly as the main thread would. You add a second engine instance, the cost of starting or reserving a worker, and a structured-clone copy of the result back to the main thread. The fix is in the query, its indexes or the connection pool size.

  • Worker threads speed up every Node.js endpoint, database calls included.
  • Async crypto.scrypt() blocks the event loop just like crypto.scryptSync().
  • Node.js is single-threaded, so the process has no other threads at all.
  • Starting a new Worker per request is free because threads are cheap.
  • Moving work into a worker removes its CPU cost from the machine.
open as a page

In Node.js, how do worker_threads, child_process.fork() and the cluster module compare on memory, messaging cost and crash isolation?

level: middleimportance: must knowfreq 75%

basics

~20 s

worker_threads run inside one process: cheaper messaging and shareable memory, but a native crash or process-wide out-of-memory kills them all. fork() and cluster start full processes: more memory and JSON IPC by default, but a crash ends only that process.

open as a page

When a Node.js service already runs as several container replicas behind a load balancer, why is adding the cluster module inside each container often redundant?

level: seniorimportance: should knowfreq 50%

basics

~20 s

The orchestrator already runs, restarts and load-balances replicas. Cluster repeats that inside each container, hiding worker crashes from health checks, sizing from a CPU count the container may not get, and packing several V8 heaps into one memory limit.

open as a page

A Node.js API renders PDF exports in a worker_threads pool, yet one large export sometimes kills the whole pod and its unrelated requests. Why, and what would you change?

level: seniorimportance: should knowfreq 45%

basics

~20 s

Worker threads share one process and one container memory limit. resourceLimits caps only each worker's JavaScript heap, not Buffers or native memory, so concurrent large exports can exhaust the process. Cap concurrency and budget memory, or render in a separate process.

open as a page

Your Node.js API mixes I/O endpoints with a CPU-heavy PDF export and runs on Kubernetes; how would you choose between worker_threads, child processes, cluster and more replicas?

level: principalimportance: should knowfreq 40%

basics

~20 s

Scale the I/O endpoints as one Node.js process per container plus replicas. Run the export where its failures may land: a bounded worker_threads pool if it can share fate, forked children for isolation, or a queue-fed deployment for independent scaling.

open as a page