Back to 20 Concepts
concurrencyExpert

Clustering (Multi-Process) vs Worker Threads

Clustering creates multiple isolated OS processes sharing the same server port (multi-core horizontal scaling). Worker Threads run multiple V8 instances sharing the same process memory (via SharedArrayBuffer).

Intuitive Mental Model

Franchise Branches vs Kitchen Assistants: Clustering is opening 4 separate restaurant branches (independent processes, no shared memory); Worker Threads is hiring 4 sous-chefs in the same kitchen sharing the same pantry.

Node.js ESM / CJS ImplementationNode.js v22 LTS
// 1. CLUSTERING (One process per CPU core):
import cluster from 'cluster';
import http from 'http';
import os from 'os';

if (cluster.isPrimary) {
  const cpus = os.cpus().length;
  for (let i = 0; i < cpus; i++) cluster.fork(); // Spawn worker processes
} else {
  http.createServer((req, res) => res.end('Handled by worker ' + process.pid)).listen(3000);
}

// 2. WORKER THREADS (Shared memory CPU parallelization):
import { Worker, isMainThread, parentPort } from 'worker_threads';
if (isMainThread) {
  const worker = new Worker(new URL(import.meta.url));
  worker.on('message', (msg) => console.log('Result from worker:', msg));
} else {
  parentPort?.postMessage('Fibonacci calculated in parallel thread');
}

Key Architectural Takeaways

  • Cluster: Best for scaling I/O-bound web servers across multiple CPU cores without shared state.
  • Worker Threads: Best for CPU-intensive tasks (image processing, encryption, machine learning) with fast SharedArrayBuffer data sharing.
  • Processes in a cluster do not share memory; session state must be stored in Redis.
Common Production Mistake

Storing in-memory global variables in a Clustered Node app, causing inconsistent state across different worker processes.

Recommended Solution

Use Redis or a central database for shared session and cache state.