tezvyn:

PM2 cluster mode and multi-core utilization?

AI-drafted, machine-checkedSource: interviewintermediate
WHAT IT TESTS

understanding multi-process concurrency and PM2's cluster mode.

OUTLINE

cluster mode forks multiple worker processes using Node.js cluster module, distributes load, uses all CPU cores.

SINGLE-PROCESS BOTTLENECK

Node.js runs single-threaded per process. A server with 8 CPU cores only uses 1 core when running a single Node.js app, leaving 7 cores idle. Throughput is capped at what one core can handle. This is a surprising limitation for beginners expecting Node.js to automatically scale.

CLUSTER MODULE FOUNDATION

Node.js provides a built-in cluster module that enables multi-process applications. It defines a master process that forks worker processes. The master can spawn as many workers as there are CPU cores. Each worker is a full Node.js event loop with its own memory space. The master load-balances incoming connections across workers.

PM2 CLUSTER MODE

PM2 wraps the cluster module, making it easy: pm2 start app.js -i max. The -i flag specifies instance count; max means one per CPU core. PM2 internally forks workers, manages restarts, and distributes traffic. Application code does not change; it is cluster-aware transparently. Unlike using cluster module directly, PM2 handles restarts and monitoring.

LOAD BALANCING

When a client connects, the master process accepts the connection and passes it to an available worker using round-robin. This distributes load evenly. If a worker crashes, the master detects it and removes that worker from rotation, eventually restarting it. Ongoing requests on the dead worker are dropped, but other workers continue serving.

PRIMARY BENEFIT: FULL CPU UTILIZATION

With 8 workers on an 8-core machine, the app can process 8 requests in parallel, one per core. Throughput increases 8x without code changes. This is the main benefit: transform an undiscovered single-core machine into a multi-core workload.

LIMITATION: STATEFUL SESSIONS

If your app stores session state in memory (not shared Redis), different requests from the same client might land on different workers, losing context. Shared session storage (Redis) solves this. Stateless applications scale perfectly with cluster mode.

VERSUS HORIZONTAL SCALING

Cluster mode uses multiple cores on one machine. Horizontal scaling uses multiple machines. Cluster mode is simple and works on any server. Horizontal scaling is necessary when a single machine is maxed out or for redundancy.

Read the original → pm2.keymetrics.io

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.