📖 Scenario: You are building a simple Express server that serves a message. You want to prepare your code to support zero-downtime deployment, which means the server can restart without dropping any requests.
🎯 Goal: Build a basic Express server with a simple route. Then add a configuration variable to simulate a graceful shutdown delay. Finally, implement the logic to handle server shutdown gracefully to avoid downtime.
📋 What You'll Learn
Create an Express app with a GET route at '/' that returns 'Server is running'.
Add a variable called shutdownDelay set to 3000 (milliseconds).
Implement a function gracefulShutdown that closes the server after the delay.
Use process.on('SIGTERM') to trigger gracefulShutdown on termination signal.
💡 Why This Matters
🌍 Real World
Zero-downtime deployment is important for web servers to update or restart without interrupting user requests. This project shows how to prepare an Express server to close gracefully when the system signals it to stop.
💼 Career
Many backend developer roles require knowledge of server lifecycle management and deployment strategies. Understanding graceful shutdowns helps maintain reliable services in production.
Progress0 / 4 steps
1
Set up Express server with a GET route
Create an Express app by requiring express and calling it. Then create a server that listens on port 3000. Add a GET route at '/' that sends the text 'Server is running' as the response.
Express
Hint
Use require('express') to import Express. Use app.get to create the route. Use app.listen(3000) to start the server.
2
Add shutdown delay configuration
Add a variable called shutdownDelay and set it to 3000 to represent milliseconds delay before shutdown.
Express
Hint
Declare const shutdownDelay = 3000; below the server start.
3
Implement graceful shutdown function
Write a function called gracefulShutdown that logs 'Shutdown initiated', waits for shutdownDelay milliseconds using setTimeout, then closes the server and logs 'Server closed'.
Express
Hint
Use setTimeout with shutdownDelay to delay closing the server.
4
Trigger graceful shutdown on SIGTERM signal
Add a listener for the 'SIGTERM' event on process that calls the gracefulShutdown function.
Express
Hint
Use process.on('SIGTERM', gracefulShutdown); to listen for termination signals.
Practice
(1/5)
1. What is the main goal of zero-downtime deployment in an Express app?
easy
A. Keep the app running without interruption during updates
B. Stop the app completely before updating
C. Restart the server manually after every update
D. Disable all routes temporarily during deployment
A. Using cluster.isMaster instead of cluster.isWorker
B. Missing code to restart worker on exit
C. Express app should be created in master process
D. app.listen should be outside else block
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 B
Quick Check:
Exit handler must fork new worker [OK]
Hint: Restart workers on exit to avoid downtime [OK]
Common Mistakes:
Not restarting workers after exit
Confusing master and worker roles
Placing app.listen in wrong process
5. You want to deploy a new version of your Express app without downtime. Which approach best achieves this?
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 immediately
Which steps should you follow?
hard
A. Steps 1 and 4 only
B. Only step 4
C. Steps 1, 2, and 3
D. Steps 2 and 4 only
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 C
Quick Check:
Fork new workers and gracefully disconnect old [OK]
Hint: Fork new workers before disconnecting old ones [OK]
Common Mistakes:
Restarting server immediately causing downtime
Not gracefully disconnecting old workers
Skipping multiple worker setup
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.