On this page
What a Layer 4 balancer seesWhat a Layer 7 balancer seesURL switching and content switchingTLS termination and its costPerformance and latencyHeader insertion: X-Forwarded-For and friendsDecision tableQuestions01 /What a Layer 4 balancer sees
A Layer 4 balancer works on the transport header: source and destination IP address, protocol number, source and destination port, and TCP flags. It sees the SYN, picks a real server, records the flow and forwards every subsequent packet of that connection by table lookup. It never assembles the byte stream and never knows what the payload is. That is its strength: it balances anything over TCP or UDP, at line rate, with one decision per connection. The basic model is described under server load balancing.
02 /What a Layer 7 balancer sees
A Layer 7 balancer completes the TCP handshake itself, receives the request, parses it and only then chooses a real server, to which it opens a second connection. Because it holds the full request it can route on the Host header, the URL path, a query parameter, a cookie, the method or the content type. It can rewrite headers, insert its own, redirect, compress, cache and apply persistence based on cookies. It is a proxy; every byte in both directions passes through its HTTP engine.
03 /URL switching and content switching
Content switching sends different parts of one site to different pools from one VIP: /images/ to static servers, /api/ to application servers, /admin/ to a restricted pool, with the Host header separating tenants that share the address. Foundry published a URL switching application note around 2001 and grouped the ServerIron GT, 4G and XL under an "application switch" product category, a label that meant Layer 7 aware load balancing in hardware. A whitepaper on balancing Microsoft Office Communications Server 2007 front-end pools belonged to the same family: the balancer had to understand the protocol well enough to keep a signalling session on one server.
04 /TLS termination and its cost
To read HTTP inside HTTPS the balancer must hold the certificate and private key and terminate TLS. That concentrates key management (convenient) and asymmetric cryptography (expensive) in one place. The full handshake dominates: a new TLS session costs orders of magnitude more CPU than forwarding a Layer 4 flow, which is why appliance vendors sold hardware acceleration and why balancer datasheets quote TLS handshakes per second separately from plain connections per second. Session resumption spreads the cost. Traffic to the real servers can be plain HTTP or re-encrypted, which doubles the crypto work.
05 /Performance and latency
Layer 4 adds microseconds: one table lookup and a header rewrite per packet, often in switching silicon. Layer 7 adds a full proxy hop: TCP handshake with the client, request parse, second handshake to the server, then two buffered streams. Expect connections per second to drop by an order of magnitude when a device moves from Layer 4 to Layer 7, and added latency in the low milliseconds per request. Neither matters until the balancer, not the servers, is the bottleneck.
06 /Header insertion: X-Forwarded-For and friends
In NAT or proxy mode the real server sees the balancer as the client, so logs, rate limits and geolocation all break. A Layer 7 balancer fixes this by inserting X-Forwarded-For with the original client address, X-Forwarded-Proto with the original scheme, or the standard Forwarded header (RFC 7239). Servers must trust these headers only from the balancer address and strip any copy a client sent, or the client can forge its own source. Layer 4 balancers cannot insert headers; they preserve the client address instead, which is why direct server return and Layer 4 are often chosen together.
If the routing decision can be made from the destination port alone, stay at Layer 4. Move to Layer 7 only when a decision needs something inside the request, and then terminate TLS on the balancer and insert X-Forwarded-For so the servers still know who called.
07 /Decision table
| Requirement | Layer 4 | Layer 7 |
|---|---|---|
| Non-HTTP protocol (database, mail, game, VPN) | Yes | Only with a protocol-specific module |
| Route by URL path or Host header | No | Yes |
| Cookie persistence | No | Yes |
| Pass through TLS without holding the key | Yes | No |
| Highest connections per second | Yes | No |
| Original client IP visible to server | Yes (DSR or transparent) | Via inserted header |
| Redirects, rewrites, compression, caching | No | Yes |
The same logic applies one level down, where a Layer 2 versus Layer 3 switch trades simplicity for routing awareness: the higher layer is chosen for what it sees and paid for in throughput.
08 /Questions
Can a Layer 4 balancer route by URL?
No. The URL lives in the HTTP request, which a Layer 4 device never parses. It forwards based on IP addresses and ports only. Routing by path, Host header or cookie requires a Layer 7 balancer that terminates the connection and reads the request.
Does Layer 7 load balancing require TLS termination?
For HTTPS, yes. The request is encrypted, so the balancer must hold the certificate and key to read it. Plain HTTP can be inspected without termination. A Layer 4 balancer passes encrypted traffic through untouched, which is its advantage when keys must stay on the servers.
How much slower is Layer 7 than Layer 4?
Typically an order of magnitude fewer connections per second on the same hardware, plus low single-digit milliseconds of added latency per request. TLS handshakes cost more again. Whether it matters depends on whether the balancer or the servers saturate first.
What is X-Forwarded-For for?
It carries the original client IP address to the real server when a proxy-mode balancer has replaced it with its own. Servers should accept the header only from the balancer address and discard any value the client supplied, otherwise the client can spoof its own origin.
What did Foundry mean by an application switch?
It was the marketing category for the ServerIron line: a switch with Layer 4 through 7 awareness that could balance servers, switch on URL content and terminate SSL in hardware. The term described what is now usually called an application delivery controller.
Sources
- IETF RFC 7239, Forwarded HTTP Extension, 2014
- IETF RFC 9110, HTTP Semantics, 2022
- IETF RFC 8446, The Transport Layer Security (TLS) Protocol Version 1.3, 2018
- Foundry Networks, URL Switching application note and application switch product pages (original Foundry documentation, historical, around 2001 to 2008)