Back to 20 Concepts
scalingIntermediate

Load Balancing: Layer-4 vs Layer-7 & Routing Algorithms

Layer-4 load balancers (IP/TCP port level, e.g. AWS NLB, IPVS) route millions of raw packets with microsecond latency. Layer-7 load balancers (HTTP/gRPC header inspection, e.g. NGINX, Envoy) route based on path, cookies, and TLS SNI.

Intuitive Mental Model

The Post Office vs The Mail Sorting Clerk: Layer-4 is the postal truck driver who only looks at the zip code on the outside envelope. Layer-7 is the clerk who opens the letter, reads the recipient department name, and routes it to billing or support.

Architecture Blueprint & CodeProduction Standard
// NGINX Layer-7 Routing:
// location /api/v1/orders { proxy_pass http://order_cluster; }
// location /api/v1/users  { proxy_pass http://user_cluster; }

Key Architectural Takeaways

  • Layer-4 (Transport): Extremely high throughput (10M+ PPS), zero payload inspection, terminates TCP connections.
  • Layer-7 (Application): Intelligent path routing, SSL/TLS termination, rate limiting, and sticky session cookies.
Common Architectural Pitfall

Using Round-Robin on long-lived WebSocket or gRPC HTTP/2 streams, resulting in severe server load imbalances.

Production Best Practice

Use Least-Connections or Resource-Based load balancing for long-lived persistent streams.