A Worker Thread runs within the same process, sharing the V8 binary but with its own JS heap, communicating cheaply and able to share memory via SharedArrayBuffer. A Child Process is a fully separate OS process with isolated memory, higher creation overhead, but stronger isolation -- a crash can't corrupt the parent's memory.
const worker = new Worker('./cpu-task.js'); // lighter weight, shared process
const child = fork('./cpu-task.js'); // heavier, fully isolated process
Real-world example
A sandbox running untrusted user-submitted JavaScript deliberately uses child_process.fork() with resource limits rather than a worker thread, since stronger process isolation prevents a malicious snippet from corrupting the parent's memory.
Likely follow-ups
In what scenario is the stronger isolation of a child process worth the higher overhead?
How does IPC between a parent and forked child compare to postMessage() between worker threads?
workerData is a value passed once, synchronously, at Worker construction time, immediately available inside the worker without waiting for an initial message -- useful for initial configuration, whereas postMessage() is used for ongoing communication after the worker is already running.
A batch-processing worker receives its input file path via workerData at creation, needing no initial handshake before beginning work, while using postMessage() afterward for progress updates.
Likely follow-ups
Why can't workerData be used for ongoing communication after the worker has started?
What happens if workerData contains a value that can't be structured-cloned, like a function?
worker.terminate() forcibly and immediately stops execution regardless of current work, abandoning in-progress work -- appropriate for canceling unneeded tasks. Letting a worker exit naturally allows pending operations to complete first, providing a cleaner shutdown when its own work is genuinely finished.
setTimeout(() => worker.terminate(), 30000); // cancel if it takes too long
worker.on('exit', (code) => console.log(`Worker exited with code ${code}`));
Real-world example
A request-timeout mechanism calls worker.terminate() on a task running over 30 seconds, ensuring a stuck task doesn't tie up pool capacity indefinitely, at the cost of losing partial progress.
Likely follow-ups
What cleanup responsibilities does code inside a worker have before being forcibly terminated?
How would you implement graceful cancellation where the worker checks a flag periodically rather than being killed?
A script using the cluster module is re-executed by every forked worker as well as the primary -- code branches on cluster.isPrimary (true only in the original process), typically containing only forking/management logic, while cluster.isWorker guards the actual application logic, ensuring the primary never also runs a full server copy.
if (cluster.isPrimary) {
for (let i = 0; i < 4; i++) cluster.fork();
cluster.on('exit', () => cluster.fork());
} else {
require('./app').listen(3000);
}
Real-world example
A misconfigured cluster setup that started the server inside both branches got an 'address already in use' error, resolved by keeping server-starting code only in the isWorker branch.
Likely follow-ups
Why does having the primary only manage workers (rather than also serving requests) improve reliability?
How many workers does the primary count as when calculating total request capacity?
os.cpus() returns an array with one entry per logical CPU core available to the process -- its length is commonly used to decide how many workers to fork, letting code automatically scale its worker count to whatever hardware it runs on rather than hardcoding a fixed number.
console.log(os.cpus().length);
for (let i = 0; i < os.cpus().length; i++) cluster.fork();
Real-world example
A startup script forks exactly as many workers as os.cpus().length reports, meaning the same code correctly utilizes all cores whether on a 2-core dev VM or a 32-core production server.
Likely follow-ups
How does os.cpus().length behave inside a container with CPU limits below the host's core count?
What's a scenario where you'd deliberately fork fewer workers than the available core count?
Advanced Node.jsDocker & Containerization for Node.js