Skip to content

Graceful Shutdown

Medium — good to knowBackend

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."

Made with passive-aggressive love by manoga.digital. Powered by Claude.