What if your Node.js server could magically use all CPU cores to handle huge traffic without crashing?
How cluster module works in Node.js - Why You Should Know This
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine running a Node.js server that handles many users at once, but it only uses one CPU core.
When traffic spikes, the server slows down and users wait longer.
Node.js runs on a single thread by default, so it can only use one CPU core.
This means it can't handle many requests in parallel efficiently, causing slow responses and crashes under heavy load.
The cluster module lets you create multiple Node.js processes (workers) that share the same server port.
This way, your app can use all CPU cores, handling many requests at once smoothly.
const http = require('http'); http.createServer((req, res) => { res.end('Hello'); }).listen(3000);
const cluster = require('cluster'); const http = require('http'); const os = require('os'); if (cluster.isMaster) { for (let i = 0; i < os.cpus().length; i++) { cluster.fork(); } } else { http.createServer((req, res) => { res.end('Hello'); }).listen(3000); }
You can build fast, reliable servers that use all CPU cores to serve many users at the same time without slowing down.
A popular chat app uses the cluster module to run multiple worker processes, so thousands of users can send messages instantly without delays.
Node.js single-thread limits performance under heavy load.
Cluster module creates multiple worker processes to use all CPU cores.
This improves speed and reliability for busy servers.
Practice
cluster module in Node.js?Solution
Step 1: Understand the cluster module role
The cluster module allows Node.js to create multiple worker processes.Step 2: Recognize the benefit
These workers use all CPU cores to improve performance by handling requests in parallel.Final Answer:
To create multiple worker processes to use all CPU cores -> Option DQuick Check:
cluster module = multiple workers for CPU cores [OK]
- Confusing cluster with database or file system modules
- Thinking cluster manages UI or frontend
- Assuming cluster runs code in a single process
Solution
Step 1: Recall the updated property name
In recent Node.js versions,cluster.isPrimaryreplacescluster.isMaster.Step 2: Identify the correct syntax
Usingcluster.isPrimarycorrectly checks if the process is the primary (master) process.Final Answer:
if (cluster.isPrimary) { ... } -> Option BQuick Check:
Primary process check = cluster.isPrimary [OK]
- Using deprecated cluster.isMaster instead of cluster.isPrimary
- Confusing isWorker with isPrimary
- Using non-existent properties like isMain
const cluster = require('cluster');
const http = require('http');
if (cluster.isPrimary) {
cluster.fork();
cluster.fork();
} else {
http.createServer((req, res) => {
res.end('Worker ' + process.pid);
}).listen(8000);
}What will happen when you visit
http://localhost:8000 multiple times?Solution
Step 1: Understand cluster.fork creates workers
Two workers are created, each running the HTTP server on port 8000.Step 2: Recognize load balancing behavior
Requests are distributed between workers, so responses show different process IDs.Final Answer:
You will see responses from different worker process IDs -> Option AQuick Check:
Multiple workers share port, respond with different PIDs [OK]
- Thinking only one worker handles all requests
- Assuming server crashes due to multiple forks
- Expecting syntax errors from this code
const cluster = require('cluster');
if (cluster.isPrimary) {
cluster.fork();
} else {
console.log('Worker running');
}Solution
Step 1: Check cluster usage
The primary forks one worker, which only logs a message but does not start a server.Step 2: Identify missing functionality
Without server code, the worker does not handle requests, so the cluster setup is incomplete.Final Answer:
Missing server code inside worker -> Option CQuick Check:
Worker must run server code to handle requests [OK]
- Confusing isPrimary with deprecated isMaster
- Expecting cluster.fork in worker process
- Assuming code runs without server in worker
Solution
Step 1: Understand worker crash handling
The primary process can listen to the 'exit' event when a worker dies.Step 2: Restart worker on exit
Inside the 'exit' event handler, calling cluster.fork() creates a new worker to replace the crashed one.Final Answer:
Listen to the 'exit' event on cluster and fork a new worker inside the handler -> Option AQuick Check:
Restart crashed workers by handling 'exit' event [OK]
- Forking workers repeatedly with setInterval causes overload
- Not restarting workers after crash leads to downtime
- Using cluster.disconnect() inside worker does not restart it
