Graceful Shutdown and In-Flight Request Draining
Handle SIGTERM to drain connections, finish work, and close resources cleanly during deploys.
Why Graceful Shutdown Matters
During a deploy, your orchestrator (Kubernetes, ECS, systemd) sends your process a signal and gives it a short window before forcibly killing it. If you exit immediately, you drop in-flight requests, abort half-written database transactions, and return broken responses to users.
A graceful shutdown means:
- Stop accepting new connections.
- Let in-flight requests finish (drain).
- Close resources cleanly: DB pools, message brokers, timers.
- Exit with code
0before the kill timer fires.
This lesson builds a production-grade shutdown path step by step.
The Signals: SIGTERM vs SIGINT vs SIGKILL
Process managers communicate shutdown intent via POSIX signals. You must handle the right ones:
SIGTERM— the polite "please stop" sent by Kubernetes, Docker, and systemd during a rollout. This is the one you handle.SIGINT— sent byCtrl+Cin a terminal. Handle it too for local dev parity.SIGKILL(signal 9) — cannot be caught, blocked, or ignored. The OS terminates you instantly. This is what fires if you miss the grace window.
Node lets you register listeners for catchable signals on process.
process.on('SIGTERM', () => {
console.log('Received SIGTERM, starting graceful shutdown');
shutdown();
});
process.on('SIGINT', () => {
console.log('Received SIGINT, starting graceful shutdown');
shutdown();
});
function shutdown() {
// close servers, drain requests, release resources
}All lessons in this course
- Timeouts, Retries, and Exponential Backoff with Jitter
- The Circuit Breaker Pattern and Bulkhead Isolation
- Graceful Shutdown and In-Flight Request Draining
- Health Checks, Readiness Probes, and Load Shedding