Zero-downtime deployment means updating your app without making users wait or see errors. It keeps the app working smoothly while you add new features or fix bugs.
Zero-downtime deployment concept in Express
const cluster = require('cluster'); const http = require('http'); const numCPUs = require('os').cpus().length; if (cluster.isMaster) { for (let i = 0; i < numCPUs; i++) { cluster.fork(); } cluster.on('exit', (worker, code, signal) => { console.log(`Worker ${worker.process.pid} died. Starting a new one.`); cluster.fork(); }); } else { const app = require('./app'); app.listen(3000); }
This example uses Node.js cluster to run multiple app instances.
When a worker stops, the master starts a new one to keep the app running.
const cluster = require('cluster'); if (cluster.isMaster) { cluster.fork(); } else { const app = require('./app'); app.listen(3000); }
cluster.on('exit', (worker) => { console.log(`Worker ${worker.process.pid} exited.`); cluster.fork(); });
This program uses Node.js cluster to run multiple Express workers. If a worker stops, the master starts a new one. This keeps the app available without downtime during updates or crashes.
const cluster = require('cluster'); const http = require('http'); const numCPUs = require('os').cpus().length; if (cluster.isMaster) { console.log(`Master ${process.pid} is running`); for (let i = 0; i < numCPUs; i++) { cluster.fork(); } cluster.on('exit', (worker, code, signal) => { console.log(`Worker ${worker.process.pid} died. Starting a new one.`); cluster.fork(); }); } else { const express = require('express'); const app = express(); app.get('/', (req, res) => { res.send(`Hello from worker ${process.pid}`); }); app.listen(3000, () => { console.log(`Worker ${process.pid} started`); }); }
Zero-downtime deployment often uses multiple processes or servers to avoid stopping the app.
Using a process manager like PM2 can simplify zero-downtime deployment.
Always test your deployment process to ensure users don't see errors or downtime.
Zero-downtime deployment keeps your app running while updating.
Node.js cluster helps run multiple app instances for this purpose.
Restarting workers automatically avoids downtime if one crashes.
Practice
Solution
Step 1: Understand zero-downtime deployment purpose
It aims to keep the app available and responsive while new code is deployed.Step 2: Compare options with this goal
Only keeping the app running without interruption matches zero-downtime deployment.Final Answer:
Keep the app running without interruption during updates -> Option AQuick Check:
Zero-downtime = no interruption [OK]
- Thinking the app must stop to update
- Confusing manual restart with automatic update
- Assuming routes must be disabled during deployment
Solution
Step 1: Identify module for multiple app instances
The cluster module allows running multiple worker processes of the app.Step 2: Verify other modules' purposes
http handles server requests, fs handles files, path handles file paths, none manage multiple processes.Final Answer:
cluster -> Option DQuick Check:
Multiple instances = cluster module [OK]
- Choosing http for multiple processes
- Confusing fs or path with process management
- Not knowing cluster exists
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);
}Solution
Step 1: Analyze cluster exit event handler
When a worker exits, the master logs a message and forks a new worker.Step 2: Understand effect on app availability
Replacing crashed workers keeps the app running without downtime.Final Answer:
A new worker starts automatically -> Option AQuick Check:
Worker crash triggers new worker fork [OK]
- Thinking app stops when worker dies
- Assuming master process crashes
- Missing automatic restart logic
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);
}Solution
Step 1: Check cluster exit event handler
The exit event logs a message but does not fork a new worker.Step 2: Understand impact on zero-downtime
Without restarting workers, the app loses capacity and may cause downtime.Final Answer:
Missing code to restart worker on exit -> Option BQuick Check:
Exit handler must fork new worker [OK]
- Not restarting workers after exit
- Confusing master and worker roles
- Placing app.listen in wrong process
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?
Solution
Step 1: Understand zero-downtime deployment best practices
Running multiple workers (step 1) allows traffic to continue during updates.Step 2: Deploy new code by forking new workers (step 2) and gracefully disconnect old ones (step 3)
This ensures no requests are dropped and old workers finish their tasks.Step 3: Avoid immediate server restart (step 4) as it causes downtime
Restarting the whole server stops all workers at once, causing downtime.Final Answer:
Steps 1, 2, and 3 -> Option CQuick Check:
Fork new workers and gracefully disconnect old [OK]
- Restarting server immediately causing downtime
- Not gracefully disconnecting old workers
- Skipping multiple worker setup
Ready to prove it?
Pass the test and this pattern moves unknown
