Graceful shutdown during deploys: how do you make sure in-flight requests finish?
During deploys we saw a small spike of failed requests every time. The old instances were being killed while still handling work.
What fixed most of it for us:
- Handle SIGTERM and stop accepting new connections
- Wait for in-flight requests to finish, with a timeout
- Make sure the load balancer stops routing to the instance before the process exits
The ordering of that last step still feels fiddly. How do you handle shutdown in your services?
Comments · 3
The ordering problem is real because deregistration from the load balancer is asynchronous. A common approach is to fail the health check first, sleep for a short drain period, and only then stop accepting connections and finish in-flight work.
Set the grace period longer than your slowest normal request. We had a default of 30 seconds and some background jobs took longer, so they kept getting cut off.
Handle the signal, stop accepting, wait for in-flight work, then exit. And test it by actually deploying under load.