Load Balancing Cheat Sheet

Load balancing algorithms (round robin, least connections, IP hash), Layer 4 vs Layer 7, health checks, and session persistence strategies.

Last Updated: May 1, 2025

Load Balancing Algorithms

ItemDescription
Round RobinDistributes requests sequentially across all servers — simplest
Weighted Round RobinAssign weights to servers with different capacities
Least ConnectionsSends to server with fewest active connections (sticky workloads)
Weighted Least ConnLeast connections + server weight (best for mixed hardware)
IP HashHash client IP → same server every time (session persistence without cookies)
Least Response TimeSends to server with lowest latency (NGINX Plus / HAProxy)
RandomPure random distribution — surprisingly effective at scale
URL HashHash the request URL — good for caching proxies

Layer 4 vs Layer 7

FeatureLayer 4 (Transport)Layer 7 (Application)
Works atTCP/UDP levelHTTP/HTTPS level
Routing decisionsIP + port onlyURL path, headers, cookies, method
SSL terminationPassthrough (or raw TCP)Terminate/re-encrypt (inspect traffic)
PerformanceHigher throughput, lower latencyMore CPU, more features
Content switchingNoYes (route /api to backend A, /static to B)
Protocol supportAny TCP/UDPHTTP/HTTPS only

Health Checks

ItemDescription
TCP checkCan the port be reached? Simple, fast
HTTP checkGET /health returns 200? More reliable
Script checkRun custom script — most flexible
Active checksLoad balancer probes servers (default)
Passive checksMonitor actual responses for errors (HAProxy observe)
Unhealthy thresholdHow many consecutive failures before marking DOWN

Session Persistence (Sticky Sessions)

ItemDescription
Cookie-basedLB inserts cookie; subsequent requests route to same backend
IP-basedSame client IP → same server (IP hash algorithm)
SSL Session IDStick to server based on TLS session ID
Trade-offPersistence breaks even distribution — use only when necessary
Pro Tip: Layer 7 gives you content-based routing (URL, headers, cookies). Layer 4 is faster and protocol-agnostic. Start simple — round robin works fine for stateless apps.