Skip to content

Load Balancing


Overview

Load Balancing Overview


Layer 4 vs Layer 7 Load Balancing

L4 vs L7 Load Balancing


Load Balancing Algorithms

Load Balancing Algorithms


Health Checks

Health Checks


Session Persistence (Sticky Sessions)

Problem

User state stored in server memory requires routing same user to same server.

Methods

  1. Cookie-Based: Load balancer sets cookie with server ID
  2. IP-Based: Hash client IP (problem: NAT, shared IPs)
  3. URL/Header Based: Use session ID in URL or header

Drawbacks of Sticky Sessions

  • Server failure loses session data
  • Uneven load distribution
  • Scaling complications

Better Alternative

Externalize session to Redis/Memcached so any server can handle any request.


SSL/TLS Termination

SSL/TLS Termination Options


Global Load Balancing (GSLB)

Global Server Load Balancing


Load Balancer Architectures

Load Balancer Architectures


Software

  • NGINX: L7 load balancer and reverse proxy, high performance
  • HAProxy: L4/L7 load balancer, very high performance
  • Envoy: Modern L7 proxy, service mesh data plane (Istio)
  • Traefik: Cloud-native, Kubernetes-friendly

Cloud Services

  • AWS: ALB (L7), NLB (L4), CLB (legacy)
  • GCP: Global HTTP(S) Load Balancer, Network Load Balancer
  • Azure: Application Gateway (L7), Load Balancer (L4)

NGINX Configuration Example

# Layer 7 load balancing configuration
upstream backend {
    # Load balancing algorithm
    least_conn;  # or: round_robin, ip_hash, random

    # Backend servers with weights
    server 10.0.0.1:8080 weight=5;
    server 10.0.0.2:8080 weight=3;
    server 10.0.0.3:8080 weight=2 backup;

    # Keep alive connections to backend
    keepalive 32;

    # Health check (NGINX Plus only)
    # health_check interval=5s fails=3 passes=2;
}

server {
    listen 443 ssl http2;
    server_name api.example.com;

    # SSL termination
    ssl_certificate /etc/ssl/certs/cert.pem;
    ssl_certificate_key /etc/ssl/private/key.pem;

    location / {
        proxy_pass http://backend;

        # Headers
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;

        # Timeouts
        proxy_connect_timeout 5s;
        proxy_read_timeout 60s;

        # Keep alive
        proxy_http_version 1.1;
        proxy_set_header Connection "";
    }

    # Health check endpoint (bypass backend)
    location /health {
        access_log off;
        return 200 "healthy\n";
    }
}

Interview Questions

  1. L4 vs L7 load balancing?
  2. L4: TCP level, faster, no content inspection
  3. L7: HTTP level, content routing, SSL termination

  4. How do you handle sticky sessions?

  5. Cookie-based affinity
  6. Better: Externalize session to Redis

  7. What happens when a server goes down?

  8. Health check detects failure
  9. Traffic routed to healthy servers
  10. Optional: Connection draining

  11. How do you handle SSL certificates?

  12. Terminate at LB (simpler, offloads CPU)
  13. Or passthrough (end-to-end encryption)
  14. Or re-encrypt (both L7 features and internal encryption)

  15. How do you ensure LB high availability?

  16. Active-passive with heartbeat/VRRP
  17. Active-active with DNS
  18. Cloud managed (auto-scaling, multi-AZ)

  19. How do you handle global traffic?

  20. GSLB with DNS-based routing
  21. Anycast for same IP across regions
  22. GeoDNS for regional routing

  • *