0
0
Node.jsframework~10 mins

Master and worker processes in Node.js - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Master and worker processes
Start Master Process
Fork Worker Processes
Master Listens for Messages
Workers Run Tasks
Workers Send Results to Master
Master Aggregates Results
Master Handles Worker Exit
End
The master process starts and creates worker processes. Workers run tasks and send results back. The master listens and manages workers until all finish.
Execution Sample
Node.js
import cluster from 'cluster';
import os from 'os';

if (cluster.isPrimary) {
  for (let i = 0; i < os.cpus().length; i++) cluster.fork();
  cluster.on('message', (worker, msg) => {
    // Master receives message
  });
  cluster.on('exit', (worker) => {
    // Master handles worker exit
  });
} else {
  process.send('worker done');
  process.exit(0);
}
This code forks one worker per CPU core. Each worker sends a message back to the master when done.
Execution Table
StepProcess TypeActionState ChangeMessage Sent/Received
1MasterCheck if masterTrue - proceed to forkNone
2MasterFork worker 1Worker 1 createdNone
3MasterFork worker 2Worker 2 createdNone
4Worker 1Run taskTask runningNone
5Worker 2Run taskTask runningNone
6Worker 1Send message to masterMessage sent'worker done'
7MasterReceive message from worker 1Message received'worker done'
8Worker 2Send message to masterMessage sent'worker done'
9MasterReceive message from worker 2Message received'worker done'
10MasterHandle worker exitWorker processes closedNone
11MasterAll workers doneMaster endsNone
💡 All workers have sent completion messages and exited; master process finishes managing workers.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 6After Step 9Final
workerCount012222
messagesReceived000122
workersAlive012220
Key Moments - 3 Insights
Why does the master process fork multiple workers instead of running tasks itself?
The master forks workers to run tasks in parallel, using multiple CPU cores. This is shown in execution_table steps 2 and 3 where workers are created.
How does the master know when a worker has finished its task?
Workers send messages back to the master (steps 6 and 8). The master listens and updates state when it receives these messages (steps 7 and 9).
What happens if a worker crashes or exits unexpectedly?
The master handles worker exit events (step 10) to clean up or restart workers if needed, ensuring the system stays stable.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step does the master receive the first message from a worker?
AStep 6
BStep 8
CStep 7
DStep 9
💡 Hint
Check the 'Message Sent/Received' column for when the master receives messages.
According to variable_tracker, how many workers are alive after step 9?
A0
B2
C1
D3
💡 Hint
Look at the 'workersAlive' row and the value under 'After Step 9'.
If the master did not fork any workers, what would happen to 'messagesReceived' in variable_tracker?
AIt would stay at 0
BIt would increase to 2
CIt would become negative
DIt would be undefined
💡 Hint
Without workers, no messages can be sent or received; check initial values in variable_tracker.
Concept Snapshot
Master process controls worker processes.
Master forks workers to run tasks in parallel.
Workers send messages back to master.
Master listens and manages worker lifecycle.
Useful for using multiple CPU cores efficiently.
Full Transcript
In Node.js, the master process starts and forks multiple worker processes, usually one per CPU core. Each worker runs its own task independently. When a worker finishes, it sends a message back to the master process. The master listens for these messages to know when workers complete their tasks. It also handles worker exits to keep the system stable. This setup allows Node.js to use multiple CPU cores by running tasks in parallel across workers. The execution table shows each step: master forking workers, workers running tasks, sending messages, and master receiving them. The variable tracker shows how counts of workers and messages change over time. This helps beginners see how master and worker processes interact step-by-step.