Building Blocks

Load Balancers

L4 vs. L7, round robin vs. least connections vs. consistent hashing, and why sticky sessions are usually a smell.

11 minMediumload-balancingbuilding-blocks

Almost every design has one, and almost every candidate mentions it without saying which kind or how it actually picks a backend. Both details are where the follow-up questions live.

L4 vs. L7

A Layer 4 load balancer routes based on IP and port alone — it doesn't look at the HTTP request, just forwards TCP connections, so it's fast and protocol-agnostic. A Layer 7 load balancer reads the actual HTTP request — path, headers, cookies — and can route /api/* to one backend pool and /static/* to another, or route based on a cookie for sticky sessions. The trade-off is the obvious one: L7 does more work per request (it has to actually parse HTTP) in exchange for routing decisions L4 physically cannot make.

Routing algorithms

  • Round robin — cycles through backends in order. Simple, and a reasonable default when every backend and every request costs about the same.
  • Least connections — routes to whichever backend currently has the fewest active connections. Better than round robin when requests vary a lot in how long they take, since round robin can pile slow requests onto one unlucky instance.
  • Consistent hashing — routes based on a hash of some request property (e.g. a user ID), so the same key reliably lands on the same backend. This is what makes a sharded cache work at all — without it, "which Redis node holds this key" would change every time the backend pool resizes.

Health checks and removal from rotation

A load balancer only helps if it stops routing to a backend that's actually failing — see Reliability for the liveness-vs-readiness distinction. An instance that fails its readiness check gets pulled out of rotation automatically, and put back once it starts passing again — this is also what makes a rolling deploy safe: new instances only receive traffic once they report ready.

Client

Load Balancer

routes only to healthy backends

Backend pool

periodic readiness checks

Unhealthy instance

pulled from rotation until it recovers

L4/L7 in front of each other

In practice these aren't either/or — a common real deployment shape is an L4 load balancer at the network edge (fast, handles raw connection volume) in front of an L7 layer that does the actual path-based routing. Naming that layering, rather than treating "load balancer" as one undifferentiated box, is a reasonable signal at the senior level.