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
Recall & Review
beginner
What is a graceful shutdown in Node.js?
A graceful shutdown means stopping the Node.js app carefully by finishing ongoing tasks and closing resources before exiting, so no data is lost or corrupted.
Click to reveal answer
beginner
Why should you handle errors during shutdown in Node.js?
Handling errors during shutdown helps avoid crashes and ensures the app cleans up resources like database connections or servers properly before stopping.
Click to reveal answer
intermediate
Which Node.js events are commonly used to trigger graceful shutdown?
The 'SIGINT' and 'SIGTERM' signals are used to start graceful shutdown, and 'uncaughtException' or 'unhandledRejection' events handle unexpected errors.
Click to reveal answer
intermediate
What is the role of process.exit() in graceful shutdown?
process.exit() stops the Node.js process. In graceful shutdown, it is called only after all cleanup tasks finish to avoid abrupt termination.
Click to reveal answer
intermediate
How can you ensure database connections close during a graceful shutdown?
You call the database client's close or disconnect method inside the shutdown handler to close connections before the app exits.
Click to reveal answer
Which event should you listen to for catching unexpected errors in Node.js?
A'close'
B'exit'
C'SIGINT'
D'uncaughtException'
✗ Incorrect
The 'uncaughtException' event catches errors not handled anywhere else in the code.
What does a graceful shutdown NOT do?
AFinish ongoing requests
BClose open resources
CImmediately kill the process
DLog shutdown steps
✗ Incorrect
Graceful shutdown avoids immediately killing the process to prevent data loss.
Which signal is commonly sent to a Node.js app to request shutdown?
ASIGINT
BSIGKILL
CSIGSTOP
DSIGCONT
✗ Incorrect
SIGINT is sent when you press Ctrl+C to stop the app.
What should you do before calling process.exit() during shutdown?
AIgnore errors
BClose all open connections
CRestart the server
DDo nothing
✗ Incorrect
Closing connections ensures no data is lost before exiting.
How can you handle promise rejections during shutdown?
AListen to 'unhandledRejection' event
BUse try-catch only
CIgnore them
DRestart the app immediately
✗ Incorrect
'unhandledRejection' catches promises that fail without a catch.
Explain how to implement a graceful shutdown in a Node.js app when an error occurs.
Think about stopping the app carefully without losing data.
You got /4 concepts.
Why is it important to handle errors during the shutdown process in Node.js?
Consider what happens if the app stops suddenly.
You got /4 concepts.
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.