Node.js › Clustering & Worker Threads

How do you handle an uncaught exception inside a specific cluster worker without taking down the entire application?

Advanced

Answer

Because each worker is an independent process, a crash doesn't directly crash the others -- but the crashed worker stops serving traffic until replaced. The primary should listen for 'exit' and fork a replacement, restoring capacity quickly while the error is still logged for investigation.
cluster.on('exit', (worker, code, signal) => {
  console.error(`Worker ${worker.process.pid} died. Forking a replacement.`);
  cluster.fork();
});
Real-world example A production cluster automatically forks a replacement worker within milliseconds of any crash, keeping total capacity roughly constant while alerting the on-call engineer to investigate.

Common follow-ups: What's the risk of a worker crashing and restarting in a tight loop if a bug is triggered by every request of a certain type?;How would you add backoff or a circuit-breaker to the fork-on-exit logic?