L4 vs L7 Load Balancers
"Just add a load balancer" sounds simple until you realize there are different types operating at different network layers. Understanding L4 vs L7 is crucial for making the right choice.
The OSI Model Quick Recap
Layer 7: Application โ HTTP, HTTPS, WebSocket (L7 LB works here)
Layer 6: Presentation
Layer 5: Session
Layer 4: Transport โ TCP, UDP (L4 LB works here)
Layer 3: Network โ IP
Layer 2: Data Link
Layer 1: Physical
L4 LOAD BALANCER
Sees: IP addresses + Ports
"I see a TCP connection from 192.168.1.100:54321 to port 443. I'll forward it to Server B."
L7 LOAD BALANCER
Sees: Full HTTP request
"I see GET /api/users with auth token X. I'll forward it to the API server cluster."
L4 Load Balancer: The Fast Forwarder
L4 load balancers work at the TCP/UDP level. They don't understand HTTP - they just forward packets.
What L4 Can See
TCP Packet:
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Source IP: 192.168.1.100 โ โ Can see
โ Source Port: 54321 โ โ Can see
โ Dest IP: 10.0.0.1 (LB) โ โ Can see
โ Dest Port: 443 โ โ Can see
โ โ
โ [Encrypted HTTP payload - can't read] โ โ Can't see
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
What L4 Can Do
Routing decisions based on:
- Source IP address
- Destination port
- Protocol (TCP/UDP)
That's it. No URL, no headers, no cookies.
L4 Architecture
Client L4 Load Balancer Backend
โ โ โ
โ โโTCP SYNโโโโโโโโโโโโโถ โ โ
โ โ โโTCP SYNโโโโโโโโโโโโโโโโถ โ
โ โ โโTCP SYN-ACKโโโโโโโโโโโโ โ
โ โโTCP SYN-ACKโโโโโโโโโ โ โ
โ โโTCP ACKโโโโโโโโโโโโโถ โ โโTCP ACKโโโโโโโโโโโโโโโโถ โ
โ โ โ
โ โโโโโโโโโโโโโโ Connection established โโโโโโโโโโโ โ
โ โโData (encrypted)โโโโถ โ โโData (encrypted)โโโโโโถ โ
โ โโData (encrypted)โโโโ โ โโData (encrypted)โโโโโโโ โ
Key insight: The L4 LB just passes TCP packets through. If it's HTTPS, the LB never sees the decrypted content.
L7 Load Balancer: The Smart Router
L7 load balancers terminate the connection and inspect HTTP content.
What L7 Can See
HTTP Request (after SSL termination):
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ GET /api/users HTTP/1.1 โ โ URL path
โ Host: api.example.com โ โ Host header
โ Authorization: Bearer eyJhbG... โ โ Auth token
โ Cookie: session=abc123 โ โ Cookies
โ X-Request-ID: uuid-here โ โ Custom headers
โ โ
โ { "name": "John" } โ โ Body (if needed)
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
What L7 Can Do
/api/* โ API servers
/static/* โ CDN
Add X-Forwarded-For
Add trace IDs
Decrypt at edge
HTTP to backends
Route by cookie
A/B testing
L7 Architecture
Client L7 Load Balancer Backend
โ โ โ
โ โโSSL Handshakeโโโโโโโถ โ โ
โ โโโโโโโโโโโโโโโโโโโโ โ โ
โ โ โ
โ โโGET /api/usersโโโโโโถ โ (reads HTTP request) โ
โ โ โ
โ โ โโGET /api/usersโโโโโโโโโถ โ
โ โ โโ200 OK + JSONโโโโโโโโโโ โ
โ โ โ
โ โโ200 OK + JSONโโโโโโโ โ โ
Key insight: L7 terminates SSL and makes two separate connections (clientโLB and LBโbackend).
Side-by-Side Comparison
| Feature | L4 (Transport) | L7 (Application) |
|---|---|---|
| Speed | Very fast (just forwards packets) | Slower (parses HTTP) |
| SSL Termination | Passes through (end-to-end) | Terminates at LB |
| URL Routing | No | Yes |
| Header Inspection | No | Yes |
| Cookie Affinity | No (use IP hash) | Yes |
| Protocol | Any TCP/UDP | HTTP/HTTPS/WebSocket |
| Connections | One (pass-through) | Two (client + backend) |
When to Use L4
- You need maximum throughput (millions of connections)
- You're load balancing non-HTTP protocols (database, gaming, IoT)
- You want end-to-end encryption (SSL passthrough)
- You don't need content-based routing
- Database connection pooling
- Gaming servers
- IoT message brokers
- Internal microservice communication (when all go to same pool)
When to Use L7
- You need URL-based routing (/api/* vs /static/*)
- You need SSL termination at the edge
- You need cookie-based session affinity
- You want to add/modify headers (X-Forwarded-For)
- You're doing A/B testing or canary deployments
- Web applications with multiple backend services
- API gateways
- Microservices with path-based routing
- CDN edge routing
Real-World Example: E-Commerce Architecture
Internet
โ
โผ
โโโโโโโโโโโโโโโโโโโ
โ L7 (Cloudflare) โ โ SSL termination, WAF, DDoS protection
โโโโโโโโโโฌโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโ
โ L7 (AWS ALB) โ โ Path-based routing
โโโโโโโโโโฌโโโโโโโโโ
โโโโโโดโโโโโฌโโโโโโโโโโโโโโโโ
โ โ โ
โผ โผ โผ
โโโโโโโโโ โโโโโโโโโ โโโโโโโโโโโโโ
โ /api/*โ โ/staticโ โ /admin/* โ
โ API โ โ CDN โ โ Admin App โ
โServersโ โ โ โ โ
โโโโโฌโโโโ โโโโโโโโโ โโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโ
โ L4 (AWS NLB) โ โ Internal service mesh, max performance
โโโโโโโโโโฌโโโโโโโโโ
โโโโโโดโโโโโ
โผ โผ
โโโโโโโโโ โโโโโโโโโ
โ DB โ โ Cache โ
โClusterโ โClusterโ
โโโโโโโโโ โโโโโโโโโ
AWS Load Balancer Options
Path routing, WAF
Millions of connections
Firewalls, IDS/IPS
Key Takeaways
- L4 = Fast and simple - Forwards TCP/UDP packets without inspection
- L7 = Smart and flexible - Understands HTTP, enables content-based routing
- Use L4 for performance - Databases, gaming, internal services
- Use L7 for web apps - Path routing, SSL termination, microservices
Next up: Assessment Quiz - Test your understanding of load balancing strategies.