Load balancing
Round-robin, least-connections, health checks.
A load balancer is the front door to your service: it spreads traffic across instances, removes broken ones, and (when configured well) handles the boring 90% of failures so your application doesn't have to.
The big idea
You have N identical app instances and one stream of incoming requests. The load balancer distributes the requests, watches health, and pretends to be one service to clients.
L4 vs L7
The first decision: at which layer does the LB look at traffic?
| Attribute | L4 (TCP) | L7 (HTTP) |
|---|---|---|
| Sees | IPs and ports | Headers, paths, methods, body |
| Smart routing | No | Yes — by URL, header, cookie |
| TLS termination | Optional | Common (offload to LB) |
| Speed | Faster, less CPU | Slower; richer features |
| Examples | AWS NLB, HAProxy in TCP mode | AWS ALB, NGINX, Envoy, Traefik |
For most services: L7. The smart routing pays for the CPU.
Algorithms
How does the LB pick which backend gets the next request?
- Round robin
- Cycle through instances in order. Simple; assumes uniform request cost — wrong often.
- Least connections
- Send to the instance with the fewest active connections. Good when requests vary in duration.
- Random
- Pick uniformly at random. Surprisingly competitive; trivially scalable.
- Power of two choices
- Pick two random instances, send to the less-loaded one. Mathematically wins over both round-robin and pure random in practice.
- Consistent hashing
- Same key (user ID, session) always goes to the same backend. Used for sticky sessions and sharded caches.
Health checks
Without them, a load balancer cheerfully forwards traffic to a dead instance.
health_check:
path: /healthz
interval: 5s
timeout: 2s
healthy_threshold: 2
unhealthy_threshold: 3Two flavours of health check coexist:
Liveness
Is the process responsive?
200on a trivial endpoint. Used by orchestrators (K8s) to restart wedged pods.Readiness
Is this instance ready to take traffic? Returns 503 during startup, drain, or dependency outages. Used by the load balancer to remove instances from rotation.
Sticky sessions (and why to avoid them)
A sticky session pins a user to one backend by cookie or IP hash. Useful when each backend caches per-user state in memory. The cost: failover is painful (the user's data is on a now-dead instance), and rolling deploys break stickiness.
Better: make the backend stateless and store session data in Redis or a signed cookie. The load balancer goes back to round-robin and life is simpler.
TLS termination
Most production setups terminate TLS at the load balancer. The LB has the cert, decrypts traffic, and forwards plain HTTP over the private network to the backends. Saves CPU on each instance and centralises certificate rotation.
In practice
The load balancer is also your operational handle for:
- Rolling deploys — drain connections from old instances before stopping them.
- Blue-green / canary — weight traffic between two pools.
- Rate limiting + WAF — block bad traffic before it reaches your app.
- Geo-routing — send users to their nearest region.
A surprising number of "incidents" are LB misconfigurations: a stale target group, a health check pointing at the wrong path, a TLS cert expiring. Treat the LB config like production code.
Key takeaways
- L7 load balancing buys smart routing, TLS termination, and observability — usually worth the CPU.
- Power-of-two choices or least-connections beats vanilla round-robin in practice.
- Health checks should test what would break a request, not just process liveness.
- Avoid sticky sessions; make backends stateless instead.
- TLS termination at the LB simplifies certs but requires X-Forwarded-* handling downstream.
Checkpoint questions
Use these to test whether the lesson is clear enough to explain without rereading.
- 1What problem does a load balancer solve besides distributing traffic?
- 2How do round-robin, least-connections, and health-check behavior differ?
- 3What happens to user sessions if backend servers are not stateless?
- 4Which signals should remove an unhealthy instance from rotation?
References
External resources for going deeper after the lesson above.