0
0
Node.jsframework~10 mins

Handling worker crashes and restart in Node.js - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Handling worker crashes and restart
Start Master Process
Fork Worker Process
Worker Runs
Worker Crashes?
NoContinue Running
Yes
Master Detects Crash
Master Forks New Worker
Worker Runs
The master process starts and forks a worker. If the worker crashes, the master detects it and restarts a new worker, keeping the system running.
Execution Sample
Node.js
import cluster from 'cluster';
import os from 'os';

if (cluster.isPrimary) {
  cluster.fork();
  cluster.on('exit', (worker) => {
    console.log(`Worker ${worker.process.pid} died, restarting...`);
    cluster.fork();
  });
} else {
  // Worker code
  setTimeout(() => { process.exit(1); }, 1000); // Simulate crash
}
This code forks a worker that crashes after 1 second; the master detects the crash and restarts the worker.
Execution Table
StepActionWorker PIDMaster Detects CrashMaster ActionOutput
1Master starts and forks worker12345NoNoneWorker 12345 started
2Worker runs normally12345NoNoneWorker 12345 running
3Worker crashes (exit code 1)12345YesFork new workerWorker 12345 died, restarting...
4Master forks new worker12346NoNoneWorker 12346 started
5New worker runs12346NoNoneWorker 12346 running
6No more crashes-NoNoneSystem stable
ExitNo crash detected-NoNoneExecution stops
💡 No crash detected, system runs stable
Variable Tracker
VariableStartAfter Step 1After Step 3After Step 4Final
worker PIDnone1234512345 (crashed)1234612346
master detects crashfalsefalsetruefalsefalse
Key Moments - 2 Insights
Why does the master fork a new worker after a crash?
Because the execution_table row 3 shows the master detects the worker crash and immediately forks a new worker to keep the system running.
Does the master fork multiple workers at start?
No, the master forks only one worker at start as shown in execution_table row 1; additional forks happen only after crashes.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what does the master do when the worker crashes?
AForks a new worker
BDoes nothing
CShuts down the system
DLogs but does not restart
💡 Hint
See execution_table row 3 under 'Master Action' and 'Output'
At which step does the new worker start running after the crash?
AStep 2
BStep 4
CStep 5
DStep 6
💡 Hint
Check execution_table rows 4 and 5 for worker start and running
If the worker never crashes, what happens to the master’s restart logic?
AIt runs repeatedly
BIt forks multiple workers at start
CIt never runs
DIt shuts down the master
💡 Hint
Look at variable_tracker 'master detects crash' staying false and execution_table exit note
Concept Snapshot
Node.js cluster master forks worker(s).
If a worker crashes, master detects 'exit' event.
Master forks a new worker to replace crashed one.
This keeps the app running continuously.
Use cluster.on('exit', ...) to handle crashes.
Restart logic runs only on worker crash.
Full Transcript
In Node.js cluster, the master process starts and forks a worker process. The worker runs its code. If the worker crashes, the master detects this via the 'exit' event. Upon detection, the master immediately forks a new worker to replace the crashed one. This cycle ensures the application keeps running even if workers fail. The example code forks one worker that crashes after one second. The master logs the crash and restarts the worker. Variables like worker PID and crash detection status change as the program runs. The master only forks new workers after crashes, not multiple at start. This pattern is essential for building resilient Node.js applications using cluster.