- rtshkmr's digital garden/
- References/
- Architecture Design Basics/
- Pattern Taxonomy/
- Scaling & Performance/
- Load Balancing Patterns/
Load Balancing Patterns
··
308 words·
2 mins
Table of Contents
🟠P1 — distributing traffic across service instances
Problem #
Multiple instances need traffic distributed among them.
L4 vs L7 Load Balancing #
| Layer | Operates on | Routing decisions based on | Examples |
|---|---|---|---|
| L4 | TCP/UDP packets | IP address, port | AWS NLB, HAProxy |
| L7 | HTTP requests | URL path, headers, cookies, content | AWS ALB, NGINX |
Algorithms #
| Algorithm | Mechanism | Best for |
|---|---|---|
| Round-Robin | Rotate through instances | Equal-capacity instances |
| Weighted Round-Robin | Rotate with weights per instance | Mixed-capacity instances |
| Least Connections | Route to instance with fewest active | Long-lived connections |
| IP Hash | Hash client IP to determine instance | Session affinity without cookies |
Instinct #
L7 for most application traffic (path-based routing, health checks, SSL termination). L4 for high-throughput, latency-sensitive traffic (database connections, gRPC streams).
Sticky sessions are a code smell — they usually mean the service is stateful when it shouldn’t be. Fix the statefulness.
Rule of thumb to choose L4 vs L7 LB: #
Persistent connections (e.g. websockets)? Then use a L4 LB
Else use a L7 LB that gives flexibility for routing traffic to services while also minimising the connection load downstream
- INTERVIEW: Choosing L4 vs L7 LB comes up specifically in system design interviews for real-time features (chat, live updates, gaming). Pair the LB choice with the protocol choice:
- WebSocket-based design → L4 LB (preserves TCP session)
- HTTP-based long-polling or SSE → L7 LB (flexible routing)
- INSIGHT: DNS provides rudimentary client-side load balancing (rotated IP lists). This avoids single-point-of-failure problems but can’t react quickly to server health changes.
- PITFALL: Problems with extremely high traffic need specialised hardware LBs that outperform software LBs by orders of magnitude.
- EXP: I’ve used AWS ELB, NGINX. NGINX is the most flexible for L7; HAProxy is battle-tested for L4.
References #
- Load Balancing — Sam Rose; excellent interactive visual explainer
- Introduction to Modern Network Load Balancing and Proxying — Matt Klein (Envoy creator)
- Google SRE: Load Balancing at the Frontend