Overview
Curated: · Written: · Reviewed:
Load Balancing
In one line
A load balancer picks the backend for each unit of work (request, stream or job) and stops admitting work when the pool cannot absorb it. Reach for it when several backends serve the same traffic and you need distribution, health-based removal and controlled failover.
Key definitions
- Unit of work: the request, stream or job whose cost the algorithm is betting on. The connection is not the unit.
- L4 balancer: routes per connection by IP and port, without reading HTTP. In proxy mode it terminates the client TCP connection and opens a second upstream connection. In DSR the backend replies directly to the client.
- L7 balancer: routes per request on host, path, headers and cookies. It terminates TLS, then re-encrypts (bridging) or sends plaintext.
- Readiness: whether a backend should receive new work now. Liveness: whether it should be restarted. Keep them separate.
- Passive health: ejection driven by real request failures, which catches what synthetic checks miss.
- Draining: remove the backend from new selection, let bounded in-flight work finish, then force-close at a deadline.
- Session affinity: pinning a client to one backend because its state lives in that process.
Formulas and complexity
Example: detection, then 5.0 s drain gives 12.0 s end to end.
Consistent hashing moves about of keys when the Nth node is added. Modulo hashing (hash(key) % 12) moves almost all keys when 12 becomes 13: about . Consistent hashing with 12 to 13 nodes moves about .
Retry sleep: drawn uniformly from , so retries spread out instead of synchronizing.
How it works
- Traffic reaches the balancer by ARP/VRRP (LAN floating IP), anycast (BGP route withdrawal) or DNS (bounded by the TTL).
- L4 proxy mode owns both TCP connections. L7 parses each request and picks a backend per request.
- The algorithm encodes a cost assumption: round robin assumes equal cost, least connections assumes connection duration tracks cost.
- Health checks eject a backend after consecutive failures. Readiness gates new work.
- Deadlines and retries draw from one end-to-end budget. Overload is shed by admission, not queued without bound.
When to use and when not to
| Situation | Balancer choice | Why |
|---|---|---|
| Several stateless backends for one service | Yes | Distributes work and removes failed backends |
| Single backend, no HA requirement | No | Adds a hop and a failure domain for nothing |
| Stateful protocol that needs a fixed peer | Use the system's own routing | A generic L7 balancer misroutes |
| Slow dependency is the real problem | Not the fix | Spreads the symptom across more backends |
| Path, header or cookie routing needed | L7 | L4 cannot see requests |
| TLS must stay end to end, no per-request routing | L4 | The balancer cannot read the request |
| Client-side discovery already exists (e.g. gRPC) | Client-side | Skips a hop |
Traps interviewers probe
- Least connections means least loaded. Under HTTP/2 one connection can carry hundreds of unequal streams, so it counts as one. Fix: measure in-flight requests, latency and CPU per backend.
- Per-connection balancing with mixed work. A single long batch connection pins all its requests to one backend. Fix: balance per request at L7, or separate batch from interactive traffic.
- A passing TCP check proves health. A connect only shows the listener accepted. Fix: add an HTTP check on a representative dependency plus passive ejection on real failures.
- Liveness probes a dependency. A database outage then restarts every pod into a loop. Fix: readiness checks the dependency, liveness checks only the process.
- Retrying non-idempotent calls. The backend may have committed before the timeout. Fix: retry only idempotent operations, or use an Idempotency-Key with a durable outcome record, and draw every attempt from one deadline.
Interview tips
Opening. Name the unit of work, what it costs and how variable that cost is, then pick the layer and the algorithm that fits that assumption.
Follow-ups to expect.
- What breaks moving L4 to L7? Forwarded-header trust, doubled connection counts, a parsing attack surface, and keep-alive owned by the proxy.
- Which algorithm, and what would you measure to know it is wrong? Per-backend requests, bytes, concurrency, latency, errors and saturation.
- Derive the failover timeline. Detection is threshold times interval plus timeout, then add drain.
- How do you remove the need for stickiness? Externalize session state to a shared store.
- When is a retry unsafe? Non-idempotent operations without a key, or when the backend may have committed.
- What changes at scale? Control-plane convergence, retry storms, balancer capacity, and revocation latency for DNS or anycast.
30-second outline. The unit of work is the request, and its cost varies, so I would balance per request at L7 and watch in-flight work per backend, not connection counts. Health is a readiness check on a real dependency plus passive ejection, with consecutive thresholds to stop flapping. Retries only for idempotent calls, inside one deadline. Overload is shed at admission. Failover time is detection plus drain, and I would measure each part.
Feedback on this overview
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.