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 FactorChoose L4Choose L7
Primary goalMaximum throughputContent-based routing
Traffic typeNon-HTTP (databases, game servers)HTTP/HTTPS
Routing logicRound-robin or least-connectionsURL, header, or cookie-based
Feature needsNo SSL or WAF requiredSSL termination, WAF, session persistence
ArchitectureHomogeneous backend serversMultiple microservice pools