Balance by connection, or read the request and balance by what it asks for.
Load balancers work at different layers of the network. A Layer 4 balancer looks only at IP addresses and ports. A Layer 7 balancer reads the HTTP request itself.
The numbers 4 and 7 come from the OSI model. Layer 4 is the transport layer (TCP and UDP, with ports). Layer 7 is the application layer (HTTP, gRPC, and so on). A load balancer is named after the highest layer it understands.
A Layer 4 load balancer sees a connection as a source address, a source port, a destination address and a destination port. It picks a backend server for the connection and then forwards the packets of that connection to the same server. It never needs to understand what the bytes mean, so it works for web traffic, databases, game servers, DNS or anything else on TCP and UDP.
A Layer 7 load balancer accepts the connection itself, reads the full HTTP request (method, host, path, headers, cookies), and then decides where to send that request. It usually opens its own connection to the backend. This lets it do clever things, but it has to do much more work for each request.
| Look at | Example rule | Use |
|---|---|---|
| Host header | api.example.com goes to the API pool | Many sites behind one IP address |
| URL path | /static/* goes to the static servers | Split one site across services |
| Header | x-beta: 1 goes to the new version | Testing and canary releases |
| Cookie | A session cookie goes to the same server | Sticky sessions |
| Method | POST goes to the write cluster | Separate read and write paths |
| Method | How it works | Good and bad |
|---|---|---|
| Proxy (connection split) | Accepts the client connection and opens a second one to the server | Simple and flexible. The server sees the balancer's address unless the client IP is passed on (for example with the PROXY protocol). |
| NAT (address rewrite) | Rewrites the destination address of packets to the chosen server | Fast. Replies must go back through the balancer. |
| Direct server return | The server replies straight to the client, skipping the balancer | Very high throughput for big replies. Needs special network set-up. |
Simplified: every request here is its own connection, and Layer 4 takes the five servers in turn. In real life one connection often carries many requests, so Layer 4 balances connections, not requests. The work units are made up to show the shape of the trade-off (1 for Layer 4, 4 to read HTTP, +3 to decrypt).
location /api/ { proxy_pass http://api_pool; }
location /static/ { proxy_pass http://static_pool; }
location / { proxy_pass http://web_pool; }| Term | Meaning in easy words |
|---|---|
| Layer 4 | The transport layer: TCP and UDP, with ports |
| Layer 7 | The application layer: protocols such as HTTP |
| Connection-level balancing | Choosing a server once per connection (Layer 4) |
| Request-level balancing | Choosing a server for each request (Layer 7) |
| TLS termination | Decrypting HTTPS at the balancer |
| TLS passthrough | Forwarding encrypted traffic without reading it |
| Content-based routing | Routing by host, path, header or cookie |
| Reverse proxy | A server that accepts client requests and forwards them to backends |
| X-Forwarded-For | An HTTP header that carries the original client IP through proxies |
| PROXY protocol | A small header that passes the client address through a Layer 4 proxy |
| Direct server return | A mode where the server answers the client without going back through the balancer |
| Choice | We gain | We lose |
|---|---|---|
| Layer 4 only | Speed, simplicity, any protocol | No content-based routing |
| Layer 7 only | Routing, TLS handling, rich features | More CPU and more things that can go wrong |
| L4 in front of L7 | Scales the smart tier, keeps routing power | Two layers to run and monitor |
| Terminate TLS at the balancer | Routing and one place for certificates | Traffic inside is plain unless you re-encrypt |
| TLS passthrough | Encrypted all the way to the backend | No Layer 7 routing on the content |
| Per-request balancing | Even load, even with long-lived connections | More work for the balancer |