Designing Load Balancer Architecture

1. Understanding Load Balancing Algorithms

AlgorithmBehaviorBest For
Round RobinCycle through backendsEqual-capacity nodes
Least ConnectionsSend to backend with fewest active connsLong-lived requests
Least Response TimeLowest p50 latencyHeterogeneous backends
IP HashHash client IP → backendSticky sessions w/o cookies
Random (P2C)Pick 2 random, choose less loadedScales well, low coordination
Consistent HashStable mapping by keyCaches, sharded backends

2. Understanding Weighted Load Balancing

VariantUse Case
Weighted Round RobinBackends with different CPU/mem
Weighted Least ConnMixed instance sizes
Weighted RandomCanary: 95% old, 5% new
Dynamic weightsEWMA of latency adjusts weight

3. Designing Layer 4 Load Balancing

PropertyL4
LayerTCP/UDP
InspectionIP + port only
LatencySub-ms
ThroughputMillions of pps
ExamplesAWS NLB, IPVS, HAProxy TCP, MetalLB
Best ForHigh-throughput TCP, gRPC, databases

4. Designing Layer 7 Load Balancing

PropertyL7
LayerHTTP/HTTPS, gRPC
InspectionHeaders, paths, cookies
FeaturesRouting, rewrites, auth, WAF
Latency overhead1–5ms
ExamplesAWS ALB, Envoy, NGINX, Traefik, HAProxy HTTP

5. Configuring Health Checks and Failover

TypeDescriptionTrade-off
TCPOpen socket on portDoesn't detect app health
HTTPGET /health expecting 200Most common
ActiveLB initiates check periodicallyStandard
PassiveDetect via real traffic errorsFaster but noisier
Deep/health checks DB, cache, depsRisk of cascading marks
Note: Use shallow checks for liveness, deep checks separately. Consecutive failures (e.g., 3) before marking unhealthy avoids flapping.

6. Designing Session Persistence

MethodMechanismTrade-off
Cookie-basedLB sets sticky cookieWorks behind NAT
IP affinityHash client IPBreaks behind shared NAT
Application cookieApp emits session cookieTied to app version
External sessionRedis/DB-backed sessionsStateless app, slight latency

7. Using DNS-Based Load Balancing

PolicyBehavior
Round-robin DNSMultiple A records
GeoDNSRoute by client geography
Latency-basedRoute to lowest-RTT region
WeightedTraffic split for canaries
FailoverHealth-check + secondary record
Warning: DNS TTLs cached by clients/resolvers. Failover via DNS can take minutes; pair with VIP/anycast for fast failover.

8. Implementing Global Server Load Balancing (GSLB)

FeatureDescription
AnycastSame IP advertised from many sites; BGP routes to nearest
GeoDNSDNS responses vary by client region
Health-awareDrains unhealthy regions
ExamplesAWS Global Accelerator, Cloudflare, Akamai GTM

9. Designing SSL/TLS Termination

ModeWhere TLS EndsNotes
Termination at LBLB decrypts; backend HTTPCentralized certs, easier WAF
PassthroughEnd-to-end TLSBackend owns certs; no L7 features
Re-encryptionDecrypt + re-encryptL7 features + in-transit encryption
Note: Use TLS 1.3, OCSP stapling, automate cert rotation (ACM, cert-manager).

10. Implementing Rate Limiting at Load Balancer

StrategyDescription
Per-IPX req/sec per source IP
Per-API keyTiered quotas per consumer
GlobalCap total RPS to backend
Burst + sustainedToken bucket: burst capacity + refill
ResponseHTTP 429 with Retry-After header

11. Designing Reverse Proxy Architecture

CapabilityExamples
RoutingPath/host/header rules
CachingStatic assets, idempotent GETs
Compressiongzip, brotli
Auth offloadOIDC, JWT validation
SoftwareNGINX, Envoy, HAProxy, Traefik, Caddy

12. Designing Load Balancer High Availability

   Clients ──▶ VIP (anycast/floating) ──▶ Active LB ──▶ Backends
                       │                       │
                       └───────keepalived──────┘
                              VRRP heartbeat
                                  │
                            Standby LB (takes over)
      
PatternMechanism
Active-passiveVRRP / keepalived floating VIP
Active-activeECMP + anycast across LB nodes
Cloud-managedAWS ALB/NLB, GCP LB (multi-AZ built-in)
DSR (direct server return)Egress bypasses LB; lower load