Inferensys

Glossary

Connection Draining

Connection draining is a load balancing technique that stops new requests to a backend instance being taken out of service while allowing existing connections to complete.
Knowledge manager reviewing enterprise knowledge management system on laptop, document library visible, casual office.
LOAD BALANCING ALGORITHMS

What is Connection Draining?

Connection draining, also known as deregistration delay, is a critical process in load balancing for ensuring graceful service termination.

Connection draining is the process by which a load balancer stops sending new requests to a backend instance that is being taken out of service, while allowing existing, in-flight connections to complete naturally. This mechanism, also called deregistration delay, is essential for performing maintenance, deploying updates, or scaling down a fleet without abruptly terminating active user sessions or transactions. It prevents client errors and data corruption by giving ongoing requests time to finish processing.

In a heterogeneous fleet orchestration context, such as managing mixed fleets of autonomous mobile robots and manual vehicles, connection draining principles apply to dynamic task allocation and agent decommissioning. The load balancer or orchestration middleware uses health checks to identify an instance for removal, initiates draining, and only fully terminates the instance after the configured delay or when all active connections close. This ensures zero-downtime deployments and maintains system reliability during fleet reconfiguration.

LOAD BALANCING ALGORITHMS

Key Features of Connection Draining

Connection draining, also known as deregistration delay, is a graceful termination process used by load balancers. It ensures operational stability by allowing in-flight requests to complete before a backend instance is fully removed from service.

01

Graceful Deregistration

Connection draining is the controlled process by which a load balancer marks a backend instance as deregistering or draining. The instance stops receiving new connections but continues to process all existing, active connections until they complete or a timeout is reached. This prevents abrupt termination of user sessions, file uploads, or database transactions, ensuring a seamless experience during deployments, scaling-in events, or instance maintenance.

  • Core Mechanism: The load balancer's health check status for the instance changes from healthy to draining.
  • Traffic Flow: New requests are routed to other healthy instances in the pool.
  • Session Integrity: Long-lived connections like WebSockets or streaming sessions are allowed to conclude naturally.
02

Configurable Drain Timeout

The drain timeout (or deregistration delay) is a critical, user-defined parameter that sets a maximum duration for the draining process. It acts as a safety mechanism to prevent instances from hanging indefinitely due to stuck connections.

  • Typical Range: Timeouts are configurable, often from 1 second to 3600 seconds (1 hour).
  • Timeout Behavior: If active connections persist beyond the timeout, the load balancer forcibly terminates them and removes the instance.
  • Use Case Tuning: A short timeout (e.g., 30s) is suitable for stateless APIs. Long-lived stateful services (e.g., video streaming) may require a timeout of several minutes.
  • Example: AWS Elastic Load Balancing allows a deregistration delay configurable between 0 and 3600 seconds.
03

Integration with Deployment Strategies

Connection draining is a foundational enabler for modern, zero-downtime deployment methodologies. It works in concert with orchestration platforms to ensure service continuity during updates.

  • Blue-Green Deployments: Draining allows the 'blue' (old) environment to gracefully finish requests while traffic is shifted entirely to the 'green' (new) environment.
  • Canary Releases: When a canary instance (running new code) is deemed unhealthy and needs removal, draining ensures its active users are not abruptly disconnected.
  • Auto-Scaling Scale-In: When a cloud auto-scaling group removes an instance to reduce capacity, connection draining is triggered to prevent dropped requests.
  • Orchestration Handshake: Platforms like Kubernetes use a termination grace period which coordinates with the service mesh or ingress controller's draining logic.
04

Layer 4 vs. Layer 7 Draining Behavior

The implementation and granularity of connection draining differ significantly between Layer 4 (Transport) and Layer 7 (Application) load balancers, due to their fundamental operating models.

  • Layer 4 (TCP/UDP) Draining:

    • Operates on connections. The load balancer stops establishing new TCP connections to the draining instance.
    • Existing TCP connections remain open until closed by the client/server or the drain timeout expires.
    • Less granular; cannot distinguish between different HTTP requests on the same persistent connection.
  • Layer 7 (HTTP/HTTPS) Draining:

    • Operates on requests. The load balancer stops sending new HTTP requests to the draining instance.
    • Can allow existing requests to complete while rejecting new ones on the same persistent HTTP connection.
    • More intelligent, enabling true request-level graceful shutdown for HTTP/1.1 and HTTP/2.
05

Health Check Coordination

Connection draining requires careful coordination with the load balancer's health check system to avoid conflicting signals and ensure traffic is routed correctly during the drain phase.

  • Dual Status: An instance has two logical statuses: its health state (healthy/unhealthy) and its registration state (registered/draining/deregistered).
  • Failed Health Checks During Drain: If an instance fails a health check while draining, most systems will immediately terminate all connections and deregister the instance, overriding the drain timeout. This prevents a failing instance from prolonging its own shutdown.
  • Proactive Draining Initiation: Draining is typically initiated by an external orchestration command (e.g., a deployment tool, scaling policy) rather than being triggered automatically by a failed health check.
06

Application-Level Draining Signals

For maximum gracefulness, applications should be designed to respond to draining signals from the infrastructure. This allows for in-memory state cleanup, final log flushing, and notification to upstream dependencies before the process terminates.

  • SIGTERM Signal: In containerized environments (Docker, Kubernetes), the orchestrator sends a SIGTERM signal to the application process when a pod is scheduled for termination, initiating the drain phase.
  • Application Responsibilities:
    • Upon receiving SIGTERM, the app should stop accepting new work from its queue.
    • Complete processing of current work items.
    • Close listeners and database connections cleanly.
    • Exit with code 0.
  • Readiness Probes: Applications can expose a /health/ready endpoint that changes to an 'unready' status when draining begins, providing a signal to the load balancer or service mesh to stop sending traffic.
LOAD BALANCER DRAIN STRATEGIES

Connection Draining vs. Immediate Deregistration

Comparison of strategies for removing a backend instance from a load balancer's pool, focusing on request completion and client impact.

Feature / MetricConnection DrainingImmediate Deregistration

Primary Objective

Gracefully complete in-flight requests

Fastest possible instance removal

New Request Handling

Stops sending new requests immediately

Stops sending new requests immediately

Existing Connection Handling

Allows active connections to complete naturally

Terminates all active connections abruptly

Client Experience Impact

Zero failed requests for active sessions

All in-flight requests fail (TCP RST/connection error)

Typical Use Case

Planned maintenance, deployments, scaling-in

Emergency failover, instance failure, security incident

Deregistration Delay Configurable

Yes (e.g., 0 to 3600 seconds)

No (effectively 0 seconds)

Load Balancer State During Process

Instance in 'draining' or 'deregistering' state

Instance removed from pool instantly

Suitable for Stateful Sessions

Yes (preserves session completion)

No (causes session disruption)

CONNECTION DRAINING

Frequently Asked Questions

Connection draining is a critical process for maintaining service availability during infrastructure updates. These questions address its core mechanisms, configuration, and role in modern orchestration.

Connection draining (also known as deregistration delay or graceful shutdown) is a load balancing process that stops sending new requests to a backend instance scheduled for removal while allowing existing, in-flight connections to complete naturally. It works by first marking the instance as draining or unhealthy for new traffic in the load balancer's routing table. The load balancer continues to honor and forward responses for requests already being processed by that instance but rejects or redirects any new connection attempts to other healthy instances in the pool. This ensures active sessions, such as file uploads, database transactions, or streaming responses, can finish without interruption before the instance is terminated or updated.

Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.