What is the difference between a Layer 4 and a Layer 7 load balancer?
BeginnerScenarioLoad Balancing, Proxies & CDN
Answer
Situation read → The layer decides what the balancer can see and therefore what it can route on.
What I'd do →
- L4 (NLB, HAProxy in
mode tcp) balances on IP and port without reading the payload, so it is fast and protocol agnostic - L7 (ALB, Nginx, Envoy) understands HTTP and can route on host, path, header or cookie
- L7 can terminate TLS, rewrite headers and do WAF or redirects
- Choose L4 for raw TCP/UDP, extreme throughput or static IPs, and L7 for web traffic
Why → The more of the packet the balancer parses, the smarter and the slower the decision gets.
Interview tip → Mention that NLB preserves the client source IP, while ALB does not and passes it in X-Forwarded-For.
More on Load Balancing, Proxies & CDN
When do you choose an ALB and when an NLB in AWS?BeginnerForward proxy versus reverse proxy: what is the difference?BeginnerHow do health checks work on a load balancer and what makes a good one?BeginnerWhat are sticky sessions and why would you avoid them?BeginnerWhat does a CDN do and when does it help?BeginnerYour app sees the load balancer's IP instead of the real client IP. How do you get the real one?Beginner