Two Places to Balance
A load balancer spreads traffic across backends. It can operate at layer four, the transport layer, or at layer seven, the application layer.
Layer 4 Balancing
A layer four balancer routes by IP address and port without inspecting the payload.
- It forwards TCP or UDP connections quickly with little processing.
- It cannot read URLs, headers, or cookies.
- It preserves the connection end to end, often using techniques like direct return.
This makes layer four very fast and protocol agnostic, ideal for raw throughput.
Layer 7 Balancing
A layer seven balancer terminates the connection and reads the application request.
- It can route by path, host, header, or cookie.
- It can terminate TLS, rewrite requests, and apply per route policies.
- It enables content based routing, sticky sessions, and request level retries.
The cost is more CPU per request and added latency from terminating and re originating connections.
Choosing
Use layer four when you need maximum speed and protocol neutrality, and layer seven when routing decisions depend on application data. Many architectures combine both, with layer four at the edge feeding layer seven proxies.
Key idea
Layer four balances fast by IP and port, while layer seven reads the request to route by path and headers at higher cost.