0
0
Node.jsframework~10 mins

Load balancing between workers in Node.js - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Load balancing between workers
Start Master Process
Fork Worker Processes
Workers Listen for Requests
OS Distributes to Workers
Workers Process Requests
Send Response Back
OS Balances Load
More Requests?
No
Shutdown
The master process forks workers. Workers listen for requests on the same port, and the OS distributes them evenly to workers for processing.
Execution Sample
Node.js
import cluster from 'node:cluster';
import { cpus } from 'node:os';
import http from 'node:http';

if (cluster.isPrimary) {
  for (let i = 0; i < cpus().length; i++) cluster.fork();
} else {
  http.createServer((req, res) => {
    res.end(`Handled by worker ${process.pid}`);
  }).listen(8000);
}
This code forks one worker per CPU core and balances incoming HTTP requests among them.
Execution Table
StepActionMaster StateWorker StateLoad Distribution
1Master starts and checks cluster.isPrimaryPrimary: true, forks 4 workersNo workers yetNo load yet
2Master forks worker 1Primary: true, 1 worker forkedWorker 1 startedNo load yet
3Master forks worker 2Primary: true, 2 workers forkedWorker 2 startedNo load yet
4Master forks worker 3Primary: true, 3 workers forkedWorker 3 startedNo load yet
5Master forks worker 4Primary: true, 4 workers forkedWorker 4 startedNo load yet
6Workers listen on port 8000Primary: trueWorkers listeningNo load yet
7Request 1 arrivesPrimary: trueWorker 1 handles requestRequest 1 -> Worker 1
8Request 2 arrivesPrimary: trueWorker 2 handles requestRequest 2 -> Worker 2
9Request 3 arrivesPrimary: trueWorker 3 handles requestRequest 3 -> Worker 3
10Request 4 arrivesPrimary: trueWorker 4 handles requestRequest 4 -> Worker 4
11Request 5 arrivesPrimary: trueWorker 1 handles requestRequest 5 -> Worker 1
12No more requestsPrimary: trueWorkers idleLoad balanced evenly
13Master shuts down workersPrimary: true, workers shutdownWorkers stoppedNo load
💡 No more requests; all workers have handled requests evenly; master shuts down.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5Final
workersForked0123444
requestsHandledByWorker10111122
requestsHandledByWorker20011111
requestsHandledByWorker30001111
requestsHandledByWorker40000111
Key Moments - 3 Insights
Why does the master fork multiple workers instead of handling requests itself?
The master process forks workers to use multiple CPU cores for parallel processing. The master does not handle requests directly; workers do, as shown in steps 1-5 and 7-11 in the execution table.
How does the master decide which worker gets a request?
Node.js cluster module uses a round-robin approach by default on most platforms, distributing requests evenly among workers. This is seen in the execution table where requests 1 to 5 are assigned to workers 1 to 4, then back to worker 1.
What happens if a worker crashes?
The master can detect worker exit events and fork a new worker to replace it, maintaining load balance. This is not shown in the sample but is part of cluster management best practices.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, which worker handles the 3rd request?
AWorker 3
BWorker 2
CWorker 1
DWorker 4
💡 Hint
Check row 9 in the execution table where Request 3 is handled.
At which step does the master finish forking all workers?
AStep 4
BStep 5
CStep 6
DStep 7
💡 Hint
Look at the 'Master forks worker 4' action in the execution table.
If the number of CPUs doubles, how would the 'workersForked' variable change after forking?
AIt stays the same
BIt halves
CIt doubles
DIt becomes zero
💡 Hint
Refer to the variable_tracker row for 'workersForked' and the for loop in the code sample.
Concept Snapshot
Load balancing in Node.js cluster:
- Master process forks workers equal to CPU cores
- Workers listen on the same port
- Requests are balanced round-robin by default (OS/kernel level)
- Workers handle requests independently
- Master can restart crashed workers to maintain balance
Full Transcript
In Node.js, load balancing between workers is done using the cluster module. The master process starts and forks one worker per CPU core. Each worker runs its own server instance. Incoming requests are distributed evenly among workers, usually in a round-robin fashion by the OS. Workers process requests independently and send responses back. This approach uses all CPU cores efficiently. The master can monitor workers and restart any that crash to keep the system balanced. The execution table shows the master forking workers, workers starting servers, and requests being assigned one by one to each worker in turn. Variables track how many workers are forked and how many requests each worker handles. This method improves performance by parallelizing work across CPU cores.