Senior LevelNode JS
How do you implement zero-downtime rolling worker reloads using the node:cluster module?
❓ Interview Prompt: Explain zero-downtime server reloads, spawning new cluster workers before disconnecting old workers, and handling SIGHUP/SIGUSR2.
Rolling reloads restart server workers one by one without dropping active HTTP connections:
Rolling Reload Workflow:
1
Catch 'process.on('SIGUSR2')' in primary process.
2
Fork a new worker: 'const newWorker = cluster.fork()'.
3
Once 'newWorker' is listening, send 'disconnect()' signal to old worker to drain active connections cleanly!
Code Implementation Example
javascript
import cluster from 'node:cluster';
if (cluster.isPrimary) {
process.on('SIGUSR2', () => {
console.log('Rolling reload initiated...');
const workers = Object.values(cluster.workers);
const reloadWorker = (index) => {
if (index >= workers.length) return;
const oldWorker = workers[index];
const newWorker = cluster.fork();
newWorker.on('listening', () => {
oldWorker.disconnect(); // Drain old worker cleanly
reloadWorker(index + 1);
});
};
reloadWorker(0);
});
}💡Implementing zero-downtime rolling worker reloads in node:cluster.
🎯 Interview Key Takeaway
Implement rolling worker reloads in cluster mode to apply application updates without dropping active connections.
#cluster#Rolling Reload#Zero Downtime#Senior
Back to all Node JS Questions→