Course map

Topic 098 / Core

Graceful shutdown and connection draining

Chapter 6.4 · Traffic Control and Workload Shaping

What and why

An instance has to stop at some point — a deploy, a scale-in (→ 95), a crash recovery — and if it simply stops accepting connections and exits, every in-flight request on it is cut off mid-response. Graceful shutdown is the deliberate sequence that lets an instance leave service without breaking requests already in progress: stop accepting new work, finish or hand off what's in flight, then exit. Builds on: → 92 (load balancing and its algorithms).

Diagram

signal to shut down
      │
      ▼
1. mark instance "not ready" ──► load balancer (→ 92) stops sending new requests
      │
      ▼
2. finish in-flight requests (connection draining) ── bounded by a drain timeout
      │
      ▼
3. close remaining connections, release resources, exit

How it works

  1. On receiving a shutdown signal, the instance first marks itself not-ready — a readiness check (→ 183) starts failing — so the load balancer (→ 92) stops routing new requests to it, without touching connections already open.
  2. The instance keeps serving requests already in flight — connection draining: existing work finishes normally instead of being severed.
  3. A drain timeout bounds step 2: if some request is stuck beyond a sensible limit, the instance forcibly closes it rather than waiting forever for a shutdown that's supposed to be fast.
  4. Only after draining (or timing out) does the instance release its resources — database connections (→ 77), open files, background workers — and exit.
  5. The gap between "a deploy that's invisible to users" and "a mini-outage" is almost entirely whether this sequence runs at all: skip it, and every deploy or scale-in event drops whatever requests happen to be in flight at that instant.

Practical example

(numbers illustrative). An instance handling 50 requests/second with a mean request duration of 200 ms is told to shut down. At the instant the signal arrives, roughly 50 × 0.2 = 10 requests are in flight on average. Without graceful shutdown, all 10 are cut off mid-response. With a 5-second drain timeout, the instance stops accepting new work immediately but keeps serving those ~10 in-flight requests to completion — at 200 ms average, essentially all finish well within the 5-second window, and zero requests are abandoned.

When to use it

  • Use graceful shutdown on every deploy, scale-in, and planned restart — there is no scenario where cutting off in-flight requests is preferable to draining them within a bounded timeout.
  • There is no meaningful "avoid" case for the mechanism itself; the only judgment is the drain timeout's length — too short re-creates the cut-off problem for slower requests, too long delays a deploy waiting on a stuck instance.
  • Failure scenarios: a load balancer health-check interval longer than it takes the instance to actually stop, so a few requests still land on a draining or already-gone instance; a drain timeout with no forced cutoff, letting one stuck connection block an entire deployment; state that was never made stateless (→ 91) living only on the instance being drained, lost regardless of how gracefully connections are closed.
  • Simpler alternative: none meaningfully simpler for a production service taking live traffic; for a batch job with no live connections, graceful shutdown reduces to "finish or checkpoint the current unit of work," a smaller version of the same idea.

Practice

(mechanism/process kind, 20 min, any language or lab).

  1. Build a small HTTP server that, on a shutdown signal, stops accepting new connections, waits for in-flight requests (simulate with a few that sleep 1–2 seconds) to finish, then exits; verify no in-flight request errors.
  2. Remove the graceful handling (exit immediately on signal) and repeat with the same in-flight requests; observe the difference directly.
  3. Add a drain timeout shorter than one request's duration; confirm that request is forcibly cut, and explain the trade-off you just chose.

Done when the ungraceful version visibly drops in-flight requests and the graceful version, under the same load, does not.

Shared lab setup

Key takeaway

Graceful shutdown is the deliberate sequence — stop accepting, drain, then exit — that turns "an instance disappeared" into a non-event for anyone mid-request; the only real decision is how long to wait. Done when you can: implement the three-step sequence and name what still breaks if some state was never made stateless. Used next in → 95 (auto scaling's scale-in event, which this topic makes safe), → 183 (readiness probes, the Kubernetes mechanism that gates this), → 187 (deployments, where this matters most).

References

None — this topic makes no external claims.

Source directory

Search the guide