Graceful shutdown means closing the server and cleaning up resources properly before the app stops, so no requests are lost or data corrupted.
Click to reveal answer
beginner
Which Node.js event is commonly used to detect when the process should shut down?
The 'SIGINT' signal is often used to detect when the user presses Ctrl+C to stop the process.
Click to reveal answer
intermediate
Why should you stop accepting new requests during graceful shutdown?
Stopping new requests prevents the server from starting work it can't finish before shutting down, avoiding errors or lost data.
Click to reveal answer
intermediate
How can you ensure all ongoing requests finish before the server closes?
You wait for all current requests to complete, often by tracking active connections and closing the server only after they finish.
Click to reveal answer
intermediate
What is a common method to clean up resources during shutdown in Node.js?
Closing database connections, clearing timers, and releasing file handles are common cleanup steps during shutdown.
Click to reveal answer
Which signal is typically used to trigger graceful shutdown in a Node.js app?
ASIGCONT
BSIGKILL
CSIGSTOP
DSIGINT
✗ Incorrect
SIGINT is sent when you press Ctrl+C and is commonly used to start graceful shutdown.
What should a server do first when starting graceful shutdown?
AStop accepting new requests
BRestart the server
CExit the process
DClose database connections immediately
✗ Incorrect
Stopping new requests prevents new work from starting during shutdown.
Why is it important to wait for ongoing requests to finish before shutdown?
ATo restart the server faster
BTo avoid losing data or breaking client connections
CTo speed up shutdown
DTo free memory immediately
✗ Incorrect
Waiting ensures requests complete properly without errors.
Which Node.js method helps to stop the server from accepting new connections?
Aserver.close()
Bserver.listen()
Cprocess.exit()
Dserver.open()
✗ Incorrect
server.close() stops new connections but lets existing ones finish.
What is NOT a typical cleanup task during graceful shutdown?
AClosing database connections
BClearing timers
COpening new network sockets
DReleasing file handles
✗ Incorrect
Opening new sockets is not part of shutdown; it would start new work.
Explain the steps to implement graceful shutdown in a Node.js server.
Think about what happens from signal to process exit.
You got /5 concepts.
Why is graceful shutdown important for a Node.js application?
Consider what happens if the app stops suddenly.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of graceful shutdown in a Node.js application?
easy
A. To stop the app safely without losing data or leaving tasks unfinished
B. To restart the app automatically after a crash
C. To speed up the app startup time
D. To increase the app's memory usage
Solution
Step 1: Understand graceful shutdown concept
Graceful shutdown means stopping the app carefully so it finishes ongoing work and saves data.
Step 2: Identify the purpose in options
Only To stop the app safely without losing data or leaving tasks unfinished describes stopping safely without losing data or leaving tasks unfinished.
Final Answer:
To stop the app safely without losing data or leaving tasks unfinished -> Option A
Quick Check:
Graceful shutdown = safe stop without data loss [OK]
Hint: Graceful shutdown means stopping safely, not restarting or speeding up [OK]
Common Mistakes:
Confusing graceful shutdown with app restart
Thinking it improves startup speed
Assuming it increases memory usage
2. Which Node.js code snippet correctly listens for the SIGINT signal to start graceful shutdown?
easy
A. process.on('SIGTERM', () => { console.log('Shutting down'); });
B. process.listen('SIGINT', () => { console.log('Shutting down'); });
C. process.on('SIGINT', () => { console.log('Shutting down'); process.exit(); });
D. process.signal('SIGINT', () => { console.log('Shutting down'); });
Solution
Step 1: Recall correct event listener syntax
Node.js uses process.on('signal', callback) to listen for signals like SIGINT.
Step 2: Check options for correct syntax and signal
process.on('SIGINT', () => { console.log('Shutting down'); process.exit(); }); uses process.on with 'SIGINT' and a callback, which is correct. process.on('SIGTERM', () => { console.log('Shutting down'); }); uses SIGTERM, which is valid but not SIGINT as asked. Options B and D use invalid methods.
A. process.exit(0) runs before server.close finishes, causing abrupt exit
B. SIGINT event is not supported by process
C. console.log should be before server.close
D. server.close() does not exist on http server
Solution
Step 1: Understand asynchronous server.close
server.close() is asynchronous and takes a callback to run after closing.
Step 2: Identify order of commands
Here, process.exit(0) runs immediately after server.close() call, not waiting for close to finish, causing abrupt exit.
Final Answer:
process.exit(0) runs before server.close finishes, causing abrupt exit -> Option A
Quick Check:
Wait for server.close callback before exiting [OK]
Hint: Always exit inside server.close callback to wait for close [OK]
Common Mistakes:
Calling process.exit too early
Thinking SIGINT is unsupported
Misunderstanding server.close existence
5. You want to handle both SIGINT and SIGTERM signals to gracefully shutdown your Node.js app. Which code correctly ensures the server closes once and exits safely without duplicate calls?
D. let isShuttingDown = false;
process.on('SIGINT', shutdown);
process.on('SIGTERM', shutdown);
function shutdown() {
if (isShuttingDown) return;
isShuttingDown = true;
server.close(() => {
console.log('Closed');
process.exit(0);
});
}
Solution
Step 1: Understand multiple signal handling
Both SIGINT and SIGTERM can trigger shutdown, so we must avoid running shutdown twice.
Step 2: Check for duplicate shutdown prevention
let isShuttingDown = false;
process.on('SIGINT', shutdown);
process.on('SIGTERM', shutdown);
function shutdown() {
if (isShuttingDown) return;
isShuttingDown = true;
server.close(() => {
console.log('Closed');
process.exit(0);
});
} uses a flag isShuttingDown to run shutdown only once, preventing duplicate server.close and exit calls.