SysDesignAtlas

Load Balancing

L4 vs L7, the algorithms and when each actually matters, health checking, and why the load balancer itself isn't a single point of failure.

HLDEasy11 min readload-balancertrafficL4L7
Confidence

#What it buys you

A load balancer sits between clients and a pool of servers and distributes requests. It gives you four things at once:

  1. Horizontal scale — the pool can grow without clients knowing.
  2. Availability — unhealthy servers are removed from rotation automatically.
  3. Zero-downtime deploys — drain a server, update it, return it.
  4. A control point — one place to handle HTTPS (TLS termination: the load balancer decrypts, so your servers deal in plain HTTP), cap abusive traffic, route requests, and watch what's happening.

It is the first component in nearly every design, and it is routinely drawn without anyone examining what it actually does. The interesting parts are below.

#L4 vs L7

The most important distinction, and a very common interview question. The numbers come from the network layer model: layer 4 is where connections live, layer 7 is where the actual HTTP request lives.

Layer 4 (transport) just forwards connections. It can see which machine and port the traffic is going to, but not what's inside it — it has no idea whether you asked for /login or /checkout. It picks a server when the connection opens, and everything on that connection goes to the same place.

Layer 7 (application) opens the envelope. It terminates the connection, reads the HTTP request, and can route on the URL path, a header, or a cookie — then makes its own separate connection to the chosen server.

L4L7
SeesIP, portFull HTTP request
Routing onConnection tuple (source/destination IP and port)Path, host, header, cookie
TLSPassthrough (or termination)Terminates (required to read content)
ThroughputVery high, low CPULower, more CPU
Latency added~µs~ms
Retries failed requestsNo (connection-level)Yes (request-level)
ExamplesAWS NLB, IPVS, MaglevAWS ALB, Nginx, Envoy, HAProxy

When L4 is right: non-HTTP protocols, extreme throughput, when you need TLS to terminate at the application, or when you want the LB to be as dumb and fast as possible.

When L7 is right: almost all web traffic. You get path-based routing (/api/* → API fleet, /static/* → asset fleet), per-request load balancing (crucial with HTTP/2, see below), request-level retries, canary routing by header, and centralized TLS.

#Algorithms

These are easy to list; the useful part is knowing when the choice matters. The honest answer: if your servers are identical and every request costs about the same, round robin is fine and the choice barely matters. The algorithms earn their keep when one of those two assumptions breaks.

Round robin — rotate through servers. Simple, no state. Breaks when request costs vary widely (one request takes 5 ms, another takes 5 s) or servers are heterogeneous.

Weighted round robin — the same, but bigger servers get proportionally more. Use it when your machines aren't all the same size, or to move traffic gradually onto a new version (a canary — a small slice of real traffic sent to the new build first, so a bad release hurts 1% of users instead of everyone).

Least connections — send to the server with fewest active connections. Naturally adapts to varying request duration; a server stuck on slow requests stops receiving new ones. Good default for variable workloads.

Least response time — least connections plus measured latency. Best at routing around a gray failure: a node that is technically up and passes its health checks but answers slowly (say, a failing disk or a noisy neighbour). Because the balancer measures latency, that slow node gets fewer requests automatically, without anyone declaring it dead.

Random with two choices (P2C) — pick two servers at random and send to whichever is less busy. Surprisingly, that tiny amount of choice performs nearly as well as always picking the least busy server, without anyone needing to track the whole fleet. That makes it ideal when the balancing happens inside each client rather than in one central place.

Hashing (IP hash / consistent hash) — deterministic mapping from a key to a server: the same key (a user ID, a client IP) always lands on the same server. Needed when servers hold per-server state — data that lives on one specific machine. Examples: a cache tier (user 42's profile is cached on node B, so requests for it should go to node B), a stateful WebSocket session, or sharded in-memory data. Use consistent hashing so adding a server doesn't remap everything.

#Health checks

The mechanism that turns a load balancer into an availability tool.

Passive — observe real traffic; eject a backend after N consecutive failures. Free, but only detects problems by failing user requests first.

Active — poll /health on an interval. Detects failure before users do; costs a little traffic.

Use both. Active for detection, passive for the failures active checks miss.

Design your health endpoint carefully — this is where people get burned:

  • A shallow check (return 200) can't detect a broken process. It'll happily keep a node in rotation while it serves garbage.
  • A deep check (verify DB, cache, downstream) detects real problems — but if the shared database hiccups, every node fails its check simultaneously and the LB removes your entire fleet. You've converted a degraded dependency into a total outage.

The standard resolution: deep checks that distinguish my problems from shared problems, plus a minimum healthy threshold — "never eject more than 50% of the pool." If everything looks unhealthy, the failure is probably in the checker, so keep serving.

Related: connection draining. On removal, stop sending new requests but let in-flight ones finish (typically 30 s). Without it, every deploy drops live requests.

#Who balances the balancer?

The obvious question, and the one the design has to answer.

A single LB instance is a single point of failure. Layered answers, in increasing sophistication:

  1. Active–passive pair with a floating IP. Two balancer instances share one virtual IP (VIP) — the address clients actually connect to. Normally the active one answers on it. The two instances check on each other (a protocol called VRRP does this), and if the active one dies, the standby takes over the VIP, so clients keep using the same address. Simple, but it wastes half your capacity (the standby sits idle) and failover takes seconds.
  1. DNS round robin to multiple LBs. Publish several A records. Free horizontal scale, but DNS caching means failover is slow (clients honor TTLs, sometimes badly) — DNS is a poor failure-detection mechanism.
  1. Anycast. Announce the same IP address from many locations at once, and let the internet's own routing send each user to the nearest one. Failure removal is a route withdrawal — fast, and it happens in the network rather than in DNS. This is how large CDNs and Google/Cloudflare-scale load balancing actually work.
  1. ECMP + stateless L4 tier. ECMP (equal-cost multi-path) is a router feature: when several next hops are equally good, the router hashes each flow (one client connection, identified by its source and destination IPs and ports) to pick one. Here the router spreads flows across many identical L4 balancers, which forward to L7 balancers, which forward to servers. The L4 balancers keep no per-connection memory, so you can add as many as you like. If one dies, the router simply re-hashes its connections onto the survivors.

The complete answer: the load balancer is redundant via an active-passive pair with a floating IP, and at higher scale you move to anycast so failover happens in the routing layer rather than in DNS.

#Global load balancing

Distributing across regions, not servers. Three mechanisms:

  • GeoDNS — resolve to the nearest region's IP based on resolver location. Cheap, universally supported, but coarse (it knows the resolver, not the user) and slow to fail over due to caching.
  • Anycast — one IP everywhere, and BGP (the protocol internet routers use to agree on paths) picks the nearest site. Fast failover, no DNS TTL problem. Harder to control precisely. And if routes change mid-connection ("flap"), a long-lived TCP connection can suddenly land on a different site that knows nothing about it, and it breaks.
  • Application-level redirect — a lightweight global endpoint returns the right regional URL. Total control, extra round trip.

You also need health-aware routing and failover policy: if us-east is down, does us-west have the capacity to absorb its traffic? If both regions run at 70%, the answer is no — the failover itself causes the second outage. Sizing for failover capacity (running each region at ≤50% if you want single-region failure tolerance) is the kind of detail that reads as operational experience.

#What else lives here

Because it's on every request path, the load balancer accumulates responsibilities:

  • TLS termination — one place to manage certificates. Behind it, backends speak plain HTTP inside your private network, which you trust. (If you don't trust even your internal network — a "zero-trust" setup — the balancer re-encrypts to the backends using mTLS, where both sides present certificates.)
  • Rate limiting — cheapest place to shed abusive traffic, before it costs you anything.
  • Request routing — path/host based, plus canary and blue-green by weight or header.
  • Compression, caching, request/response rewriting.
  • Observability — a single consistent source of latency, status codes, and traffic metrics.

When it does all of this and fronts many services, we usually call it an API gateway — same box, more responsibilities.

#Check yourself

Layer 4 versus layer 7 load balancing: what can each see and do?Show answer
L4 forwards connections and sees only IPs and ports — fast and cheap, one server per connection. L7 terminates the connection and reads the HTTP request, so it can route by path, header or cookie, retry individual requests and do canaries.
Why does HTTP/2 or gRPC break layer-4 balancing?Show answer
The client multiplexes thousands of requests over one long-lived connection, which an L4 balancer assigns to a single backend once. You need L7 or client-side balancing to spread the requests.
When does the balancing algorithm actually matter?Show answer
When servers differ in size or request costs vary widely. With identical servers and similar requests, round robin is fine; otherwise least connections, least response time or power-of-two-choices adapt to slow requests and slow nodes.
Why does picking the less busy of two random servers work so well?Show answer
A tiny amount of choice gives nearly all the benefit of always picking the least loaded server, with no need to track the whole fleet — ideal when each client balances for itself.
Shallow versus deep health checks: what's the risk of each, and the standard fix?Show answer
Shallow checks keep a broken process in rotation; deep checks that test a shared database can fail every node at once and empty the pool. Separate "my" failures from shared ones and never eject more than about half the pool.
What is connection draining?Show answer
When a server is removed, stop sending it new requests but let in-flight ones finish (often about 30 s), so deploys don't drop live requests.
Who balances the balancer? Name the escalating answers.Show answer
An active–passive pair sharing a floating IP; DNS round robin across several balancers (slow failover); anycast, where many sites announce one IP and routing picks the nearest; a stateless L4 tier behind router hashing (ECMP).
Two regions each run at 70%. Can one absorb the other's traffic if it fails?Show answer
No — it would need 140% and the failover would cause a second outage. For single-region failure tolerance, run each region at 50% or less.

#What to carry forward

  • A load balancer buys scale, availability, zero-downtime deploys and one control point for TLS, limits and routing.
  • L4 is fast and blind; L7 reads requests — and is required to balance HTTP/2 and gRPC properly.
  • The algorithm matters only when requests or servers vary; hashing is for tiers with per-server state.
  • Health checks must separate a node's own failures from shared ones, with a floor on how many nodes can be ejected.
  • Make the balancer itself redundant — floating IP, then anycast — and size regions to absorb each other's traffic.
00:00
/ 11m