⚖️Key Takeaways
- 1Layer 4 (transport) balances by IP/port — fast but no content awareness; Layer 7 (application) can route by URL, headers, cookies
- 2Algorithms: Round Robin (simplest), Weighted RR, Least Connections, IP Hash (sticky sessions)
- 3Health checks prevent routing to dead servers — active (ping) and passive (monitor responses)
- 4Global Server Load Balancing (GSLB) distributes across data centers using DNS
The Traffic Cop of Distributed Systems
A load balancer distributes incoming network traffic across multiple backend servers. Without one, a single server handles all traffic — creating a bottleneck and single point of failure. Load balancers enable horizontal scaling by making multiple servers look like one.
Every production web architecture has at least one load balancer. Most have several: one in front of web servers, another for API servers, and internal ones between microservices.
L4 vs L7 Load Balancing
| Feature | Layer 4 (Transport) | Layer 7 (Application) |
|---|---|---|
| Operates on | TCP/UDP packets | HTTP requests |
| Routing criteria | IP + Port | URL path, headers, cookies, body |
| Performance | Very fast (no content inspection) | Slower (must parse HTTP) |
| SSL termination | Pass-through | Can terminate SSL |
| Content routing | No | Yes (/api → API servers, /static → CDN) |
| Examples | AWS NLB, HAProxy (TCP mode) | Nginx, AWS ALB, Envoy, HAProxy (HTTP mode) |
Load Balancing Algorithms
Sends requests to servers in sequential order: 1→2→3→1→2→3. Simplest algorithm. Assumes all servers have equal capacity.
Weighted Round Robin: assigns weights (server 1 gets 3x traffic of server 2). Useful when servers have different specs.
Advantages
- •Enables horizontal scaling
- •Eliminates single points of failure
- •Health checks auto-remove unhealthy servers
- •SSL termination offloads crypto from app servers
Disadvantages
- •Single LB is itself a SPOF (need active-passive pair)
- •Sticky sessions complicate scaling
- •L7 inspection adds latency
- •Misconfigured health checks can cascade failures
🧪 Test Your Understanding
What's the key advantage of Layer 7 over Layer 4 load balancing?