Load balancing at layer 4 and layer 7
A layer 4 balancer picks a backend per flow from addresses and ports; a layer 7 balancer reads each HTTP request and routes on host, path, header or cookie. The choice fixes what a design can route on and see.
What it is
The layer names come from the OSI model and name the protocol layer the balancer works at when it chooses a backend. A layer 4 balancer works from the packet headers: source and destination IP address, port and protocol, with TCP and UDP among the protocols it handles. A layer 7 balancer works from the application protocol, most often HTTP: it reads the request and can route on the host, the path, a header, a cookie, the method or a query parameter. Google Cloud describes its layer 4 products as routing on network and transport data and its layer 7 products as adding decisions based on the HTTP header and URI; AWS places its Application Load Balancer at layer 7 and its Network Load Balancer at layer 4.
The difference that matters in a design is the unit of decision. A layer 4 balancer decides once, when a connection (or a UDP flow) starts, and then forwards bytes it doesn't interpret. A layer 7 balancer ends the client's connection at itself, reads each request, applies its rules and sends the request on to the backend it picked. So two requests on the same client connection can go to different backends at layer 7, and at layer 4 they can't be told apart. Cloudflare's note on its TCP-level product makes the same point from the other side: at that level it lacks the information for session affinity by cookie, content rules or caching.
Two questions hide under "layer 4": does the balancer rewrite packets, or does it proxy the connection? Google Cloud's passthrough Network Load Balancer isn't a proxy. Packets reach the backend with the client's source address, protocol and ports unchanged, the backend terminates the connection, and its replies go straight back to the client rather than through the balancer, a pattern called direct server return. AWS's Network Load Balancer is also layer 4 but does terminate the flow, so the backend talks to the balancer, not the client. Both kinds route by connection; only the passthrough kind leaves the packets as they were.
| What you get | Layer 4 | Layer 7 |
|---|---|---|
Protocols | TCP, UDP, TLS listeners on AWS; TCP, UDP, ESP, GRE, ICMP on Google Cloud passthrough | HTTP, HTTPS, gRPC on AWS; WebSockets on both AWS types |
Routing inputs | Address, port, protocol | Host, path, header, method, query, source IP, in rules checked by priority |
TLS | Passthrough leaves decryption to backends; AWS's NLB can terminate TLS on a TLS listener | Terminates TLS to read the request; AWS's ALB supports SNI and ALPN |
Actions beyond forwarding | None on the content; AWS's NLB can still terminate the flow and offload TLS | Redirects, fixed responses, user authentication before routing |
Health checks (AWS) | TCP, HTTP or HTTPS | HTTP, HTTPS or gRPC |
Stable address | Static or Elastic IP on AWS's NLB | Reached through a DNS name on AWS's ALB |
How the balancer spreads load is a separate axis from which layer it reads. NGINX's stream module, its layer 4 proxy for TCP and UDP, uses round robin unless told otherwise, offers least connections, and can hash a key such as the client address so one client keeps reaching the same server, with a consistent option that applies ketama consistent hashing so a server change moves few keys. AWS's Application Load Balancer defaults to round robin per target group and can switch to least outstanding requests. Sticky sessions on AWS are set per target group on both types.
When to use it
Reach for layer 7 when the traffic is HTTP and the backends differ by content: /api to one pool and /static to another, several hostnames behind one address, a cookie that keeps a user on one server, a redirect from the old URL, or a sign-in step before the request reaches the service. AWS's Application Load Balancer documents path and host conditions, header, method and query conditions, redirects, fixed responses and user authentication; Cloudflare's layer 7 balancer adds session affinity and faster failover than DNS. A layer 7 balancer also decides per request, so a client that holds one connection open and sends many requests can be spread across backends, unless a sticky rule keeps it on one.
Reach for layer 4 when no proxy in the path needs to parse the protocol (a database protocol, a game server over UDP, DNS), when the backend must see the client's real source address in the packets, when TLS has to end at the backend rather than at the balancer, or when you need a fixed IP address to hand to a partner's firewall. Google Cloud names source IP preservation, non-TCP protocols and backend-side TLS as the reasons to pick its passthrough balancer; AWS recommends its Network Load Balancer for extreme performance and a static IP, and lists long-lived TCP connections as a feature of that type.
The two stack well. A layer 4 balancer in front gives a stable address and spreads connections cheaply; a tier of layer 7 proxies behind it reads requests and routes them. AWS allows an Application Load Balancer as a target of a Network Load Balancer for exactly this arrangement.
When not to
Don't put a layer 7 balancer in front of traffic it won't parse. A balancer that only speaks HTTP can't proxy a PostgreSQL or Redis connection; a layer 4 balancer is the right tool. Don't terminate TLS at the balancer when the requirement is that the operator of the balancer never reads the payload: pass the connection through and accept that the backends spend the CPU on decryption, which Google Cloud calls out as the cost of that choice.
Don't pick layer 4 and then expect content behavior from it. Cookie stickiness, path rules, redirects and per-request balancing all need the request to be read, and a layer 4 balancer has no view of it. If the design needs one of those, the balancer needs to be layer 7 or needs a layer 7 tier behind it.
Sometimes the weaker option is right. DNS-based balancing, which hands back the address of a healthy endpoint, fails over more slowly because resolvers cache answers and offers no session affinity, but Cloudflare notes it's the mode to use for a hostname that carries MX or SRV records, where a proxy can't sit in the path. And a single service with uniform requests behind a layer 4 balancer is a perfectly good design; adding layer 7 there buys parsing cost without a routing rule to justify it.
Compared
| Question | Layer 4 | Layer 7 | When the layer 4 answer is the right one |
|---|---|---|---|
When is the backend chosen? | Once per connection or flow | Once per request | Connections are short or requests on a connection are uniform, so per-request spreading gains little |
What can it route on? | Address, port, protocol | Host, path, header, cookie, method, query | One backend pool, no content rules |
Who decrypts TLS? | Backends in passthrough; the balancer if it has a TLS listener | The balancer, to read the request | The payload must stay private to the backend, or the backends already hold the certificates |
Does the backend see the client address? | Yes in passthrough, in the packet itself | Not in the packet once the proxy terminates the connection; the proxy has to be set up to forward it, which AWS lists as a feature of both its types | Firewalling or logging by source address at the backend, or non-HTTP protocols with no header to carry it |
What else can it do? | Static IP, direct server return, long-lived connections | Redirects, fixed responses, authentication, cookie stickiness | None of those actions are needed |
What does it cost? | Little work per byte | Parsing every request, plus TLS when it terminates it | Throughput is the constraint and the traffic isn't HTTP |
In a picture
Step 1 of 3
One connection, one choice
A client opens a TCP connection. The layer 4 balancer reads the addresses and ports, picks a backend, and from then on forwards every byte of that connection to it without looking inside.
How to say it in an interview
The common mistake
Quiz
Questions that use this
Related
Sources
- AWS: Elastic Load Balancing features
- AWS documentation: What is an Application Load Balancer?
- Cloudflare Learning Paths: Types of load balancers
- Google Cloud documentation: Cloud Load Balancing overview
- Google Cloud documentation: Passthrough Network Load Balancer overview
- NGINX documentation: TCP and UDP Load Balancing