Appearance
Load Balancer (L4 vs L7)
Distributes incoming traffic across multiple servers so no single server gets overwhelmed.
Clients
│
▼
Load Balancer
├── Server 1
├── Server 2
└── Server 3L4 — Transport Layer Load Balancer
Operates at TCP/UDP level. Doesn't look inside the packet, just routes based on:
- Source/Destination IP
- Source/Destination Port
- Protocol (TCP/UDP)
Client --- LB sees: IP + Port only --- forwards to server
(never opens the packet)- Very fast — no deep inspection.
- Can't make smart routing decisions (doesn't know if it's HTTP, video, API).
- One TCP connection: Client → LB → Server (NAT-based or TCP passthrough).
- Use case: Raw speed, non-HTTP traffic, database connections, game servers.
L7 — Application Layer Load Balancer
Operates at HTTP/HTTPS level. Actually reads the request content:
- URL path
- Headers
- Cookies
- HTTP method
Client --- LB reads full HTTP request --- smart routing decision
"oh this is /api → send to API servers"
"oh this is /video → send to media servers"- Smarter routing decisions.
- Can terminate TLS (decrypt, inspect, re-encrypt).
- Can modify requests/responses.
- Slightly more overhead than L4.
- Use case: Web apps, microservices, API gateways.
L4 vs L7 Comparison
| L4 | L7 | |
|---|---|---|
| Layer | Transport (TCP/UDP) | Application (HTTP) |
| Sees | IP + Port | Full request content |
| Speed | Faster | Slightly slower |
| Routing based on | IP/Port | URL, headers, cookies |
| TLS termination | No | Yes |
| Use case | DB, games, raw TCP | Web, APIs, microservices |
Load Balancing Algorithms
| Algorithm | How |
|---|---|
| Round Robin | Requests go 1→2→3→1→2→3 in order |
| Weighted Round Robin | Powerful servers get more requests |
| Least Connections | Send to server with fewest active connections |
| IP Hash | Same client IP always goes to same server (sticky) |
| Random | Pick a server randomly |
Key Concepts
Health Checks
- LB periodically pings servers.
- If server fails → removed from pool automatically.
Session Persistence (Sticky Sessions)
- Same user always hits same server.
- Needed when session state is stored locally on server.
- L7 can do this via cookie, L4 via IP hash.
TLS Termination
- L7 LB decrypts HTTPS traffic.
- Backend servers receive plain HTTP.
- Offloads crypto work from app servers.
Client --HTTPS--> L7 LB (decrypts) --HTTP--> Backend serversExamples
- L4 — AWS NLB, HAProxy (TCP mode)
- L7 — AWS ALB, Nginx, Cloudflare, HAProxy (HTTP mode)