Skip to content

child_process in Node.js: Scaling Without Illusions

Node.js runs on a single event loop – CPU work blocks everything. This post shows when fork, cluster, and worker_threads really help and when they are just overhead.

≈ 12 min readSoftwareNode.jsBackend

Non-blocking doesn’t mean parallel. Node.js accepts thousands of concurrent network connections without breaking a sweat – but that doesn’t mean it spreads computational work across multiple cores. The two properties get lumped together all the time, and this confusion is what produces the endpoint that freezes the entire application under load without anyone understanding why.

The difference lies in what the work consists of: waiting or computing. Node handles waiting on its own. Computing, it doesn’t. That is what child_process, cluster, and worker_threads are for – three tools with similar names, very different costs, and one shared pattern of misuse.

One event loop, one thread, one bottleneck#

Let’s start at the root. A Node.js process has one event loop, and JavaScript runs in that process on exactly one thread. That isn’t a bug, that is the design principle. As long as the work consists of waiting – for a database response, for a file, for an HTTP response – the event loop can take care of other things in the meantime. The actual input and output happens in the background via libuv, and your code only gets the finished callback at the end. If you want to understand how this machinery works in detail, my Node.js training on event loop architecture has the full version.

The problem begins as soon as the work doesn’t consist of waiting but of computing. Scaling an image, parsing and transforming a large JSON document, verifying a password with an expensive hash function, a cryptographic operation, a synchronous loop over millions of entries. This work happens on the same single thread. And as long as it runs, the event loop can’t do anything else – no accepting new requests, no firing timers, no processing callbacks. Everything stops.

The following code looks harmless and still slips through code reviews regularly:

const http = require('node:http');

function fibonacci(n) {
  if (n < 2) return n;
  return fibonacci(n - 1) + fibonacci(n - 2);
}

http
  .createServer((req, res) => {
    if (req.url === '/compute') {
      const result = fibonacci(45); // blocks the event loop for seconds
      res.end(`result: ${result}`);
    } else {
      res.end('ok');
    }
  })
  .listen(3000);

As long as fibonacci(45) is computing, not a single other client gets a response – not even on the harmless / path, which would normally finish in microseconds. A single slow request brings down the whole server. That is exactly what the official docs mean by “Don’t Block the Event Loop.”

Four tools, four jobs#

Node.js offers several tools for this problem, and they get mixed up regularly. It pays to keep them cleanly apart:

  • child_process starts a completely separate operating system process with its own V8, its own heap, and its own event loop. Communication runs over pipes or – with fork – over an IPC channel via message passing. No shared memory.
  • cluster starts several Node instances behind a shared port and distributes incoming connections among them. Internally, it builds on child_process.fork() and serves request throughput.
  • worker_threads starts another thread within the same process. Each worker has its own V8 isolate and its own event loop, but can optionally share real memory with the main thread. Meant for CPU work.
  • The regular asynchronous APIs are for everything that is I/O – and that needs none of the three options above.

I emphasize this last point deliberately, because that is where the most common reasoning error lurks. But one thing at a time.

child_process: spawn, exec, execFile, fork#

The node:child_process module offers four ways to start a child process, and the differences matter in practice.

spawn(command, args, options) starts a program and gives you its stdout and stderr as streams. It buffers nothing and doesn’t start a shell by default. That is the right choice when the output can be large or you want to process it as a stream.

exec(command, options, callback) starts a real shell and buffers the entire output in memory before the callback is called. That is convenient for short commands but has two catches: the default for maxBuffer is 1 MB – if the output gets larger, Node kills the process with ERR_CHILD_PROCESS_STDIO_MAXBUFFER. And because a shell is involved, exec with unchecked input is an invitation to shell injection.

execFile(file, args, options, callback) behaves like exec but doesn’t start a shell. That is safer and more efficient when you are only calling a specific program with arguments anyway.

const { execFile } = require('node:child_process');

execFile('git', ['rev-parse', 'HEAD'], (err, stdout) => {
  if (err) throw err;
  console.log('current commit:', stdout.trim());
});

The choice fits into three lines: short output from a specific program – execFile. Large or streaming output – spawn. exec with a shell only when you really need the shell features (pipes, globbing) and you control the input.

The most interesting of the four is fork. It is a special case of spawn that specifically starts a new Node instance – via process.execPath, the same Node binary – and automatically sets up an IPC channel. This lets you exchange messages between the parent and child process without handling pipes and serialization by hand.

The main example: heavy computation in a fork#

Let’s take the blocking Fibonacci example from above and move the computation into a child process. The parent process stays responsive because the CPU load lands in an entirely different OS process.

First the worker in worker.js:

// worker.js
function fibonacci(n) {
  if (n < 2) return n;
  return fibonacci(n - 1) + fibonacci(n - 2);
}

process.on('message', ({ n }) => {
  const result = fibonacci(n);
  process.send({ n, result });
});

And the parent process that starts it and hands it work:

// main.js
const { fork } = require('node:child_process');
const http = require('node:http');

const child = fork('./worker.js');

const pending = new Map();
child.on('message', ({ n, result }) => {
  const res = pending.get(n);
  if (res) {
    res.end(`fibonacci(${n}) = ${result}`);
    pending.delete(n);
  }
});

http
  .createServer((req, res) => {
    if (req.url === '/compute') {
      pending.set(45, res);
      child.send({ n: 45 }); // heavy work happens elsewhere
    } else {
      res.end('ok'); // stays responsive during the computation
    }
  })
  .listen(3000);

The crucial point here: the / path now responds immediately, even while /compute is still computing. The main process’s event loop is free, because the expensive loop runs in a separate process with its own V8 heap. Communication between the two goes exclusively through messages – here via child.send() and the message event.

This is worth a second look: send is not shared memory. The message is serialized and rebuilt as a copy in the child process. The default is serialization: 'json', and JSON loses types – a Date becomes a string, undefined disappears, BigInt doesn’t work at all. If you want to send structured values over IPC, you can set serialization: 'advanced', which uses the structured clone algorithm and preserves considerably more types.

The following flow shows the principle as a sequence:

sequenceDiagram
    participant Client
    participant Main as Main process<br/>(event loop free)
    participant Child as Forked process<br/>(own V8 heap)
    Client->>Main: GET /compute
    Main->>Child: send({ n: 45 })
    Note over Main: Event loop stays free<br/>for other requests
    Child->>Child: heavy computation
    Child-->>Main: message({ result })
    Main-->>Client: HTTP 200 result

worker_threads: the same goal, shared memory#

A fork is robust but expensive. Every child process brings a complete V8 and its own heap – that costs memory and startup time. If you want to parallelize CPU work and don’t need the full isolation of a separate process, worker_threads are the lighter alternative. They have been stable since Node 12 and, in 2023, have long been the production standard.

A worker runs in the same process but in its own thread with its own V8 isolate and its own event loop. The same computation looks like this:

// task.js
const { parentPort, workerData } = require('node:worker_threads');

function fibonacci(n) {
  if (n < 2) return n;
  return fibonacci(n - 1) + fibonacci(n - 2);
}

parentPort.postMessage(fibonacci(workerData.n));
// main-threads.js
const { Worker } = require('node:worker_threads');

function computeFib(n) {
  return new Promise((resolve, reject) => {
    const worker = new Worker('./task.js', { workerData: { n } });
    worker.on('message', resolve);
    worker.on('error', reject);
  });
}

computeFib(45).then((result) => {
  console.log('fibonacci(45) =', result);
});

The big difference from fork: worker threads can share real memory. A SharedArrayBuffer is the same memory region for the main thread and the worker thread – no copy, no serialization. Alternatively, you can pass an ArrayBuffer via transferList, which is a zero-copy transfer: the buffer moves to the receiver and is unusable on the sender afterward. For large amounts of data between threads, that is a real advantage that a separate process, by its nature, can’t offer.

The pitfall that does the most damage#

The mistake that burns the most time in real codebases is this one: trying to “speed up” I/O-bound work with worker_threads or cluster. I have run into it in several projects, each time with the same justification.

The motivation is understandable. Someone has heard that workers parallelize, sees an endpoint that makes many database queries or HTTP calls, and puts the whole thing into a worker pool. The result is regularly slower than before. The reason: network and file I/O in Node.js is already asynchronous and doesn’t block the event loop at all. There is nothing to offload. The docs say it unambiguously: worker threads don’t help much with I/O-intensive work. What you get is the overhead of thread creation and message passing – with no gain whatsoever.

So the rule is simple and still gets violated all the time:

  • Is the work I/O-bound – database, network, file system? Then stick with the regular asynchronous APIs. Workers and processes bring nothing here except complexity.
  • Is the work CPU-bound – hashing, image processing, compression, crypto, large synchronous transformations? Only then are worker_threads or child_process the right tool.

A second pitfall sits right next to it: creating a worker or process per task. Creation is expensive – V8 has to be initialized, memory gets allocated. If you start a fresh worker on every request, you often make things slower, not faster. The answer is a worker pool: a fixed number of reusable workers that process tasks from a queue. The same consideration applies to fork – keep the child processes alive and send them work one after another instead of restarting them constantly.

cluster: throughput, not relief#

That leaves cluster, and here, too, there is a persistent misunderstanding. cluster doesn’t solve the problem of the single blocking computation. It solves the problem of throughput: a single Node process uses only one core, but your machine has several. cluster starts one Node instance per core, all of them listen behind the same port, and the primary distributes incoming connections round-robin.

const cluster = require('node:cluster');
const http = require('node:http');
const os = require('node:os');

if (cluster.isPrimary) {
  for (let i = 0; i < os.cpus().length; i++) {
    cluster.fork();
  }
  cluster.on('exit', (worker) => {
    console.log(`worker ${worker.process.pid} died, restarting`);
    cluster.fork();
  });
} else {
  http
    .createServer((req, res) => res.end(`handled by pid ${process.pid}`))
    .listen(3000);
}

This doubles or quadruples the throughput of concurrent requests. But if a single request triggers a synchronous computation that takes seconds, it still blocks the worker handling it. cluster only turns many slow requests into many parallel ones if each lands on a different worker – and the limited number of workers runs out at some point. For the actual CPU problem per request, you still need worker_threads or offloaded processes. By the way, cluster.isPrimary has been the preferred name since Node 16; isMaster still exists but is considered deprecated.

What to use when: the decision at a glance#

The decision boils down to a few questions:

flowchart TD
    A[Classify the work] --> B{I/O-bound?}
    B -->|Yes| C[Regular async APIs<br/>no workers needed]
    B -->|No, CPU-bound| D{Shared memory<br/>or lightweight?}
    D -->|Yes| E[worker_threads<br/>SharedArrayBuffer possible]
    D -->|No, isolation<br/>or external script| F[child_process / fork<br/>own process + IPC]
    A --> G{Just more throughput<br/>on all cores?}
    G -->|Yes| H[cluster<br/>multiple instances, one port]

The three tools answer different questions – they don’t compete for the same one:

  • child_process / fork gives you a separate process with its own heap and communication via copied IPC messages – maximum isolation, ideal for external programs or standalone Node scripts.
  • cluster gives you several Node instances behind one port for more request throughput – it scales instances, not individual computations.
  • worker_threads gives you threads in the same process with optionally shared memory – the right choice for parallelizing CPU work without spinning up a whole process.

Conclusion#

The illusion this post argues against is the most comfortable one: that Node.js somehow spreads CPU work across all cores on its own. It doesn’t. One process, one event loop, one JS thread – and every synchronous computational load blocks everything else. Once you accept that, the right decisions almost make themselves: I/O doesn’t need workers, just the asynchronous APIs that Node already ships with. Only CPU work justifies heavier artillery – worker_threads for in-process parallelism with shared memory, fork for isolated processes, cluster for pure throughput.

The most expensive mistake is not choosing the wrong tool. It is reaching for a tool where there is no problem at all – “speeding up” I/O with workers and buying complexity and overhead in exchange, without gaining a single millisecond. First measure whether you are CPU-bound at all. If you are, you now know where the work belongs. If you push large amounts of data through your application anyway, my post on Node.js streams and backpressure is the right complement – because often the best scaling is the kind where you never load the data completely into memory in the first place.

Further reading#

Revised and translated from German with the help of AI.