Load balancers: route, detect and drain
Choose a routing layer and algorithm, detect unhealthy targets, and remove instances without abandoning existing work.
20 min7 steps
By the end you can
- Distinguish L4 connection routing from L7 request routing
- Choose a balancing algorithm for workload and affinity needs
- Explain health-check thresholds and detection delay
- Explain the availability and balance costs of sticky sessions
- Sequence connection draining during an instance replacement
Decide what the balancer can see
L4
- Transport addresses and ports
- Typically routes connections
L7
- HTTP paths, headers and cookies
- Can route individual requests
L4 balances transport flows without needing HTTP semantics. L7 understands the application protocol and can route /images separately from /checkout. Routing encrypted HTTP fields requires terminating or otherwise accessing decrypted traffic.
Equal turns need comparable work
Round robin gives targets turns. It is simple when capacities and request costs are similar. Weighted round robin adjusts shares for unequal capacity; equal request counts still do not guarantee equal CPU time.
Count work still in progress
- Target A20 connections
- Target B3 connections
Least-connections favors the target with fewer active connections. Power-of-two-choices samples two eligible targets and picks the less loaded by a chosen metric. Sampling reduces selection work. With HTTP/2 multiplexing, one connection may contain many requests, so choose the metric carefully.
Balance against key affinity
Power of two choices
- Sample two targets
- Pick lower observed load
Consistent hash
- Stable key-to-target mapping
- Membership changes move a subset
Consistent hashing is useful for cache locality and affinity. It does not divide a single hot key across targets. Power-of-two-choices responds to load but may send the same key to different targets. Choose whether locality or dynamic balance matters more.
Detection takes time
Just after t0
Target fails after a successful check
t5
First failed check completes
t10
Second failed check completes; remove target
Active checks probe readiness on a schedule. Thresholds avoid flapping but delay removal. This example assumes negligible probe duration; timeouts and propagation add delay. A shallow health endpoint may pass while real requests fail, so test critical serving dependencies without making checks overload them.
Affinity is not durability
Local memory + sticky cookie
- Repeat requests prefer one target
- Target loss can lose session state
Shared durable session store
- Any target can resume
- Store becomes a dependency
Sticky sessions trade routing freedom for locality. Heavy users can overload one target. If state lives only in that process, affinity cannot save it after a crash. Keep required session state outside disposable instances, or explicitly accept session loss.
Retire without abandoning work
Start
Mark target draining; stop assigning new work
Wait
Let in-flight work finish within a deadline
Finish
Close remaining connections and terminate
Draining separates admission from completion. Stop new assignments before terminating. Bound the wait so stuck requests cannot block deployment forever. Long-lived WebSockets and streams need an explicit reconnect or shutdown policy; they may outlast a normal drain timeout.
Loading question…
Mastery check
5 questions on fresh variants. One attempt each, no time limit. Score 80% or more to master the lesson.
Answer this step to continue.