Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Graceful shutdown on errors
📖 Scenario: You are building a simple Node.js server that needs to close resources properly when errors happen or when the server stops. This helps avoid problems like data loss or corrupted files.
🎯 Goal: Create a Node.js server that listens on port 3000 and shuts down gracefully when an error occurs or when the process receives a termination signal.
📋 What You'll Learn
Create a basic HTTP server using Node.js http module
Add an error event listener on the server to handle runtime errors
Add a function called gracefulShutdown to close the server and exit the process
Listen for SIGINT and SIGTERM signals to trigger graceful shutdown
💡 Why This Matters
🌍 Real World
Servers often need to close open connections and clean up resources before stopping. This prevents data loss and keeps systems stable.
💼 Career
Understanding graceful shutdown is important for backend developers and DevOps engineers to build reliable and maintainable server applications.
Progress0 / 4 steps
1
Create a basic HTTP server
Create a variable called http that requires the http module. Then create a server using http.createServer() that responds with 'Hello World' to every request. Finally, make the server listen on port 3000.
Node.js
Hint
Use require('http') to import the module. Then use http.createServer to create the server. Use server.listen(3000) to start listening.
2
Add error event listener
Add an event listener on the server for the 'error' event. The listener should call a function named gracefulShutdown passing the error object.
Node.js
Hint
Use server.on('error', (err) => { ... }) to listen for errors and call gracefulShutdown(err).
3
Create gracefulShutdown function
Create a function named gracefulShutdown that takes an error parameter. Inside, log the error message using console.error. Then close the server using server.close() and exit the process with code 1 inside the close callback.
Node.js
Hint
Define function gracefulShutdown(error) { ... }. Use console.error to log the error message. Call server.close() with a callback that calls process.exit(1).
4
Handle termination signals
Add listeners for 'SIGINT' and 'SIGTERM' signals on process. Both listeners should call gracefulShutdown with a new Error object with the message 'Process terminated'.
Node.js
Hint
Use process.on('SIGINT', () => { ... }) and process.on('SIGTERM', () => { ... }) to listen for termination signals. Call gracefulShutdown(new Error('Process terminated')) inside each.
Practice
(1/5)
1. What is the main purpose of implementing a graceful shutdown in a Node.js application?
easy
A. To restart the server automatically after an error
B. To log all incoming requests for debugging
C. To increase the server's performance under load
D. To stop accepting new requests and clean up resources before exiting
Solution
Step 1: Understand graceful shutdown concept
Graceful shutdown means stopping new work and cleaning up before the app exits.
Step 2: Identify the main goal
The goal is to avoid abrupt termination by closing servers and resources properly.
Final Answer:
To stop accepting new requests and clean up resources before exiting -> Option D
Quick Check:
Graceful shutdown = stop new work + cleanup [OK]
Hint: Remember: graceful shutdown means clean exit, not restart [OK]
Common Mistakes:
Confusing graceful shutdown with automatic restart
Thinking it improves performance directly
Assuming it only logs requests
2. Which of the following is the correct way to listen for an uncaught exception to trigger graceful shutdown in Node.js?
easy
A. process.on('uncaughtException', handler)
B. process.addListener('error', handler)
C. process.listen('uncaughtException', handler)
D. process.catch('uncaughtException', handler)
Solution
Step 1: Recall Node.js event listening syntax
Node.js uses process.on(event, handler) to listen for events.
Step 2: Identify the correct event for uncaught exceptions
The event name is 'uncaughtException', so process.on('uncaughtException', handler) is correct.
Final Answer:
process.on('uncaughtException', handler) -> Option A
Quick Check:
Use process.on for events like uncaughtException [OK]
Hint: Use process.on(event, handler) for error events [OK]
Common Mistakes:
Using process.catch instead of process.on
Using wrong event names like 'error' for uncaughtException
Confusing listen with on
3. Consider this Node.js code snippet handling graceful shutdown:
C. It exits immediately without closing the server
D. It uses the wrong event name for errors
Solution
Step 1: Analyze error handling behavior
The handler logs the error but calls process.exit(1) immediately.
Step 2: Check for graceful shutdown steps
It does not close the server before exiting, which can cause abrupt termination.
Final Answer:
It exits immediately without closing the server -> Option C
Quick Check:
Graceful shutdown requires closing server before exit [OK]
Hint: Always close servers before calling process.exit [OK]
Common Mistakes:
Exiting without cleanup
Ignoring server.close in error handlers
Assuming logging is enough
5. You want to implement a graceful shutdown that handles both SIGTERM and uncaughtException events, ensuring the server closes before exit. Which code snippet correctly achieves this?
process.on('SIGTERM', () => { server.close(() => process.exit(0)); }); process.on('uncaughtException', (err) => { console.error(err); server.close(() => process.exit(1)); }); closes the server and then exits, which is correct for graceful shutdown.
Step 2: Check handling of uncaughtException
process.on('SIGTERM', () => { server.close(() => process.exit(0)); }); process.on('uncaughtException', (err) => { console.error(err); server.close(() => process.exit(1)); }); logs the error, closes the server, then exits with error code, ensuring cleanup.