Course contentsShow
← Software Engineering
Lesson 1390 of 1,93341. Scalability and Distributed SystemsFree lesson

Layer 4 vs Layer 7 Load Balancing

Differences between transport-layer and application-layer load balancing and when to use each approach.

Layer 4 vs Layer 7 Load Balancing

What you'll learn: You'll understand the difference between transport-layer and application-layer load balancing and when each approach makes sense.

The Core Difference

Load balancers can operate at different layers of the network stack:

Layer 4 (Transport Layer) load balancers make routing decisions based on network information like IP addresses and TCP/UDP ports. They don't inspect the actual content of your requests—they just look at where packets are coming from and going to.

Layer 7 (Application Layer) load balancers peek inside the HTTP request itself. They can read URLs, headers, cookies, and message content to make intelligent routing decisions.

The Restaurant Analogy

Think of Layer 4 like a parking attendant who directs cars based solely on license plate numbers—fast and simple. Layer 7 is like a maître d' who reads your reservation details, dietary restrictions, and party size before seating you at the perfect table—smarter but slower.

When to Use Each

Use Layer 4 when:

  • You need maximum speed and throughput
  • All backend servers handle requests identically
  • You're load balancing non-HTTP protocols (databases, game servers)
  • Lower latency is critical

Use Layer 7 when:

  • You need content-based routing (e.g., /api/* goes to service A, /images/* to service B)
  • You want SSL termination at the load balancer
  • You need to implement authentication, rate limiting, or request transformation
  • Different services handle different URL paths

Layer 7 provides more flexibility but adds latency since it must parse application data. Layer 4 is faster but "dumber"—it cannot make decisions based on request content.

Key Takeaway: Layer 4 load balancing is faster and simpler, operating on network addresses; Layer 7 is smarter and more flexible, making decisions based on application content—choose based on whether you need speed or intelligent routing.