Graceful Shutdown
ELI5 — The Vibe Check
Graceful shutdown is when your server stops accepting new requests but finishes all the in-flight ones before dying. It's like a store announcing 'we're closing in 5 minutes' instead of throwing everyone out mid-purchase. Essential for zero-downtime deployments.
Real Talk
Graceful shutdown is the process of cleanly terminating a server by stopping new connection acceptance, completing in-progress requests, closing database connections, flushing buffers, and deregistering from service discovery. It's triggered by OS signals (SIGTERM, SIGINT) and is critical for container orchestration, rolling deployments, and data integrity.
Show Me The Code
process.on('SIGTERM', async () => {
console.log('Shutting down gracefully...');
server.close(async () => {
await db.disconnect();
await cache.quit();
process.exit(0);
});
setTimeout(() => process.exit(1), 30000); // force after 30s
});
When You'll Hear This
"Kubernetes sends SIGTERM 30 seconds before killing the pod — handle graceful shutdown." / "Our deploy had 502 errors because we weren't doing graceful shutdown."
Related Terms
Container
A container is a running instance of a Docker image — it's the lunchbox you made and actually opened to eat from.
Health Endpoint
A health endpoint is a simple URL (usually /health or /healthz) that returns 'I'm alive and everything's fine' or 'something's broken.