Concept
Core Concepts & L4 vs L7
What a Load Balancer Does
A load balancer sits between clients and backend servers, distributing incoming requests across a pool of identical servers. It solves two problems simultaneously: the single-server performance ceiling and the single point of failure.
Without a load balancer, scaling is vertical — expensive, finite, and still a SPOF. With one, scaling is horizontal: add commodity servers, and the load balancer distributes the work.
Common Distribution Algorithms
- Round Robin — Requests distributed sequentially. Best when servers are identical and requests have similar cost.
- Least Connections — Route to the server with the fewest active connections. Better when request cost varies significantly.
- IP Hash — Route based on a hash of the client IP, ensuring the same client always reaches the same server. Used for sticky session requirements.
L4 vs L7 Load Balancers
Layer 4 (Transport Layer) — Operates at TCP/UDP level. Forwards packets based on IP and port without inspecting content.
- Strengths: Extremely fast; minimal CPU overhead; handles millions of connections per second.
- Use when: Maximum throughput is critical; traffic is non-HTTP (game servers, databases, streaming); no content-based routing needed.
Layer 7 (Application Layer) — Terminates the TCP connection and inspects the HTTP request before routing. Routes based on URL path, headers, or cookies.
- Strengths: Intelligent routing (e.g.,
/api/*to API servers,/static/*to file servers); SSL termination; WAF integration; session persistence. - Trade-off: Higher CPU and memory cost than L4.
| Decision Factor | Choose L4 | Choose L7 |
|---|---|---|
| Primary goal | Maximum throughput | Content-based routing |
| Traffic type | Non-HTTP (databases, game servers) | HTTP/HTTPS |
| Routing logic | Round-robin or least-connections | URL, header, or cookie-based |
| Feature needs | No SSL or WAF required | SSL termination, WAF, session persistence |
| Architecture | Homogeneous backend servers | Multiple microservice pools |