Support Library
Load Balancers: Layer 4 vs Layer 7
A Layer 4 load balancer spreads connections using only IP addresses and ports, while a Layer 7 load balancer reads the application request, such as an HTTP host and path, before choosing a backend.
The short answer
Both kinds of load balancer accept traffic on a virtual IP address (VIP) and spread it across a pool of backend servers. The difference is how much of the traffic they read. A Layer 4 balancer works at the transport layer: it sees IP addresses, TCP or UDP ports, and connection state, and forwards whole connections. A Layer 7 balancer works at the application layer: it terminates the client connection, reads the request, and can route based on hostnames, paths, headers, or cookies. The layer numbers come from the OSI model, explained in The OSI and TCP/IP Models.
Many production systems use both: a Layer 4 tier to spread raw connections across a fleet of Layer 7 proxies, which then make request-level decisions.
What every load balancer does
Whatever layer it works at, a load balancer has three jobs. It publishes a stable front door, the VIP, that clients connect to. It keeps a pool of backends and runs health checks against them, removing any that stop responding. And it chooses a backend for each new connection or request using an algorithm such as round robin, least connections, weighted distribution, or a hash of some client property so the same client tends to land on the same server.
Health checking is the part people underestimate. A balancer that only checks whether a TCP port accepts connections will keep sending traffic to an application that accepts connections and then returns errors. Layer 7 balancers can check a real request, such as an HTTP GET for a status page, and expect a specific response code.
How Layer 4 load balancing works
A Layer 4 balancer makes one decision per connection, or per flow for UDP, based on the source and destination addresses and ports. After that, it simply moves packets. It never parses the payload, so it works the same for HTTPS, SMTP, database protocols, game traffic, or DNS. There are a few common forwarding modes:
| Mode | How it works | Trade-off |
|---|---|---|
| Destination NAT | Rewrites the destination address to the chosen backend; replies return through the balancer | Simple, but the balancer carries traffic in both directions |
| Full proxy at Layer 4 | Terminates the client TCP connection and opens a new one to the backend | Backend sees the balancer's address unless the PROXY protocol is used |
| Direct server return | Forwards requests to the backend, which replies straight to the client using the VIP as its source | Very efficient for large responses, but backends need the VIP configured and the balancer never sees replies |
Large-scale Layer 4 balancers are often built as a set of identical machines that all announce the same VIP with BGP, so routers spread traffic across them with equal-cost multipath. Each machine then uses consistent hashing on the flow so that any of them would pick the same backend for a given connection. That way, when a balancer machine fails, its connections can usually continue through a neighbor.
Because Layer 4 balancers do not decrypt TLS, they never hold your certificates' private keys. That is a security advantage, and it keeps them cheap and fast. The cost is that they cannot see anything inside the encrypted stream.
How Layer 7 load balancing works
A Layer 7 balancer is a reverse proxy. It completes the TCP handshake and usually the TLS handshake with the client, reads the full request, and then opens or reuses a separate connection to the chosen backend. Because it understands the protocol, it can do things a Layer 4 balancer cannot:
- Route api.example.com and www.example.com, or /images/ and /checkout/, to different pools on the same address.
- Retry a failed request on another backend before the client notices.
- Keep a user on one backend with a cookie, rather than relying on their IP address.
- Add the client's address to a header, such as the standard Forwarded header from RFC 7239 or the common X-Forwarded-For.
- Terminate HTTP/2 or HTTP/3 from clients while talking HTTP/1.1 to older backends.
- Apply rate limits, authentication, or web application firewall rules per request.
The price is work. Every request is decrypted, parsed, and re-sent, which costs CPU and adds a little latency. The balancer holds TLS private keys, so it becomes a high-value target. And it only helps for protocols it understands; a Layer 7 HTTP proxy cannot balance an SMTP session by message content.
Side-by-side comparison
| Question | Layer 4 | Layer 7 |
|---|---|---|
| What does it read? | IP addresses, ports, protocol | Full request: host, path, headers, cookies |
| Decision made per | Connection or flow | Request |
| TLS | Passed through to backends | Usually terminated at the balancer |
| Protocols | Any TCP or UDP service | Only those it parses, mostly HTTP and gRPC |
| Client IP at backend | Preserved with direct return, or via PROXY protocol | Passed in a header |
| Performance cost | Low | Higher, grows with TLS and request volume |
| Typical use | Front tier, non-HTTP services, DNS | Web apps, APIs, microservice routing |
Balancing DNS servers themselves
DNS is an interesting case because most queries are a single UDP packet each way with no connection. A Layer 4 balancer in front of resolvers or authoritative servers can treat every query independently, which spreads load evenly. The main pitfall is source address translation: if the balancer rewrites the client's address, the DNS server sees every query as coming from the balancer, which breaks per-client access lists, rate limiting, and logging. Direct server return, or an anycast design where each server announces the service address itself, avoids that. TCP vs UDP and Why DNS Uses Both explains why the TCP side still needs connection tracking.
There are also DNS-aware proxies that act at Layer 7 for DNS: they parse each query and can route by query name or type, cache answers, or drop abusive patterns before they reach the servers behind them.
How DNS points users at a load balancer
Users reach a load balancer through DNS. If you run your own balancer, you publish its VIP as an A or AAAA record. Managed cloud balancers usually give you a hostname instead, because the addresses behind it change as the provider scales the service. You point your name at it with a CNAME:
$ dig +noall +answer www.example.com A
www.example.com. 300 IN CNAME lb-prod-1.example.net.
lb-prod-1.example.net. 60 IN A 198.51.100.20
lb-prod-1.example.net. 60 IN A 198.51.100.21
Two details matter here. First, the provider's own records often have short TTLs so it can move addresses quickly; do not copy those addresses into your zone as A records, or your site will break when they change. Second, a CNAME cannot sit at the zone apex alongside SOA and NS records, so pointing example.com itself at a balancer hostname needs a provider feature such as ALIAS or CNAME flattening, covered in CNAME at the Apex: ALIAS and Flattening.
DNS can also choose between whole sites. Global server load balancing uses health checks and DNS answers to send users to the nearest or healthiest region, each of which has its own Layer 4 or Layer 7 balancers. GeoDNS and DNS Load Balancing covers that layer.
How DNS Panopticon helps
Domain Visualizer resolves a domain's public records live, so you can see the CNAME chain and addresses that lead to your balancer. Website Recon matches CNAME suffixes, name servers, and the AS number behind each address against its provider catalog to suggest which hosting or CDN provider is in front of a name. If you have a zone export, Zone Analyzer lets you tick specific A, AAAA, and CNAME rows and open screenshots of them with Verify selected, a quick way to confirm each balanced hostname serves the page you expect.
FAQ
Is DNS round robin a load balancer?
It is a simple form of load distribution, not a load balancer. Publishing several A records spreads clients across addresses, but DNS does not check backend health in the basic case, and caching resolvers keep handing out the same answers until the TTL expires.
Can a Layer 4 load balancer handle HTTPS?
Yes. It forwards the encrypted TCP stream to a backend without decrypting it, and the backend terminates TLS. It just cannot route on anything inside the encrypted request, such as the URL path.
How do backends see the real client IP behind a load balancer?
A Layer 7 balancer usually adds it in a header such as X-Forwarded-For or Forwarded. A Layer 4 balancer either keeps the original source address, for example with direct server return, or passes it with the PROXY protocol, which the backend must be configured to accept.