What Is Connection Draining? Stop New Traffic and Finish In-Flight Requests During Shutdown
Connection draining is the process of removing a server or instance from new traffic before replacement, scale-down, or shutdown, while giving work already accepted a bounded time to finish. Sending a process a termination signal alone does not ensure that the load balancer stops routing to it or that the application finishes its requests. Draining coordinates the traffic layer, application, and orchestrator against one shutdown deadline.
Connection draining is the process of removing a server or instance from new traffic before replacement, scale-down, or shutdown, while giving work already accepted a bounded time to finish. Sending a process a termination signal alone does not ensure that the load balancer stops routing to it or that the application finishes its requests. Draining coordinates the traffic layer, application, and orchestrator against one shutdown deadline.
Why shut connections down in stages?
If an instance disappears immediately during deployment or scale-down, clients or load balancers may still send requests to it, causing failures. If it keeps accepting new requests, shutdown may never finish or active work may be cut off. Draining separates the two jobs: first direct new requests to healthy instances, then let accepted requests finish or stop them safely.
“Drain connections” does not mean immediately sever every network connection. HTTP/1.1 can use Connection: close so the connection closes after a response. HTTP/2 can use GOAWAY to announce that no new streams will be accepted while existing streams finish. WebSockets, gRPC streams, and long-running jobs may outlive ordinary requests and need their own termination policy.
Shutdown sequence: separate routing removal from work completion
The exact order and signals depend on the load balancer, proxy, framework, and orchestrator.
| Stage | Action | Check |
|---|---|---|
| 1. Begin shutdown | Deployment system marks the instance for removal and signals the app | Does the app receive the signal and run its shutdown hook? |
| 2. Remove from new traffic | Fail readiness or request draining from the load balancer/proxy | Is the target actually removed from normal routing? How long does propagation take? |
| 3. Keep accepted work alive | Let existing requests/streams finish; reject or redirect new connections/streams | How are keep-alive, HTTP/2 multiplexing, and WebSockets handled? |
| 4. Wait with a deadline | Observe active connections/requests and enforce a drain timeout | Does the grace period cover the longest permitted work? |
| 5. Exit or force cleanup | Exit normally when work completes; on timeout, cancel/force stop and record outcome |
Removing a target from routing and closing its connections are separate steps. In Kubernetes, a terminating Pod’s Endpoint is not removed immediately; it exposes a terminating state. Such an Endpoint has ready: false, while serving can indicate whether it is still serving traffic. The kubelet runs preStop before sending the container process a termination signal. If terminationGracePeriodSeconds expires while the process remains, Kubernetes can force it to stop. The application drain and hook time must fit within this grace period.
Product examples: NGINX and Envoy
Draining means and controls differ by product. In NGINX, sending QUIT to the master starts graceful shutdown: workers stop accepting new connections, finish existing clients, and exit. HUP starts new workers and asks old workers to shut down gracefully after reloading configuration. TERM and INT request fast shutdown, so choose signals for the intended behavior.
Envoy’s HTTP connection manager can signal that new work should stop in a protocol-specific way: Connection: close for HTTP/1, GOAWAY for HTTP/2, and connection close after requests complete. Listener draining also depends on server options and filter support. Calling a proxy’s drain API does not automatically complete all application work.
These are product-scoped examples. For a real deployment, check the documentation for the deployed version, termination signal, active-request counter, maximum drain time, and load balancer target-removal behavior.
Drain deadlines and long-lived connections
A drain timeout is an upper bound the system can tolerate, not an arbitrary large number. Kubernetes documents a default Pod termination grace period of 30 seconds, and a preStop hook runs within that budget. Choose settings using the maximum application request duration, proxy drain time, target-state propagation delay, and hook time. Increasing the grace period alone does not resolve endless work or disconnected clients.
Long HTTP requests, downloads, gRPC streams, WebSockets, and message-consumer jobs need more careful policies than ordinary request-response work. For example, HTTP/2 GOAWAY can prevent new streams, but when existing streams finish depends on the application and client. For streaming work, consider delivering the shutdown signal, allowing a bounded completion window, then checkpointing or dividing work into resumable units. Make retryable work idempotent because retries can duplicate execution.
Implementation and verification
**Identify the target and traffic path.** Find which cloud load balancer, service discovery, Kubernetes Service/EndpointSlice, or proxy actually selects destinations.
**Define the app’s shutdown signal.** On receipt, lower readiness and stop accepting new work while tracking existing work. Do not mark the app ready before initialization is complete.
**Coordinate layer ordering.** Target-removal propagation can be delayed. Align when the app stops accepting work with when the load balancer changes routing. Do not assume a fixed `sleep` proves propagation completed; verify state or telemetry.
**Set a maximum wait and timeout behavior.** Align request limits, drain timeout, and platform grace period. At the deadline, cancel cancellable work, safely hand off retryable work, and prevent indefinite waiting.
**Observe in-flight connections.** Record active requests/connections, signal receipt, rejected new requests, drain duration, and forced exits. Do not log sensitive request data or credentials.
**Verify representative shutdowns.** Deploy or scale down while sending requests. Confirm new requests go elsewhere and existing requests finish. Test long requests, connection reuse, streams, and WebSockets separately.
**Prepare recovery.** If draining stalls, inspect remaining work and the deadline; slow the rollout or keep capacity if needed. Force termination can interrupt work, so record impact and reprocessing steps.
Passing is not just “the termination command succeeded.” Confirm the target left new routing, allowed in-flight work finished or followed the stated deadline/cancellation policy, and remaining instances did not experience a sharp rise in errors or saturation.
Common failure modes
**Readiness fails but the app exits immediately:** the load balancer may not have applied the change yet. Coordinate propagation delay and process exit.
**The shutdown hook consumes the full grace period:** hook and app draining can share the same shutdown budget. Calculate total platform grace and per-stage timeouts together.
**Exit waits only for zero connections:** a long-lived connection may stall, or a counter may not include streams. Consider the deadline, request state, and application work state.
**Relying on client retries:** the client may not know whether a disconnected request was already processed. Make retries safe with idempotency keys or deduplication.
**Unbounded keep-alive or streams:** existing connections can stay open even when no new work is accepted. Set protocol-specific drain signals and maximum idle/request durations.
Sources and scope
Kubernetes, [Pod termination](https://kubernetes.io/docs/concepts/workloads/pods/pod-termination/)
Kubernetes, [Explore Pod and endpoint termination behavior](https://kubernetes.io/docs/tutorials/services/pods-and-endpoint-termination-flow/)
NGINX, [Controlling nginx](https://nginx.org/en/docs/control.html)
Envoy, [Draining](https://www.envoyproxy.io/docs/envoy/v1.40.2/intro/arch_overview/operations/draining)
Envoy Gateway, [Graceful Shutdown and Hitless Upgrades](https://gateway.envoyproxy.io/latest/tasks/operations/graceful-shutdown/)
This article explains the common goal and sequence of connection draining, then separates documented Kubernetes, NGINX, and Envoy Gateway behavior as product examples. Verify Kubernetes grace periods and endpoint state, and product shutdown behavior against the deployed version and settings. No example configuration was applied and no live cluster or proxy was tested.