What is the difference between Layer 4 and Layer 7 load balancing?
MediumConceptLoad Balancing
Answer
A Layer 4 load balancer routes using transport-level information (IP addresses and TCP or UDP ports) without inspecting the payload. A Layer 7 load balancer understands the application protocol, usually HTTP, and routes on paths, headers, cookies or hostnames.
- L4: very fast with low overhead, protocol-agnostic, and can pass encrypted traffic through untouched; it cannot make content-based decisions.
- L7: supports path-based routing (for example,
/apito one service and/imagesto another), header-based canary releases, cookie-based sticky sessions, caching and request rewriting. - Cost: L7 must terminate TLS and parse every request, which uses more CPU and adds slight latency.
- A common setup places an L4 balancer at the edge and L7 proxies such as NGINX or Envoy behind it.
More in Load Balancing
| # | Problem | Platform | Difficulty | Open | 1st1st Visit | 2nd2nd Visit | RevRevise |
|---|---|---|---|---|---|---|---|
| 1 | What is a load balancer, and why is it used? | Concept | Easy | Read the answer to What is a load balancer, and why is it used? | |||
| 2 | What are the common load-balancing algorithms, and when would you use each? | Concept | Medium | Read the answer to What are the common load-balancing algorithms, and when would you use each? | |||
| 3 | What is the difference between Layer 4 and Layer 7 load balancing? | Concept | Medium | Read the answer to What is the difference between Layer 4 and Layer 7 load balancing? | |||
| 4 | What is consistent hashing, and why is it used? | Concept | Hard | Read the answer to What is consistent hashing, and why is it used? |