AI Hints
Home›System Design Tutorial›Load Balancing
System Design · Lesson 27 of 120

L4 vs L7 Balancing

Packets or requests.

⏱ 2 min read▶ Video at 30:40🎓 Free
Watch this lesson in the System Design courseWatch on YouTube ↗Full System Design course

Load balancers work at one of two levels. Layer four looks only at network addresses. Layer seven reads the actual HTTP request. The names come from the layers of the networking model.

L4 vs L7 Balancing
From the video: L4 vs L7 Balancing
  • Two levels
  • Layer 4: addresses
  • Layer 7: the request itself

Layer 4 vs layer 7

A layer four balancer sees only IP addresses and ports. It can't read the request, so it's extremely fast, and works for any protocol. A layer seven balancer reads the HTTP path, headers, and cookies. It's slower, but much smarter. It can send slash images to one group of servers, and slash api to another.

Layer 4

  • Sees IPs and portsVery fastAny protocol

Layer 7

  • Reads HTTP paths, headersSmart routingCan end HTTPS

Where they show up

In practice, you often see both. A layer four balancer at the edge spreads huge traffic across several layer seven balancers. Those route each request by path, to the right service. Cloud providers offer both kinds, and tools like Nginx and HA Proxy can do layer seven routing.

keyvalue
EdgeL4 for raw speed
InsideL7 for smart routing
/apito API servers
/imagesto image servers

Summary

Layer four balancers are fast and simple, layer seven balancers read requests and route them smartly. Next, how a balancer knows which servers are healthy.

Key takeaways
  • L4: addresses only
  • L7: reads requests
  • Often used together
Next up: Health Checks — removing sick servers