Sub topic 9
Connection draining and graceful deploys
This chapter assumes you know what a load balancer does, how health checks mark a backend as up or down, and how to read a basic NGINX or HAProxy configuration. It covers how to take a backend out of rotation without dropping the requests it is serving.
The problem: removing a server abruptly
Suppose you deploy a new version to 10.0.0.12. If you stop the process while clients are mid-request, those clients see connection resets or 502 errors. Health checks do not prevent this. A health check notices a failure after it happens, and by then users have already been affected.
Connection draining (also called deregistration delay) fixes this. The load balancer stops sending new connections to a backend but lets existing ones finish. Once they finish, or a timeout expires, the backend can be stopped safely.
The drain sequence
A graceful removal has four steps:
- Mark the backend as draining, so the balancer sends it no new requests.
- Wait while in-flight requests complete.
- Stop or restart the backend once its connection count reaches zero, or when the drain timeout expires.
- Deploy, start the new version, pass health checks, and return the backend to rotation.
Draining in HAProxy
HAProxy exposes server states through its runtime API. The drain state stops new sessions but keeps existing ones. Persistent clients that match a cookie can still be routed to a draining server, which is useful for sticky sessions.
# Requires: stats socket /var/run/haproxy.sock mode 600 level admin
echo "set server app/app1 state drain" | socat stdio /var/run/haproxy.sock
# Check the current session count for the server
echo "show stat" | socat stdio /var/run/haproxy.sock | cut -d, -f1,2,5
# After the count reaches 0, deploy, then bring it back
echo "set server app/app1 state ready" | socat stdio /var/run/haproxy.sockThe state maint setting is stricter. It removes the server immediately and also disables its health checks.
Draining in NGINX
Open source NGINX has no runtime drain command. The usual approach is to mark the server down in the upstream block and reload the configuration:
upstream app {
least_conn;
server 10.0.0.11:8080;
server 10.0.0.12:8080 down;
}Running nginx -s reload starts new worker processes with the new configuration. The old workers stop accepting new connections and exit after they finish their current requests. This is effectively a graceful drain. You can limit how long old workers linger with worker_shutdown_timeout. NGINX Plus adds a drain parameter and an API for changing server state without a reload.
Cloud load balancers
Managed load balancers provide the same behavior as a setting. AWS target groups call it the deregistration delay, which is 300 seconds by default. When you deregister a target, it moves to a draining state. Set the delay a little longer than your slowest normal request. A 300-second default is often too long for short API calls, because deploys wait for the full period.
Order of operations in a rolling deploy
A rolling deploy updates one backend, or a small batch, at a time. For each one, do the following:
- Drain it first, and then wait for connections to finish.
- Give the application its own graceful shutdown. On
SIGTERM, it should stop accepting new work, finish active requests, and then exit. - Make the new version report healthy only when it is ready, for example after caches and database pools are warm.
- Keep enough capacity in the remaining servers. If you drain half the pool while load is high, the rest can overload.
Common errors
- Timeout shorter than requests. Long downloads or WebSocket connections are cut off when the drain timeout expires. Set the timeout to match your longest legitimate connection, or accept that those clients reconnect.
- Stopping the process before draining finishes. Scripts that restart the service immediately negate the drain.
- Ignoring keep-alive connections. A client holding an idle keep-alive connection can still send a request on it. Make sure the backend closes idle connections when it shuts down.
- Skipping the readiness check. Returning a backend to rotation before it can serve requests causes errors right after the deploy.
Exercise
During a rolling deploy, users see a burst of 502 errors each time a backend restarts. The deploy script runs systemctl restart app on each server, and the health check runs every 10 seconds. What two changes would you make?
Show answer
First, drain the server at the load balancer (for example set server app/app1 state drain in HAProxy) and wait for its active connections to reach zero before restarting. The health check cannot help here, because it detects a failure only after requests have already been lost. Second, make the application handle SIGTERM gracefully, and have it report healthy only once it is ready, so the server rejoins rotation after the restart without receiving traffic too early.