Skip to main content

Leaving Quietly: Draining a Node Before You Take It Down

· 11 min read
Clustron Team
Distributed Systems Engineering

Every node in a cluster is going to be taken down deliberately at some point — a rolling upgrade that cycles through the fleet, a scale-in that gives capacity back, a host that needs a kernel patch. This is not failure; it is maintenance, and it is routine. The question is only how violent it has to be.

The lazy way is to just kill the process. On a well-built store this loses no data: the keys the node owned have replicas elsewhere, the failure detector notices the node has gone silent, and the recovery machinery rebuilds the missing owner from those replicas. So the hard kill is safe. But safe is not the same as smooth. At the instant the process dies, every request currently executing on it fails and surfaces to some client as an error; every connection open to it is severed; and every key it owned is briefly ownerless, because the cluster has to first detect the absence — which deliberately takes a few seconds, since detecting too eagerly would evict healthy-but-slow nodes — and only then start the reassignment. A planned shutdown ends up looking, from the outside, exactly like a crash: a spike of errors and a window of unavailability for a slice of the keyspace.

That is a waste, because a planned departure has something a crash never does: warning. The node knows it is about to leave. Draining is the practice of spending that warning well — announcing the intent, stopping new work, finishing or handing off what is in flight, moving ownership to the successors before leaving rather than after, and only then exiting. Done right, a node leaving the cluster produces no client-visible errors at all.

Why the hard kill hurts more than it looks​

Trace what a hard kill actually triggers. The requests mid-flight on the node — reads being served, writes being replicated — all abort at once, and each becomes an error the client did not expect and may or may not retry correctly. Clients that had healthy, warm connections to the node now see them drop and must reconnect and re-route, a latency hiccup across every one of them simultaneously. And the keyspace the node owned enters a gap: the failure detector is tuned to wait before declaring a node dead precisely so it does not overreact to a GC pause or a slow link, so for that waiting window the owned keys have no active owner, and requests for them either fail or fall back to a replica that may not yet be promoted. The recovery is correct, but it is reactive — it starts only after detection, and detection is intentionally unhurried.

Draining removes every one of those costs by reordering the work. Instead of letting the cluster discover the departure after the fact and scramble to react, the departing node does the reassignment itself, in advance, while it is still alive and able to cooperate — so ownership has already moved by the time the process exits, and there is nothing to detect and nothing to scramble over.

Hard kill — react after the factkillt = 0in-flight failconnections dropdetection windowkeys ownerless (~seconds)rebuild ownerelsewhereDrain — do the work before leavingannounce"I am leaving"stop new workreject + redirectfinish in-flightlet them completehand off ownershipto replicasclose conns + exitclients re-routeownership moves while the node is still alive → no error burst, no ownerless gap

The ordered steps of a clean drain​

A drain is a sequence, and the order is what makes it clean. Skipping a step or reordering two of them reintroduces exactly the disruption draining exists to avoid.

Announce. The node tells the cluster — through the same membership gossip it uses for everything else — that it is leaving, which is a distinct state from down. Peers and routing-aware clients learn not to send it new work and to prefer other owners. This is a graceful-leave signal, not a death.

Stop accepting new work. The node begins rejecting new requests with a signal that means "not here, retry elsewhere" rather than a hard failure, so clients re-route immediately instead of erroring. New connections are refused; new writes and reads are redirected to the keys' other owners.

Finish what is in flight. The requests already executing are allowed to complete — this is the quiesce, and it is why a drain takes a moment rather than being instantaneous. The node waits for its in-flight queue to empty, the same bounded queue that governs its normal load, now draining toward zero instead of holding steady.

Hand off ownership. This is the heart of it. For every key range the node owns, it transfers ownership to the replicas that will take over, confirming they are caught up before it relinquishes the role — the same careful handover as a partition cutover, run proactively. When this completes, no key the node owned is left without an active owner, because the successor is already serving.

Close connections and exit. With no ownership and no in-flight work, the node closes its client connections cleanly so clients see an orderly disconnect and reconnect to the new owners, and the process exits. There is nothing for the failure detector to detect, because the cluster already knows this node is gone and has already moved on.

async Task Drain(TimeSpan budget) {
using var deadline = new CancellationTokenSource(budget); // bound the whole thing
membership.AnnounceLeaving(); // gossip: "leaving", not "down"
listener.StopAcceptingNew(redirect: true); // new work → retry-elsewhere, not error

await inFlight.WhenDrained(deadline.Token); // let running requests finish

foreach (var range in owned.Ranges()) // proactively move ownership
await handoff.TransferToReplicas(range, deadline.Token);

await connections.CloseGracefully(); // orderly disconnect → clients re-route
// if the deadline fired: stop waiting, let crash-recovery cover the rest, then exit
}

The budget: drain, but not forever​

A drain must not be allowed to hang. If one in-flight request is stuck, or a replica that should take ownership is itself unhealthy, a drain that waits unconditionally will never finish, and a rolling upgrade stalls on a single node that will not leave. So every drain runs under a budget — a deadline after which it stops being polite. When the budget expires, the node stops waiting, exits anyway, and deliberately falls back on the crash-recovery path for whatever did not drain: the few still-in-flight requests fail and retry, the ownership that did not hand off is rebuilt reactively. The budget turns graceful shutdown into best-effort graceful shutdown, which is the only honest kind — it captures the common case where everything drains in a second or two, without letting the uncommon case hold the fleet hostage.

This is why draining does not replace crash-recovery; it sits in front of it. The reactive path — detect, rebuild, heal — must exist regardless, because real crashes happen with no warning at all, and a drain that overruns its budget converges onto that same path on purpose. Draining is the optimization for the case where the node knew it was leaving; crash-recovery is the floor underneath it for every case where it did not. A production store needs both, and a good shutdown simply prefers the graceful path and times out into the safe one.

Wrap-up​

A hard kill of a cluster node is safe but needlessly disruptive: in-flight requests fail, connections drop, and the owned keyspace sits ownerless through the failure detector's deliberate waiting window before recovery reacts. Draining spends the warning a planned shutdown has — it announces the departure as a graceful leave, stops taking new work and redirects it, lets in-flight requests finish, proactively hands ownership to the replicas while the node is still alive, and closes connections cleanly before exiting, so the departure produces no error burst and no ownerless gap. A budget keeps the drain from hanging on a stuck request or an unhealthy successor, timing out into the crash-recovery path that must exist anyway for unplanned failures. Leave on your own terms when you can, fall back to being rebuilt when you can't, and make a node going down for maintenance a non-event for everyone still running.