Sub topic 3
Layer 4 vs Layer 7 load balancing
This chapter assumes you know how HTTP requests and responses work, what a TCP connection is, and how to edit a configuration file. It also assumes you know what a load balancer does in general. Here you will see the two main ways it can inspect traffic and how to pick between them.
The layers in question
The numbers come from the OSI model. Layer 4 is the transport layer (TCP and UDP). Layer 7 is the application layer (HTTP, gRPC, and similar protocols).
- A Layer 4 load balancer decides using only the IP addresses and ports in the packet headers.
- A Layer 7 load balancer reads the application protocol. For HTTP it can see the method, path, host name, headers, and cookies.
How Layer 4 works
A Layer 4 balancer picks a backend when a connection opens. It then forwards every packet of that connection to the same backend. It does not parse the payload, so it works for any TCP or UDP protocol: databases, mail, game servers, or HTTP.
Because it does little work per connection, it is fast and uses little memory. If the traffic is encrypted, it never needs to decrypt it.
The cost is that it cannot see requests. If a client reuses one long-lived connection for many HTTP requests, all of them go to the same backend.
How Layer 7 works
A Layer 7 balancer terminates the client connection and reads each request. It then opens or reuses a separate connection to a backend. That gives it more abilities:
- Route by path or host name, such as
/apito one pool and everything else to another. - Balance each request on its own, even when the client keeps one connection open.
- Rewrite headers, add
X-Forwarded-For, compress responses, and cache. - Use cookies for session persistence.
- Run HTTP health checks that look at status codes.
The cost is CPU and memory, since it parses every request. It also has to decrypt HTTPS to read it, which is covered in the TLS termination chapter.
Comparing them
| Aspect | Layer 4 | Layer 7 |
|---|---|---|
| Decision input | IP, port | URL, headers, cookies, method |
| Protocols | Any TCP or UDP | Specific ones, mainly HTTP and gRPC |
| Balancing unit | Connection | Request |
| Per-connection cost | Low | Higher |
| Content routing | No | Yes |
| Sees encrypted payload | No | Only after terminating TLS |
Configuration examples
This NGINX stream block balances raw TCP to two PostgreSQL servers. NGINX never parses the database protocol.
stream {
upstream db {
server 10.0.0.21:5432;
server 10.0.0.22:5432;
}
server {
listen 5432;
proxy_pass db;
}
}This HAProxy configuration works at Layer 7. It sends paths beginning with /api to one pool and everything else to another.
frontend web
mode http
bind *:80
acl is_api path_beg /api
use_backend api if is_api
default_backend site
backend api
mode http
balance roundrobin
server api1 10.0.0.31:8080 check
server api2 10.0.0.32:8080 check
backend site
mode http
balance roundrobin
server web1 10.0.0.11:8080 check
server web2 10.0.0.12:8080 checkHAProxy uses the same program for both layers. Setting mode tcp instead of mode http selects Layer 4 behavior.
Choosing between them
Use Layer 4 when:
- The protocol is not HTTP, such as a database, message broker, or custom TCP service.
- You need the lowest latency and highest throughput.
- You want end-to-end encryption with no decryption at the balancer.
Use Layer 7 when:
- You need routing by URL or host name, for example several services behind one domain.
- You want per-request balancing, cookie persistence, or header changes.
- You want health checks that confirm the application responds correctly.
Many systems use both. A Layer 4 tier spreads connections across several Layer 7 balancers, which then route requests to services.
Common errors
- Uneven load behind Layer 4. Clients with long-lived or multiplexed connections (HTTP/2, gRPC) pin many requests to one backend. Use Layer 7 balancing for these.
- Expecting path routing at Layer 4. The balancer cannot see a path in the connection headers, and an encrypted request is unreadable to it.
- Using Layer 7 for a non-HTTP protocol. The balancer fails to parse the traffic. Use
mode tcporstream.
Exercise
A gRPC service uses HTTP/2, so each client opens one long connection and sends thousands of requests over it. A Layer 4 balancer leaves one backend overloaded. Why does this happen, and what change fixes it?
Show answer
A Layer 4 balancer chooses a backend once per connection. Every request on a client's single long connection therefore goes to the same backend, and a few busy clients can overload it. A Layer 7 balancer that understands HTTP/2 or gRPC balances each request separately, which spreads the load.