Load Balancing Fundamentals Series

🚦 Safely Removing a Server From a Load Balancer — Understanding Connection Draining (DRAIN) vs MAINT From a Top 1% Perspective

Introduction

This article is part of the Top 1% Series Complete Article Guide, and article 4.1 in the Load Balancing Fundamentals Series.

Prerequisite Knowledge

Getting the Big Picture

Many people’s first instinct, for removing a backend server from a load balancer’s rotation, is deleting that server’s entry from the config file and reloading. But with this method, any request that server is still processing, that hasn’t returned a response yet, gets forcibly cut off without any regard for its state. From the user’s perspective, a request that should still have been in progress suddenly fails. Connection draining (DRAIN) exists precisely to avoid this problem. DRAIN is a state combining two distinct behaviors: “don’t send new connections to this server” and “let the exchange on an already-established connection keep going until it naturally finishes.”

graph TB
    subgraph MAINT["MAINT State"]
        A1["New connections: not sent"] 
        A2["Existing connections: cut off immediately"]
    end
    subgraph DRAIN["DRAIN State"]
        B1["New connections: not sent"]
        B2["Existing connections: continue until they naturally finish"]
    end

Deep Dive Into the Fundamentals

The MAINT State: Cuts Off Immediately, Without Regard for State

The MAINT (maintenance) state stops health checks to that server, and immediately blocks both new and existing connections alike, with no exception. This is the right choice for an emergency scenario, where a hardware failure occurred and you need to stop all traffic to that server right now — but using it for planned maintenance or a deployment forces every in-flight request to fail.

The DRAIN State: Stops Only New Connections, Watches Existing Ones Through to Completion

The DRAIN state excludes that server from routing candidacy and stops sending any new connections to it at all, but for clients already connected to that server, lets the exchange on that connection keep going until it naturally finishes. In HAProxy, you set the DRAIN state via the Runtime API (the stats socket), like this:

# Set the target server to the DRAIN state (via the stats socket)
echo "set server backend_name/server_name state drain" | socat stdio /var/run/haproxy.sock

# Check the current session count, and wait until it reaches 0
echo "show stat" | socat stdio /var/run/haproxy.sock | grep server_name

By strictly following the order of confirming the session count has reached 0 before actually stopping that server or putting it into maintenance, you can achieve a safe removal without losing a single in-flight request.

Why "Edit the Config File and Reload" Isn't Enough

Editing HAProxy’s config file and reloading switches over to a new process at the moment of reload, so how the existing connections held by the pre-reload process get handled can become unpredictable, depending on the reload implementation and configuration. Changing to the DRAIN state via the Runtime API involves no process restart at all — it just dynamically rewrites the internal state of the already-running HAProxy process, so the continuity of existing connections is guaranteed far more reliably. For planned maintenance, using a Runtime API state change, rather than a config file change plus reload, is strongly recommended in real-world practice.

What a Pro Sees Here (Top 1% Understanding)

“Connection Draining” Is a Shared Concept That Goes by a Different Name in Every Load Balancer Implementation

The word “DRAIN” itself is HAProxy-specific terminology, but the concept behind it — “stop new connections, let existing ones finish naturally” — is widely used across load balancer implementations, regardless of vendor. AWS’s ELB/ALB calls this mechanism “Deregistration Delay,” allowing a configured number of seconds for a deregistered target to keep responding to its existing connections. GCP’s load balancer calls it “connection draining timeout.” Kubernetes has no dedicated feature called a “DRAIN state,” but achieves effectively the same result through a combination of two features: removing a Pod that fails its readiness probe from the Service’s endpoints, while simultaneously delaying the Pod’s own termination by the duration set in terminationGracePeriodSeconds. The name differs by implementation, but the design philosophy of separating “stopping new inflow” from “waiting for existing work to complete” is remarkably consistent. Understanding this shared pattern lets you quickly get to the heart of a new load balancer product or cloud service, by asking “what’s the equivalent of DRAIN in this service?”

Common Misconceptions and Pitfalls

Troubleshooting Perspective

  1. Error reports come in from users during maintenance work: Check whether the server was stopped immediately in the MAINT state, and whether changing to DRAIN and confirming the session count were strictly followed.
  2. The session count just won’t reach 0, even though you set it to DRAIN: Check whether there’s traffic of a kind that holds connections open for a long time, like WebSocket or a long-lived API connection.
  3. A similar problem happened on a cloud load balancer: Check whether that service’s equivalent setting for “connection draining” (like deregistration delay) is set to an appropriate number of seconds.

Summary

Takeaways to Apply Today

  1. When putting a backend server into planned maintenance, use a change to the DRAIN state, not a config file deletion plus reload.
  2. When you work with a new load balancer product or cloud service, make it a habit to first ask “what’s the equivalent of DRAIN in this service?”

References

Comments

Feel free to leave thoughts, questions, or corrections below (sign in with a GitHub account to comment or react).