advanced
Load balancers
Use load balancers for health checks, routing algorithms, connection draining, sticky sessions, TLS, failover, and capacity planning.
Load balancers distribute traffic across healthy backends. Layer 4 (TCP) balancers route by IP/port with minimal inspection. Layer 7 (HTTP) balancers route by host, path, headers, and can insert cookies for stickiness.
Algorithms: round-robin, least connections, weighted, consistent hash (session affinity). Health checks remove unhealthy targets; connection draining waits for in-flight requests during deploys.
┌── App-1
Client → LB ─┼── App-2
└── App-3
Sticky sessions simplify server-side state but complicate rolling updates — prefer external session stores when possible.
On interviews: design failover when a zone fails; explain why health checks should hit a lightweight dedicated endpoint; align idle timeout with app keep-alive.
Common pitfalls: health checks that pass while app dependencies are down; sticky sessions pinning users to draining nodes; TLS renegotiation per request without session reuse.
The trade-off is balancing simplicity, performance, safety, and operability — name which axis you optimized and what cost you accepted.
Checklist:
- Choose L4 vs L7 by routing needs.
- Health check the same path/stack users hit.
- Drain connections before removing instances.
- Externalize session state when scaling horizontally.