Performancehigh

Cluster Module

The cluster module forks multiple Node.js processes (workers) that share the same server port. The primary process manages workers; workers handle requests. This utilizes multiple CPU cores.

Memory anchor

Cluster = cloning yourself into N copies, each handling customers at a separate cash register but sharing the same store entrance (port). Each clone has its own brain (heap)—clone #1's shopping list (cache) is invisible to clone #2. Need shared memory? Use an external bulletin board (Redis).

Expected depth

cluster.fork() creates child processes that each run the same script. The primary process distributes incoming connections to workers using a round-robin algorithm (on Linux/macOS; on Windows it's left to the OS). Workers are independent processes with separate heaps—no shared memory. When a worker crashes, the primary can fork a replacement. cluster is the original multi-core solution; it predates worker_threads.

Deep — senior internals

Under the hood, cluster uses child_process.fork() and sets up an IPC channel between primary and workers. The magic: all workers call server.listen() on the same port, but only the primary actually binds to the socket. The primary receives connections and distributes them to workers via the IPC channel, sending the socket handle. This is why round-robin works—the primary controls distribution. In production, PM2 uses the cluster module under the hood with its cluster mode. The key operational concern: session affinity (sticky sessions) requires external load balancing if in-memory session state is used, since different requests from the same client may hit different workers.

🎤Interview-ready answer

The cluster module forks N processes (typically one per CPU core) that all listen on the same port. The primary process accepts connections and distributes them to workers via IPC-passed socket handles. Workers are fully isolated processes with separate heaps. Cluster provides resilience (respawn crashed workers) and multi-core utilization but requires stateless application design or external session storage for sticky sessions.

Common trap

In-memory caches (like node-cache or a plain Map) are worker-local. A cached item written by worker 1 is invisible to worker 2. Engineers migrating from single-process development to cluster mode discover this when cache hit rates drop to near zero under load. Use Redis or Memcached for shared state in clustered environments.

Related concepts