Node.js › Clustering & Worker Threads
How do you handle an uncaught exception inside a specific cluster worker without taking down the entire application?
AdvancedAnswer
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.