Load Balancing and Proxies
Learn how traffic is spread across instances, the difference between L4 and L7 balancing, and what forward and reverse proxies do.
Intermediate · about 5 hours · difficulty 3/5
Learn first: HTTP and HTTPS
Load balancers make services scalable and highly available. Almost every production request passes through at least one proxy, so their behaviour shapes latency, errors and failover.
Proxies
- A forward proxy acts for clients (corporate egress, caching).
- A reverse proxy sits in front of servers: TLS termination, routing, compression, caching, rate limiting. Nginx, HAProxy, Envoy and cloud load balancers are all reverse proxies.
Load balancing
- Layer 4 balances TCP/UDP connections by IP and port; fast and protocol-agnostic (AWS NLB).
- Layer 7 understands HTTP and can route by host, path or header (AWS ALB, ingress controllers).
- Algorithms: round robin, least connections, weighted, consistent hashing.
- Health checks remove bad instances; connection draining lets in-flight requests finish during deploys.
- Session affinity (sticky sessions) is sometimes needed but hurts even spreading.
Beyond one region
DNS-based and anycast global load balancing, and CDNs as edge proxies.
Pitfalls
Health checks that only test "process is up" rather than "can serve traffic", lost client IPs (use X-Forwarded-For or the PROXY protocol), and mismatched idle timeouts between the balancer and the app causing sporadic 502s.
Good enough to move on: you can choose between L4 and L7 for a use case, configure meaningful health checks, and explain how draining enables zero-downtime deploys.
Key concepts
- Forward vs reverse proxy
- L4 vs L7 load balancing
- Balancing algorithms
- Health checks and draining
- Sticky sessions
- Client IP preservation
- Global load balancing and anycast
Mini project
Run three copies of a small app that returns its hostname, put a reverse proxy in front with round robin and a health check, then kill one instance and show traffic keeps flowing.
Check yourself
Why can a load balancer idle timeout that is longer than the app's keep-alive timeout cause random 502 errors?
Free resources
- What is load balancing? (Cloudflare)
- Elastic Load Balancing docs (AWS)