Skip to content
Tech Interview Prep
Technical interview guide

Load Balancing

Distributing traffic across multiple backend instances: algorithms, health checks, and layer 4 vs layer 7 balancing.

Minutes to read
4
Practice MCQs
25
Interview Q&A
25
Edition
v5
Editorial status
Reviewed

Scope: Current HTTP/1.1, HTTP/2, QUIC, TLS 1.3, Forwarded, HAProxy 3.2, NGINX, Envoy, Kubernetes, AWS ELB, and Google Cloud Load Balancing references reviewed 2026-09-04.

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

detection=threshold×interval+check timeout\text{detection} = \text{threshold} \times \text{interval} + \text{check timeout}

Example: 3×2.0 s+1.0 s=7.0 s3 \times 2.0\,\text{s} + 1.0\,\text{s} = 7.0\,\text{s} detection, then 5.0 s drain gives 12.0 s end to end.

Consistent hashing moves about 1N\frac{1}{N} of keys when the Nth node is added. Modulo hashing (hash(key) % 12) moves almost all keys when 12 becomes 13: about 1213≈92%\frac{12}{13} \approx 92\%. Consistent hashing with 12 to 13 nodes moves about 113≈8%\frac{1}{13} \approx 8\%.

Retry sleep: drawn uniformly from [0,min⁡(cap,base×2attempt)][0, \min(\text{cap}, \text{base} \times 2^{\text{attempt}})], so retries spread out instead of synchronizing.

How it works

  1. Traffic reaches the balancer by ARP/VRRP (LAN floating IP), anycast (BGP route withdrawal) or DNS (bounded by the TTL).
  2. L4 proxy mode owns both TCP connections. L7 parses each request and picks a backend per request.
  3. The algorithm encodes a cost assumption: round robin assumes equal cost, least connections assumes connection duration tracks cost.
  4. Health checks eject a backend after consecutive failures. Readiness gates new work.
  5. Deadlines and retries draw from one end-to-end budget. Overload is shed by admission, not queued without bound.
per connection or per request probe probe probe Client VIP via ARP, BGP or DNS Balancer L4 or L7 Backend 1 Backend 2 Backend N Health checker

When to use and when not to

SituationBalancer choiceWhy
Several stateless backends for one serviceYesDistributes work and removes failed backends
Single backend, no HA requirementNoAdds a hop and a failure domain for nothing
Stateful protocol that needs a fixed peerUse the system's own routingA generic L7 balancer misroutes
Slow dependency is the real problemNot the fixSpreads the symptom across more backends
Path, header or cookie routing neededL7L4 cannot see requests
TLS must stay end to end, no per-request routingL4The balancer cannot read the request
Client-side discovery already exists (e.g. gRPC)Client-sideSkips a hop

Traps interviewers probe

  1. 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.
  2. 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.
  3. 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.
  4. Liveness probes a dependency. A database outage then restarts every pod into a loop. Fix: readiness checks the dependency, liveness checks only the process.
  5. 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.