Cluster Module and Scaling
Scaling Node.js: The Cluster Module
Because the V8 engine operates on a single thread, a default Node.js process can fully utilize only one CPU core. Modern production servers possess dozens of cores, meaning a single Node.js instance drastically underutilizes available hardware.
Clustering Mechanisms
The native cluster module allows a primary (master) process to fork identical worker processes. These workers operate simultaneously and share the same server port.
- Load Balancing: The OS kernel distributes incoming socket connections across the available workers using a round-robin algorithm.
- Resiliency: If a worker thread crashes due to an unhandled exception, the primary process can intercept the exit event and immediately fork a replacement instance, ensuring zero-downtime self-healing.
Note: In contemporary architectures, process managers like PM2 or container orchestrators like Kubernetes often supersede manual cluster implementation.