In Node.js, how do worker_threads, child_process.fork() and the cluster module compare on memory, messaging cost and crash isolation?
answer
- thread in-process versus whole process
- one V8 heap each, either way
- structured clone versus JSON over IPC
- who dies when one unit fails
basics
~20 sworker_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.
solid answer
~40 sA `worker_threads` Worker is a thread in the same process with its own V8 heap and event loop; it talks over `postMessage()` using structured clone, can transfer an `ArrayBuffer` or share a `SharedArrayBuffer`, and its heap is capped with `resourceLimits`. With an `'error'` listener attached, an uncaught exception ends just that worker, but a native crash or process-wide memory exhaustion ends the whole process. `child_process.fork()` starts a separate Node.js process with its own memory and an IPC channel that uses `serialization: 'json'` by default, so a `Buffer` arrives as a plain object unless you opt into `'advanced'`. `cluster` is `fork()` plus a shared server port. Processes cost more memory and messaging, and in return a failure in one stays in that one.
code
javascript · 6 linesimport { Worker } from 'node:worker_threads';
const worker = new Worker('throw new Error("render failed")', { eval: true });
worker.on('error', (err) => console.error('worker failed:', err.message));
worker.on('exit', (code) => console.log('worker exit code', code));
setTimeout(() => console.log('main thread still serving'), 100);go deeper
Recall that worker threads live inside one process while fork() and cluster start separate processes, and that processes never share JavaScript memory.
Explain the messaging difference, structured clone and transfer versus JSON over IPC, and exactly which failures stay contained in a worker and which reach the whole process.
Use the isolation boundary in production decisions: per-worker resourceLimits, error and exit handlers on every unit, and processes for code that may crash natively.
Weigh isolation against memory and messaging cost when setting a team default, and know when an orchestrator's replicas make in-process mechanisms unnecessary.
## Three ways to use more than one core Node.js ships three built-in modules for running work beside the main thread, and they differ in what they share: - `node:worker_threads` starts **threads inside the current process**. Each `Worker` gets its own JavaScript engine instance, heap and event loop, but shares the process's address space, file descriptors and libuv threadpool. - `child_process.fork()` starts **a new Node.js process** running a module, with an IPC channel back to the parent. - `node:cluster` is built on `fork()`: it starts several **copies of a server process** that share one listening port, and on every platform except Windows the primary hands out connections round-robin. The cluster documentation draws the line itself: when process isolation is not needed, use `worker_threads` instead. ## Memory A worker thread is lighter than a process because it lives inside one, but it is not free: each carries its own V8 heap. A forked child or a cluster worker is a complete Node.js process; the `child_process` docs note that each has its own memory and its own V8 instance, and that spawning a large number of them is not recommended. What can be shared differs too. Only worker threads can share memory: an `ArrayBuffer` can be **transferred** (moved, not copied) through a `transferList`, and a `SharedArrayBuffer` can be read by several threads at once. Processes never share a heap, so a module-level `Map` used as a cache exists once per cluster worker, which is why in-memory sessions break under `cluster`. Heap limits are set differently as well. A worker's heap is capped by its `resourceLimits` option (`maxOldGenerationSizeMb` among others); its `execArgv` does not accept V8 options such as `--max-old-space-size`. A forked child is its own process, so its `execArgv` can carry that flag. ## Messaging cost | | worker_threads | fork() and cluster | |---|---|---| | Channel | `postMessage()` and `'message'` inside one process | `send()` and `'message'` over an OS IPC channel | | Default encoding | HTML structured clone | `serialization: 'json'` | | Avoiding the copy | `transferList`, `SharedArrayBuffer` | None for data; server and socket handles can be passed | | Types preserved | `Map`, `Set`, typed arrays, circular references (a `Buffer` arrives as a plain `Uint8Array`) | JSON types only, unless `serialization: 'advanced'` | Every message is serialized on one side and rebuilt on the other. Inside one process that is cheaper than crossing a process boundary, and large binary data can skip it entirely by transfer. With `fork()`, a `Buffer` sent under the default `'json'` mode arrives as a plain object rather than a `Buffer`; `serialization: 'advanced'` uses the `node:v8` serializer, which keeps `Buffer`, `Map`, `Set`, `BigInt` and `Error`, though the docs warn it is not a full superset of JSON and is not always as fast. The rule of thumb: offloading pays when the computation takes much longer than moving its input and output. ## Crash isolation Isolation is what processes buy with their memory. 1. **An uncaught exception inside a worker thread** stops that worker. The parent's `Worker` object emits `'error'` and then `'exit'`, and the process keeps serving. If the parent has no `'error'` listener, the error is thrown in the parent, like any unhandled `'error'` event on an `EventEmitter`. 2. **`process.exit()` inside a worker** ends only that thread. 3. **A worker reaching its `resourceLimits`** is terminated with `ERR_WORKER_OUT_OF_MEMORY`. Those limits cover only the JavaScript heap, and the docs warn that the process may still abort on a global out-of-memory condition. 4. **A native crash or process-wide memory exhaustion** takes every thread with it, because they are one process. 5. **A forked child or cluster worker** that crashes takes down only its own process. The parent sees `'exit'` with a code or signal and decides what to do; Node does not restart it for you. ## Choosing quickly - CPU-heavy JavaScript on trusted input that needs cheap data passing: a `worker_threads` pool. - Code that might crash the process, needs its own heap cap, or must be killed outright on a timeout: a forked child. - A network server that should use every core on a host with no other supervisor: `cluster`. - The same server under an orchestrator: usually more replicas instead.
- Why does a Buffer sent through child_process.fork() not arrive as a Buffer, and how do you change that?`fork()` defaults to `serialization: 'json'`, so the message is JSON-encoded and a `Buffer` arrives as its JSON form, a plain object with `type` and `data` fields. Passing `serialization: 'advanced'` switches to the `node:v8` serializer, based on structured clone, which keeps `Buffer`, `Map`, `Set`, `BigInt` and `Error`. The docs warn it is not a full superset of JSON and may not match JSON's speed.
- Do worker threads each get their own libuv threadpool?No. libuv's threadpool is global to the process and shared by every event loop in it, so all worker threads compete for the same four threads (by default) when they call `fs`, `dns.lookup()`, async crypto or `zlib`. Cluster workers and forked children are separate processes, so each gets its own pool.
- What happens when a Worker throws and the parent never attached an 'error' listener?The `Worker` object is an `EventEmitter`, and emitting `'error'` with no listener throws the error in the parent thread. That becomes an uncaught exception on the main thread and, under the default policy, crashes the process, the opposite of the isolation you wanted. Always attach `'error'` and `'exit'` handlers to every Worker.
Worker threads are flatmates sharing one flat: passing a note across the table is quick, and one flatmate walking out leaves the rest, but a fire or a burst pipe hits everyone. Forked processes are neighbours in separate flats: notes go through the post, slower and copied, but a fire next door stays next door.
- Worker threads see the main thread's objects directly, so nothing is copied.
- A thrown exception in a worker thread always brings down the whole process.
- Cluster workers share one heap, so an in-memory cache is visible to all of them.
- fork() IPC preserves Buffers and Maps by default.
- Each worker thread gets its own libuv threadpool of four threads.
- Passing --max-old-space-size in a Worker's execArgv caps that worker's heap.