Design building blocks · core
Load balancers: layer 4 and layer 7
A layer 4 balancer routes connections by address and port and is fast. A layer 7 balancer reads HTTP, so it can route by path, end TLS and retry. The algorithm and the health checks matter as much as the layer.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe notes
What you must know
- Layer 4 (TCP and UDP) sees only addresses and ports and passes connections through, for example AWS NLB. Layer 7 (HTTP, gRPC) sees paths, headers and cookies and can route by path, retry, terminate TLS and rate-limit (ALB, Nginx, Envoy, HAProxy).
- Algorithms: round robin for identical stateless servers; least connections or least outstanding requests when requests vary in cost; consistent hashing when a key should reach the same server (cache locality); weights for mixed hardware or canaries.
- Health: active checks (a /health endpoint, with readiness separate from liveness) and passive outlier detection on errors and latency, so a slow server is removed.
- WebSockets are long-lived: use least connections, drain connections on deploys and rebalance slowly.
- Sticky sessions hide state on servers. Keep session state in Redis or a token so any server can answer.
- TLS ends at the layer 7 balancer so it can read requests. Re-encrypt to backends where policy needs it.
- The balancer itself must not be a single point of failure: run several across zones.
- gRPC uses one HTTP/2 connection for many requests, so a layer 4 balancer pins the traffic to one pod. Use a layer 7 aware balancer, client-side balancing or a mesh.
The red pen
Common traps: mistake → fix
Mistake: Round robin is always fine
Fix: requests vary in cost, so use least outstanding requests.
Mistake: A health check that checks the process is running is enough
Fix: check that it can serve real requests.
Mistake: Sticky sessions are the way to keep login state
Fix: use shared session state.
Easy to hard
Questions interviewers ask, easy to hard
- Layer 4 or layer 7: how do you choose?
- Which algorithm for servers with very different request costs?
- How do you do health checks?
- How do you load balance WebSockets?
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.