What it is
A load balancer sits between clients and a group of servers that do the same job. Clients connect to the balancer's address; for every request (or connection) it picks one server, forwards the traffic and relays the answer back. To the client the group looks like one server.
Why it exists
One machine has a ceiling: a fixed number of requests it can work on at once and a time per request. Past that ceiling, requests wait and then fail. Running more copies of the service raises the ceiling, but only if traffic actually reaches the copies. The load balancer is the piece that does that, and it also hides servers that are down.
How it works
Layer 4 or layer 7. A layer-4 balancer works with TCP/UDP connections: it picks a server per connection and forwards bytes without reading them. A layer-7 balancer terminates HTTP, so it can route by path, host or header, retry idempotent requests and add headers, at the cost of more work per request.
Choosing a server. Common algorithms:
- Round robin takes turns. Cheap and predictable; fair only when servers and requests are similar.
- Weighted round robin gives bigger servers more turns. You must keep the weights right.
- Least connections (or least outstanding requests) picks the server with the fewest requests in progress. Slow servers and slow requests show up as more work in progress, so it adapts without configuration.
- Random, or "power of two choices" picks two servers at random and takes the less loaded one; it avoids a herd of balancers all picking the same idle server.
- Hashing (by client IP, user or key) sends the same key to the same server, useful for caches; consistent hashing limits how many keys move when servers change.
Health checks. The balancer probes each server on an interval (a TCP connect, or an HTTP request to a health endpoint). After a number of consecutive failures the server is taken out of rotation; after a number of successes it is put back. Many balancers also use passive checks: real requests that fail count against the server.
When to use it
Whenever more than one instance serves the same traffic: web and API tiers, internal services, database read replicas. Also when you need a stable entry point while servers come and go (deployments, autoscaling).
When not to
- A single instance with no need for redundancy: a balancer adds a hop and another thing to run.
- When clients can balance themselves well (gRPC clients with service discovery, or a service mesh sidecar); a central balancer may be redundant there.
- For stateful protocols where any server cannot handle any request, until the state is moved out of the servers.
Trade-offs
| Choice | You gain | You pay |
|---|---|---|
| Layer 7 instead of layer 4 | Routing by content, retries, observability | CPU per request, TLS termination to manage |
| Least connections instead of round robin | Adapts to slow servers and requests | Needs connection tracking; misleads with long-lived connections |
| Short health check interval | Faster detection | More probe traffic, more false alarms from brief hiccups |
| Sticky sessions | Simple in-memory sessions | Uneven load; users lose sessions when a server dies |
Alternatives
- DNS round robin: several IP addresses for one name. Free and global, but clients cache answers, so failover is slow and uneven.
- Client-side balancing: the client knows all instances (from service discovery) and picks one. No extra hop; every client needs the logic.
- Anycast: the same IP is announced from many locations and the network routes to the nearest. Used by CDNs and global front doors.
Failure modes
- Detection gap: between a crash and the server's removal, requests sent to it fail. Size it as interval × threshold.
- No headroom: removing a dead server moves its traffic onto the others. If they were already near capacity, the failure of one becomes the overload of all (cascading failure).
- Flapping: a server that passes and fails checks alternately keeps entering and leaving rotation. Require several consecutive successes before restoring it.
- Health check lies: an endpoint that returns 200 while the app cannot reach its database keeps a broken server in rotation. Check what matters, but not so deeply that one shared dependency fails every server at once.
- The balancer itself can fail or saturate.
In production
Run balancers as a redundant pair (with a floating IP or virtual router protocol) or use a managed, multi-zone balancer. Drain connections before taking a server out for deployment. Monitor per-backend error rate and latency, not just the total. Keep sessions and other state in a shared store so any server can handle any request.
Common misconceptions
- "Round robin is fair." It is fair by count. If one server is slower, or some requests are much heavier, equal counts mean unequal load.
- "More servers always means lower latency." Only while the bottleneck is the servers. If the database is saturated, more application servers add load to it.
- "Health checks make failures invisible." They shorten the impact; requests in the detection window still fail unless clients retry.
- "A load balancer removes single points of failure." A single balancer is one.
Interview angle
Interviewers expect you to place a balancer in front of every stateless tier without being asked, then probe: which algorithm and why, layer 4 vs 7, how health checks and draining work, how you avoid the balancer being a single point of failure, how sessions are handled, and whether the remaining servers can absorb a failure. Mentioning capacity in numbers ("each instance does ~75 req/s, peak is 160, so four instances leave N+1 headroom") stands out.
Model assumptions
The simulation in this lesson is a simplified model, not a benchmark. Each server has 6 workers, an 80 ms mean service time (lognormal, cv 0.35) and room for 12 waiting requests; the older server runs at half speed. Arrivals are random (Poisson). Network hops take a fixed 5–25 ms. Health checks probe every 500 ms; 2 failures eject, 3 successes restore. Utilisation is the fraction of busy workers, not CPU. Numbers show behaviour and orders of magnitude only.