Skip to content
tezvyn:

Layer 4 vs Layer 7 load balancers

Source: interviewEasyHow cards are made

Summary

OSI model and load balancer routing knowledge.

Key points

L4 routes on IP and TCP/UDP ports fast and protocol-blind; L7 inspects HTTP for host, path, and headers.

Watch out for

claiming L7 is always better.

What's really being asked

The interviewer wants to see that you understand the OSI model and how a load balancer's operating layer changes what routing decisions it can make and at what cost.

The full answer

A Layer 4 load balancer operates at the transport layer. It forwards packets based only on source and destination IP addresses and TCP or UDP ports. It does not read the payload, so it works with any protocol, adds minimal latency, and handles very high throughput. A Layer 7 load balancer operates at the application layer. It terminates the connection, reads the HTTP request, and can route based on hostname, URL path, headers, cookies, or query strings. This enables content-based routing, TLS termination, sticky sessions, and request rewriting, at the cost of more CPU and slightly higher latency. A strong use case for L7 is routing /api to one service and /static to another, or canary releases by header. A strong use case for L4 is balancing a database protocol, a gRPC stream over raw TCP, or any workload where throughput matters more than inspection.

The mistakes people make

Claiming Layer 7 is always the better choice ignores its overhead and its inability to handle non-HTTP traffic cleanly. Saying L4 cannot do TLS is wrong; it passes encrypted bytes through untouched. Confusing the layers with hardware versus software is also a mistake.

What usually comes next

How does TLS termination differ from TLS passthrough? How does an L7 balancer preserve the client IP? What happens to long-lived connections during a deploy?

A concrete example

An AWS Application Load Balancer is L7 and routes /checkout to a payments service by path. A Network Load Balancer is L4 and fronts a high-volume MQTT broker, preserving source IP and sustaining millions of connections with predictable latency.

Interview question

A service exposes a custom binary protocol over TCP that needs maximum throughput and source IP preservation. Which load balancer fits best and why?

  • a.Either works identically since both forward TCP packets the same way
  • b.Layer 7, because it can inspect the binary payload for routing
  • c.Layer 7, because only it can preserve the client source IP
  • d.Layer 4, because it routes on IP and port without reading payload, adding minimal overheadCorrect
Why?

L4 routes purely on IP and port, so it handles arbitrary protocols at high throughput with low overhead. L7 must terminate and parse HTTP, which it cannot do for a custom binary protocol.

Just read this? Test yourself on what you have been reading.

Read the original → docs.cloud.google.com

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.

See open roles