Load balancing, L4 and L7
The moment you run more than one server, something has to decide which one gets each request. That something is a load balancer, and it has two jobs: spread the traffic evenly, and — the underrated, more important job — stop sending traffic to dead servers. A balancer that spreads perfectly but routes 10% of users to a crashed instance has failed at the job that matters.
Layer 4 vs Layer 7: how deep does it look?
The "layers" come from the networking stack. What matters is what the balancer can see:
Layer 4 operates on IP addresses and ports — the envelope, never the letter. It doesn't read the request at all. That blindness makes it blazingly fast (millions of connections, minimal CPU per packet), but limited: it can't route /api differently from /images, can't terminate TLS, can't retry a failed request elsewhere. And once a connection is established, it's pinned to one backend for its whole lifetime.
Layer 7 opens the letter. It reads the full HTTP request, and everything becomes possible: route /api/* to one server pool and /static/* to another; terminate TLS (handle the encryption handshakes from Tier 1 so backends deal in plain traffic); retry a failed request on a different backend before the user ever sees an error; rewrite headers; enforce rate limits. The price is CPU — parsing every request costs more than forwarding packets. For application traffic, L7 is what you want; in fact, an API gateway is essentially an L7 balancer with auth and quota duties bolted on.
Choosing the algorithm
- Round robin — deal requests out in turn: 1, 2, 3, 1, 2, 3. Simple, and it silently assumes all requests cost the same. They don't — one heavy report request next to a hundred trivial pings, and round robin happily keeps piling onto the busy server.
- Least connections — send each request to whichever backend has the fewest currently in flight. In-flight count is a decent live proxy for "busy," which handles variable request costs gracefully. This is the sane default.
- Least response time — also factor in each backend's recent latency. A subtly degraded instance (failing disk, GC storms) responds slowly, gets sent less work, automatically. Self-adjusting.
- Consistent hashing — same key (say, user ID) always lands on the same backend. Necessary when backends hold per-user in-memory state or local caches. (Full mechanics in Lesson 4.05 — it's the same tool sharding uses.)
Health checks — where a subtle trap lives
The balancer probes each backend: alive? Sounds trivial; the design question is how deep to probe.
A shallow check ("does the port answer?") passes while the server is actually useless — its database connection pool exhausted, every real request failing. A deep check ("can you reach your database?") catches that — and creates a spectacular failure mode: the database blips for five seconds, every backend's deep check fails simultaneously, the balancer removes all of them, and a five-second wobble becomes a total outage. The dependency's failure was amplified by your own safety mechanism.
The production compromise: a shallow liveness check ("is the process running?" — restart it if not) plus a separate readiness check ("should traffic come here right now?"), and critically, a floor: never remove more than, say, half the fleet at once, no matter what the checks claim. When everything looks dead simultaneously, the checks are more likely wrong than the fleet.
Sticky sessions as a substitute for stateless servers — routing each user to "their" server because that's where their session memory lives. Now you can't deploy without logging users out, can't scale down without evicting people, and load skews toward whichever servers accumulated the heavy users. The real fix: move session state out of the servers (into Redis), so every server can serve every user and the whole class of problem evaporates. Stateless servers are what make horizontal scaling actually work.