Node.js Internals6h estimated

Cluster Mode & Scaling Node

Difficulty
Importance

The problem

A single Node process runs on a single CPU core, no matter how many cores the machine has — leaving most of a modern multi-core server idle unless something deliberately spreads the work across more than one Node process.

Why now

Child processes and worker threads already gave the mechanism (spawning separate processes); cluster mode is that mechanism applied specifically to the scaling problem, and CPU scheduling already established that the OS decides which core actually runs which process — cluster mode's whole point is giving the OS multiple Node processes to schedule across cores instead of one.

Mental model

Cluster mode forks multiple copies of your server process — one per CPU core is typical — all listening on the same port, with the OS (or a built-in round-robin) distributing incoming connections across them. Each worker is a fully separate process with its own event loop and memory, so cluster mode is horizontal scaling within a single machine, the same underlying idea as running multiple server instances behind a load balancer, just one level closer to the metal.

Requires

Used in

Projects

  • Convert a single-process Node server into a clustered one using the cluster module, forking one worker per CPU core, and benchmark throughput before and after under concurrent load
  • Implement a worker restart strategy so that if one clustered worker crashes, the primary process detects it and forks a replacement automatically

Examples

  • A single Node process on an 8-core machine uses at most 1 core under CPU-bound load; cluster mode forking 8 workers can use all 8, roughly multiplying CPU-bound throughput
  • PM2's cluster mode combines Node's cluster module with automatic worker restarts, giving zero-downtime deploys by replacing workers one at a time instead of stopping the whole server

Resources

Mastery checklist