Intermediate22 min read· Topic 4.1

Load balancers

Layer 4 vs Layer 7, algorithms, sticky sessions, health checks, GSLB

⚖️Key Takeaways

  • 1
    Layer 4 (transport) balances by IP/port — fast but no content awareness; Layer 7 (application) can route by URL, headers, cookies
  • 2
    Algorithms: Round Robin (simplest), Weighted RR, Least Connections, IP Hash (sticky sessions)
  • 3
    Health checks prevent routing to dead servers — active (ping) and passive (monitor responses)
  • 4
    Global 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.

Load Balancer Architecture
blockedClientsInternetLoad BalancerL7 / NginxServer 1Healthy ✓Server 2Healthy ✓Server 3Healthy ✓Server 4Down ✗

L4 vs L7 Load Balancing

FeatureLayer 4 (Transport)Layer 7 (Application)
Operates onTCP/UDP packetsHTTP requests
Routing criteriaIP + PortURL path, headers, cookies, body
PerformanceVery fast (no content inspection)Slower (must parse HTTP)
SSL terminationPass-throughCan terminate SSL
Content routingNoYes (/api → API servers, /static → CDN)
ExamplesAWS 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

Knowledge Check1/2

What's the key advantage of Layer 7 over Layer 4 load balancing?