Skip to main content

How does cluster help scale an application?

1. The problem of being single-threaded

Node.js is single-threaded by nature - all JavaScript runs on one thread with one event loop.

That means:

  • one Node.js app can use only one CPU core;
  • if the server has multiple cores (as most do), the rest sit idle;
  • under heavy request load, the event loop can get overwhelmed, and the server starts to lag.

2. What cluster does

The cluster module lets you run several copies (workers) of your application

  • one per CPU core - and spread the load across them.

In other words:

cluster turns one Node.js server into a pool of independent processes, running in parallel, each with its own event loop and memory.

3. How it works

The cluster architecture:

javascript
┌───────────────────────┐ Master process │ - listens on a port (e.g. 3000)- creates workers (via fork)└──────────────┬────────┘ ┌───────────┴───────────┐ │ │ │ ▼ ▼ ▼ Worker 1 Worker 2 Worker 3 ... Worker N (port 3000) (port 3000) (port 3000)

Every worker:

  • is a separate Node.js process;
  • has its own event loop, its own runtime, its own memory;
  • listens on the same port as the others.

The master process:

  • accepts incoming connections,
  • distributes requests among workers (using Round Robin),
  • watches over the workers' health and can restart them on failure.

4. An example of scaling with cluster

javascript
import cluster from 'cluster'; import http from 'http'; import os from 'os'; if (cluster.isPrimary) { const numCPUs = os.cpus().length; console.log(`Primary process ${process.pid}, starting ${numCPUs} workers...`); // Create workers, one per core for (let i = 0; i < numCPUs; i++) cluster.fork(); cluster.on('exit', (worker) => { console.log(`Worker ${worker.process.pid} exited. Restarting...`); cluster.fork(); }); } else { // Each worker creates its own HTTP server http.createServer((req, res) => { res.end(`Response from worker ${process.pid}\n`); }).listen(3000); console.log(`Worker ${process.pid} started`); }

On an 8-core processor, this creates 8 workers, all of them listening on port 3000 and sharing the load.

5. How cluster improves scalability

ProblemWhat cluster does
Node.js uses only 1 coreCreates one process per core
One event loop is a bottleneckSeveral event loops (one per worker)
One process can crashThe master restarts the worker
Rising traffic raises loadRequests spread across workers
Limits on memory and GCEach worker has its own heap

In short, cluster solves the CPU-scaling problem ("vertical scaling"), loading every processor core as much as possible.

6. Advantages of using cluster

AdvantageDescription
Multi-processor throughputThe app handles more requests at once
Error isolationA crashed worker doesn't affect the rest
Automatic restartThe cluster can bring a worker back on its own
Load distributionAll workers listen on one port, load is even
Compatible with Express / Fastify / NestJSAn existing server can simply be "wrapped"
Production-readyUsed internally by PM2 and other process managers

7. An example: load testing

Without cluster (1 process, 1 core):

  • the server handles, say, 1,000 requests/sec.

With cluster (8 workers on 8 cores):

  • each worker handles ~1,000 requests/sec,
  • total throughput is roughly 8,000 requests/sec.

8. Scaling plus resilience

cluster doesn't just speed things up, it also makes the system resilient:

  • if a worker hangs or crashes → the master notices via the exit event;
  • it spawns a new worker to replace it;
  • the server keeps running with no downtime.

9. How clustering is used in practice

Commonly used:

  • in production servers (Express, Fastify, NestJS);
  • in microservice and REST API systems;
  • inside process managers, such as PM2;
  • behind WebSocket or GraphQL server balancers.

PM2 uses cluster under the hood:

javascript
pm2 start app.js -i max

⟶ automatically starts as many workers as there are CPU cores.

10. Worth remembering

  • Workers don't share memory → data needs to be synchronized through IPC or Redis.
  • For WebSocket connections, sticky sessions are better, so one connection stays on one worker.
  • Scaling across several machines needs an external load balancer (Nginx, HAProxy, AWS ELB).

Summary

Criterioncluster
What it doesRuns several copies of the app across every CPU core
How it scalesDistributes requests across processes
Where it's usedServers, APIs, production apps
Type of scalingVertical (across CPU cores)
Data exchangeVia IPC (Inter-Process Communication)
BenefitsPerformance, resilience, fault tolerance

Conclusion:

The cluster module lets Node.js use every available processor core, creating a pool of workers that serve requests in parallel.

It delivers scalability, resilience and high performance - without changing the application's logic.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.