0
0
Node.jsframework~8 mins

Handling worker crashes and restart in Node.js - Performance & Optimization

Choose your learning style9 modes available
Performance: Handling worker crashes and restart
HIGH IMPACT
This concept affects server responsiveness and stability, impacting how quickly the server recovers from worker failures without blocking requests.
Managing worker crashes in a Node.js cluster
Node.js
import cluster from 'node:cluster';
import os from 'node:os';

if (cluster.isPrimary) {
  const numCPUs = os.cpus().length;
  for (let i = 0; i < numCPUs; i++) {
    cluster.fork();
  }

  cluster.on('exit', (worker, code, signal) => {
    console.log(`Worker ${worker.process.pid} died, restarting...`);
    cluster.fork();
  });
} else {
  // Worker code
  import('./server.js');
}
Automatically restarts workers on crash, maintaining full capacity and responsiveness without manual intervention.
📈 Performance GainMaintains steady request handling capacity; reduces INP spikes caused by worker downtime.
Managing worker crashes in a Node.js cluster
Node.js
import cluster from 'node:cluster';
import os from 'node:os';

if (cluster.isPrimary) {
  const numCPUs = os.cpus().length;
  for (let i = 0; i < numCPUs; i++) {
    cluster.fork();
  }

  cluster.on('exit', (worker, code, signal) => {
    console.log(`Worker ${worker.process.pid} died`);
    // No restart logic here
  });
} else {
  // Worker code
  import('./server.js');
}
When a worker crashes, it is logged but not restarted, causing reduced capacity and slower response times until manual intervention.
📉 Performance CostBlocks handling of some requests until manual restart; increases INP due to fewer workers.
Performance Comparison
PatternWorker AvailabilityRequest DelayRecovery TimeVerdict
No restart on crashDecreases over timeIncreases significantlyManual restart needed[X] Bad
Automatic restart on crashMaintained at full capacityMinimal increaseImmediate recovery[OK] Good
Rendering Pipeline
In Node.js server clusters, worker crashes cause request handling delays. Restarting workers quickly restores capacity, minimizing request queuing and delays.
Request Handling
Event Loop
Process Management
⚠️ BottleneckReduced worker availability causes request queuing and slower event loop processing.
Core Web Vital Affected
INP
This concept affects server responsiveness and stability, impacting how quickly the server recovers from worker failures without blocking requests.
Optimization Tips
1Always listen to the 'exit' event to detect worker crashes.
2Restart workers immediately to maintain full concurrency and responsiveness.
3Avoid manual restarts to prevent prolonged request delays and degraded user experience.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance benefit of automatically restarting crashed workers in a Node.js cluster?
AMaintains server responsiveness by quickly restoring worker capacity
BReduces memory usage by killing workers permanently
CImproves CPU usage by limiting the number of workers
DPrevents any worker from ever crashing
DevTools: Node.js Inspector (Debugger) and Logs
How to check: Run the Node.js app with --inspect flag, monitor cluster worker exit events in console logs, and observe if workers restart automatically after crash.
What to look for: Look for logs showing worker crashes followed by immediate worker forks indicating automatic restarts, ensuring no prolonged request delays.