System Design Fundamentals
11 items
11 items
From round-robin to consistent hashing - distributing traffic at every layer
Load balancing distributes incoming traffic across multiple servers to improve availability, throughput, and response time. It operates at different layers: L4 (transport) makes decisions based on IP/port, while L7 (application) can route based on HTTP headers, URLs, or content. The choice of algorithm matters—round-robin is simple but ignores server load, while least-connections adapts to actual capacity. For stateful applications, sticky sessions or external session stores solve the affinity problem. At global scale, GeoDNS and Anycast route users to nearby data centers.
Layer 4 load balancers see only IP addresses and ports—they're fast (millions of connections/second) but blind to application logic. Layer 7 balancers understand HTTP, can route by URL or header, terminate SSL, and compress responses—but with higher CPU cost per request.
Round-robin assumes all requests are equal. If one request takes 10ms and another takes 1000ms, round-robin creates imbalance. Least-connections sends new requests to servers with fewest active connections, naturally adapting to request complexity.
When adding or removing servers, simple hash-based routing (hash % N) reshuffles most requests. Consistent hashing only moves K/N keys (where K is total keys, N is servers). This is essential for caches and stateful services.
Load balancing distributes incoming requests across multiple servers. It serves three purposes:
Load balancers act as a reverse proxy—clients talk to the balancer, which forwards requests to backend servers. Clients are unaware of the backend topology.
Where load balancers sit:
| Layer | Between | Examples | |-------|---------|----------| | Edge | Internet → Data center | Cloudflare, AWS ALB | | Internal | Service → Service | HAProxy, Envoy | | Database | App → DB replicas | ProxySQL, PgBouncer | | DNS | User → Data center | Route53, Cloudflare DNS |
Load balancers can be hardware appliances (F5, Citrix), software (HAProxy, Nginx), or cloud services (AWS ALB/NLB, GCP Load Balancer).