L7 vs L4 Load Balancers
Packet-level vs request-level routing
Layer 4 balancers forward packets/TCP by IP+port with minimal intelligence; layer 7 balancers read HTTP(S) content (paths, headers, cookies) to route precisely. Knowing which one serves which protocol shape is the core decision.
What you will learn
- Compare L4 (TCP/UDP) and L7 (HTTP/HTTPS) behavior
- Choose the right balancer class per workload
- Explain TLS termination's role at L7
L4 (NLB, Azure LB/TCP proxy) forwards connections by IP/port with near-zero overhead. L7 (ALB, App Gateway, GCP HTTP LB) reads the request — path, host, cookies — enabling microservice routing, WAF and traffic shaping at some cost.
What is this?
L4: AWS NLB, Azure Load Balancer, GCP TCP/UDP proxy. Fast, connection-preserving, works for any TCP/UDP protocol (gRPC, MQTT, game servers, databases). L7: AWS ALB, Azure Application Gateway, GCP external HTTP(S) LB. Route by path/host/header/query, rewrite, sticky by cookie, TLS termination, WAF. L4 keeps source IP (proxy protocol optional); L7 inserts X-Forwarded-* headers. L4 = no protocol awareness (UDP supported); L7 = HTTP/2, WebSocket, gRPC-aware.
Why do you need it?
- L4 sustains millions of connections/sec with tiny overhead — IoT, game, VPN transports need it.
- L7 gives precise routing so /api vs /static split cleanly across services.
- TLS at L7 centralizes certificates; L4 passes raw TLS (you terminate elsewhere).
- Protocol choice drives everything: you can't route HTTP on a dumb TCP balancer.
Mental model
Receptionist who just points to a queue vs one who reads your ticket
L4 vs L7
L4: taken to whatever door matches the floor number (TCP port). L7: reads your request subject (path) and sends you to accounts or support — more work, way more useful routing.
How it works
- L4: forwards TCP/UDP flows between client and chosen target, keeping a 5-tuple flow table; no payload inspection.
- L7: terminates HTTP(S), parses headers/path, applies rules, then re-encrypts (or passes HTTP) to the target.
- L7 inserts X-Forwarded-For/Proto/Host so apps see real clients behind the proxy.
- Sticky sessions at L7 use cookies; at L4 use source-IP affinity (client IP to same target).
- TLS: L7 offload terminates cert; L4 can do TLS passthrough or TCP+TLS termination (signatures none).
Two balancer lenses.
Examples
Route by path (L7) and forward TCP (L4)
# ALB rule: /api -> api target group, everything else -> webaws elbv2 create-rule --listener-arn <arn> --priority 10 --conditions Field=path-pattern,Values=/api/* --actions Type=forward,TargetGroupArn=<api-tg># NLB: TCP passthrough for a game serveraws elbv2 create-load-balancer --name game-lb --type network --subnets subnet-aaaa subnet-bbbbL7 rules read the request; L4 just forwards the TCP stream.
In production
- A VoIP platform forwards UDP via NLB to media servers preserving source IP.
- App Gateway routes /api plus WAF and header rewrites at the edge.
- GCP HTTP LB serves gRPC (h2c) with path-based backend selection.
Best practices
- Use L7 when you need routing/rewrites/WAF; L4 when you need raw protocol speed.
- Terminate TLS at L7 for central cert management; use L4 passthrough for end-to-end TLS.
- Preserve client context: enable proxy protocol on L4 or rely on X-Forwarded-* at L7.
- Match the balancer class to the fleet's protocol before sizing capacity.
Common mistakes
- Routing gRPC or WebSocket over an L4 box and losing path-based scaling.
- Using L7 for a TCP stream (it can't parse frames) → just a very expensive TCP proxy.
- Trusting X-Forwarded-For blindly from CDNs (spoofing) — validate with trusted hops.
- Putting L7 behind L4 and losing client IP unless proxy protocol is forwarded.
Common issues
L7 balancer drops long-lived WebSocketL7 idle timeout or HTTP/1.1 upgrade handling
Raise idle timeouts / enable WebSocket on the LB or route that traffic over an L4.
UDP traffic silently droppedL7 (HTTP) balancer can't carry UDP
Switch to an L4/TCP-UDP balancer for non-HTTP transports.
Debugging tips
- L7 rules not firing: check path-pattern match and rule priority order.
- 'Connection reset' on an L4: verify the flow table and target port; L4 won't tell you about HTTP.
- X-Forwarded-*-confusion: trust chains fixed via known proxies in the app config.
- Sticky mismatch: cookie vs source-IP stickiness behave differently under NAT — test both.
Interview checkpoint
Hands-on lab
- Classify each workload as L4- or L7-appropriate
- Sketch the balancer + routing layout
- Justify TLS handling per choice
Pick L4 vs L7 for Three Workloads
15 min · intermediate
Quick quiz
Which balancer class can route by request path?