skip to content

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%

answer

  1. two workloads, two shapes
  2. where does the export run
  3. who already supervises the processes
  4. isolation bought with memory
  5. evidence that triggers a split

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.

solid answer

~40 s

I would split by workload. The I/O endpoints need concurrency, which Node gives per process, so I would run one process per container and scale replicas, letting the orchestrator supervise rather than adding `cluster`. The export needs CPU and memory away from the event loop. A bounded `worker_threads` pool is cheapest and can transfer buffers, but it shares the process's memory and fate. Forked children cost a full Node.js instance each and JSON IPC by default, and buy crash isolation, per-child heap caps and hard kills. A separate deployment behind a queue costs an async job flow and more operations, and buys independent scaling and limits. I would start with the pool plus `resourceLimits` and a concurrency cap, and agree which measurements, such as tail latency or memory per replica, trigger the move.

go deeper

for a junior

Recall the basic map: worker threads for CPU-heavy JavaScript, child processes for isolation, cluster or replicas for spreading requests across cores.

for a middle

Explain what each option costs in memory and messaging, from structured clone in one process to JSON over IPC between processes.

for a senior

Show how each choice fails in production, from shared-fate memory exhaustion to hidden cluster crashes, and the bounds you would set on pools and heaps.

for a principal

Own the trade: separate the workloads, pick an isolation boundary on evidence, set team rules for pools, timeouts and state, and name what would trigger a redesign.

## Start from the two workloads, not the APIs The service runs two very different shapes of work: - **I/O endpoints**: short callbacks between waits on a database or another service. One Node.js process handles many of these concurrently, and throughput grows by adding processes. - **PDF export**: seconds of CPU per request, large memory per job, often a third-party renderer. It blocks whichever event loop runs it, and it fails and scales differently from the rest. Every option is an answer to two questions: where does the export run, and who supervises the processes? ## The options for the export | Option | Isolation | Memory cost | Messaging | Operational cost | |---|---|---|---|---| | `worker_threads` pool in each API process | Thread: shares the process's memory and fate | One V8 heap per worker | `postMessage()` structured clone; buffers can be transferred | Lowest: same deployment | | Forked child pool (`child_process.fork()`) | Process: a crash or OOM ends that child | A full Node.js process per child | IPC with `'json'` serialization by default | Medium: you write the pool, timeouts and restarts | | Separate export deployment behind a queue | Separate pods and limits | Sized on its own | Queue messages, results in object storage | Highest: a queue, job status, another service | **In-process worker pool.** Cheapest to build, lowest latency, and the only option that can hand over a large `ArrayBuffer` without copying it. The costs are shared fate and shared capacity: an export that exhausts process memory, or crashes in native code, takes down the I/O endpoints in the same process, and export CPU competes with request handling for the pod's CPU. `resourceLimits` caps each worker's JavaScript heap, not its buffers. **Forked children.** A child can get its own heap cap through `execArgv`, can be stopped on a timeout with `subprocess.kill()`, and dies alone. You pay a full Node.js instance per child and serialization over IPC, and you write the supervision. It still shares the container's CPU and memory limits. **Separate deployment.** Exports become jobs. The API enqueues and returns a job ID; export workers scale on their own signal, get their own memory profile and deploy on their own cadence. You pay with an asynchronous user experience and one more thing to run. ## The I/O side: who supervises On Kubernetes the orchestrator already runs replicas, restarts them and spreads traffic, so the I/O path is usually **one Node.js process per container, scaled by replicas**. Adding `cluster` inside the pod duplicates that supervision and hides worker crashes from health checks; keep it for hosts with no other supervisor. Whatever runs the export, size pools from configuration rather than from `os.availableParallelism()`, which libuv documents in terms of the affinity mask, with no mention of a CPU quota. ## How to decide 1. **Measure the export.** Duration, peak memory including buffers, and frequency. A rare sub-second export is a different problem from a steady stream of thirty-second ones. 2. **Write down the I/O latency target.** If export runs move tail latency on unrelated endpoints even with a bounded pool, sharing the process has stopped being acceptable. 3. **Rate the input and the renderer.** Untrusted documents and native renderers favour a process boundary, because their worst failure is a crash or runaway memory. 4. **Compare scaling curves.** If export demand would force every API replica to carry export-sized memory, separate the workloads so each scales on its own signal. 5. **Price the team's capacity.** A queue and a second deployment pay off only if someone owns them. A common path is to start with a bounded in-process pool, `resourceLimits` and a concurrency cap, and to agree in advance which measurements trigger the move to a separate deployment. ## Rules worth setting for the team - No CPU-heavy synchronous work on a request path beyond a budget derived from the latency target. - Pool sizes, heap caps and replica counts come from configuration. - Every `Worker` and child process gets `'error'` and `'exit'` handling and a job timeout. - State that must agree across instances (sessions, rate-limit counters, shared caches) lives outside the process, because replicas, cluster workers and children never share a heap. There is no single right answer here. What an interviewer listens for is that you separate the workloads, name the isolation boundary you chose and its cost, and state what evidence would change the choice.

  • When would you choose forked children over worker_threads for the export despite the memory cost?
    When the export's worst failure must not reach API traffic: untrusted input, a native renderer that might crash, or jobs that need a hard kill. A child process has its own heap cap through `execArgv`, can be ended with `subprocess.kill()`, and a crash ends only that child. Worker threads cannot contain a native crash or process-wide memory exhaustion.
  • How do you size an export worker pool inside a pod with a CPU limit?
    From configuration matched to the CPU allocation, leaving room for the main thread and libuv's threadpool, which all worker threads share. CPU-bound workers beyond the CPUs you may use only take turns, adding memory and scheduling cost with no gain. libuv documents `os.availableParallelism()` in terms of the affinity mask, with no mention of CPU quotas, so it is not a dependable source inside a quota.
  • What evidence would make you move the export into its own deployment?
    Export runs that still move tail latency on unrelated endpoints with a bounded pool; API replicas that must carry export-sized memory only for occasional jobs; incidents where an export took down API pods; or an export that needs a different scaling signal or release cadence from the API.

  • Worker threads give the export the same crash isolation as a separate process.
  • Running cluster inside each pod is always the way to use every core on Kubernetes.
  • More replicas alone fix a CPU-heavy export blocking each replica's event loop.
  • A queue-fed export service is always the right first step, whatever the volume.
  • Pool size should simply equal os.availableParallelism() in every container.