Graceful Shutdown: Deploying Without Dropping Requests
Every deploy restarts your service. If it simply dies when it receives SIGTERM, in-flight requests fail, queue messages are processed twice and users see errors. Graceful shutdown makes deploys invisible.
The sequence
- Receive SIGTERM. Orchestrators such as Kubernetes send SIGTERM, wait a grace period (30 seconds by default), then send SIGKILL. Your process must handle the signal instead of exiting immediately.
- Stop receiving new work. Fail the readiness check so the load balancer removes the instance. Because endpoint removal propagates with a delay, keep serving for a few seconds (a short pre-stop sleep) before closing the listener.
- Finish in-flight work. Let active HTTP requests complete, with a deadline shorter than the grace period. Stop pulling new queue messages, finish the current ones and acknowledge them; anything unfinished should be left unacknowledged so another worker picks it up.
- Release resources. Flush logs, metrics and buffered writes, close database pools, release leases and locks so other nodes do not wait for a timeout.
- Exit with status 0 before the grace period ends.
Common mistakes
- Running the app under a shell script that does not forward signals, so SIGTERM never reaches it.
- Long-lived connections (WebSockets, gRPC streams) that never close; send a close or GOAWAY frame and let clients reconnect elsewhere.
- Background jobs with no checkpoint, so a long job restarts from zero after each deploy. Make jobs idempotent and resumable.
- A grace period shorter than the longest request.
Interview tip
When discussing deployments or autoscaling, mention connection draining and idempotent consumers. It shows you have run services in production, not just designed them.
Written by the Linkjob AI team. Linkjob AI is an AI interview copilot for job seekers: Linkjob AI