Cluster Mode & Scaling Node
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
- Child Processes & Worker ThreadsNode.js Internals
Cluster mode is built directly on Node's child_process forking mechanism already covered; you need to already understand what a forked child process is and how it communicates before cluster mode's worker-management layer on top of it makes sense.
- CPU SchedulingOperating Systems
Cluster mode's entire value proposition is giving the OS multiple processes to schedule across multiple cores; you need the OS-level scheduling problem already understood to see why one process per core, not more, is the natural target.
Used in
- Node's built-in cluster module for multi-core scaling within one machine
- Process managers (PM2) that manage cluster mode and automatic worker restarts
- Understanding the boundary between scaling within a machine (cluster) and across machines (load balancing)
- Zero-downtime restarts by cycling cluster workers one at a time
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
- I can explain why a single Node process can't use more than one CPU core
- I can set up cluster mode to fork one worker per core
- I can implement automatic worker restart on crash
- I can explain the difference between scaling via cluster mode and scaling via multiple machines behind a load balancer