Performance: Zero-downtime deployment concept
This concept affects the availability and responsiveness of a web application during updates, ensuring users experience no interruptions.
const cluster = require('cluster'); if (cluster.isMaster) { cluster.fork(); cluster.on('exit', () => { cluster.fork(); }); } else { app.listen(3000); } // Use process signals to reload workers gracefully
app.listen(3000); // To deploy update: // 1. Stop server // 2. Replace code // 3. Restart server
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Stop and restart server for deployment | N/A | N/A | N/A | [X] Bad |
| Cluster with graceful worker reload | N/A | N/A | N/A | [OK] Good |
const cluster = require('cluster');
const express = require('express');
if (cluster.isMaster) {
cluster.fork();
cluster.on('exit', (worker) => {
console.log('Worker died, starting a new one');
cluster.fork();
});
} else {
const app = express();
app.get('/', (req, res) => res.send('Hello'));
app.listen(3000);
}const cluster = require('cluster');
const express = require('express');
if (cluster.isMaster) {
cluster.fork();
cluster.on('exit', () => {
console.log('Worker died');
});
} else {
const app = express();
app.listen(3000);
}1. Use cluster to run multiple workers 2. On update, fork new workers with new code 3. Gracefully disconnect old workers after new ones are ready 4. Restart the entire server process immediatelyWhich steps should you follow?
Pass the test and this pattern moves unknown