| Users / Traffic | Layer 4 Load Balancer | Layer 7 Load Balancer |
|---|---|---|
| 100 users | Simple TCP/UDP routing; single server often enough | Basic HTTP routing; minimal overhead; single server sufficient |
| 10,000 users | Multiple servers; round-robin or least connections; still low latency | Content-based routing starts; SSL termination; CPU usage rises |
| 1 million users | Needs multiple load balancers; network bandwidth critical; health checks | High CPU load for parsing HTTP; caching and compression needed; SSL offload essential |
| 100 million users | Distributed load balancers; global DNS load balancing; network partitioning | Multiple clusters; microservices routing; advanced security (WAF); CDN integration |
Layer 4 vs Layer 7 load balancing in HLD - Scaling Approaches Compared
For Layer 4 load balancing, the first bottleneck is network bandwidth and connection tracking on the load balancer, as it handles raw TCP/UDP packets without inspecting content.
For Layer 7 load balancing, the first bottleneck is CPU and memory on the load balancer because it must parse and inspect HTTP headers and bodies, perform SSL termination, and apply routing rules.
- Layer 4: Add more load balancer instances horizontally behind a DNS or anycast; use connection tracking optimization; employ network-level health checks.
- Layer 7: Use horizontal scaling with multiple load balancers; offload SSL termination to dedicated hardware or proxies; implement caching and compression; use CDN for static content.
- For both, use global load balancing and geo-DNS to distribute traffic regionally.
Assuming 1 million users generating 10 requests per second each = 10 million requests per second (RPS).
- Layer 4 load balancer can handle ~5,000 concurrent connections per server; need ~2,000 servers to handle 10M RPS if each connection sends 1 request per second.
- Layer 7 load balancer handles fewer RPS per server (~1,000-2,000 RPS) due to CPU overhead; need ~5,000-10,000 servers or aggressive caching.
- Bandwidth: 10M RPS * average 1 KB request/response = ~10 GB/s (~80 Gbps), requiring high network capacity and distributed load balancers.
Start by explaining the difference between Layer 4 and Layer 7 load balancing simply: Layer 4 works at the transport level (IP, TCP), Layer 7 at the application level (HTTP).
Discuss scaling challenges for each, focusing on network vs CPU bottlenecks.
Then propose solutions like horizontal scaling, SSL offloading, caching, and CDN integration.
Use real numbers to show understanding of capacity and bottlenecks.
Your Layer 7 load balancer handles 1,000 requests per second (RPS). Traffic grows 10x to 10,000 RPS. What do you do first and why?
Answer: Add more Layer 7 load balancer instances horizontally to distribute CPU load, and implement SSL offloading or caching to reduce processing per request.
Practice
Solution
Step 1: Understand OSI layers relevant to load balancing
Layer 4 corresponds to the transport layer, which handles IP addresses and ports.Step 2: Match Layer 4 load balancing function
Layer 4 load balancing uses IP and port information to distribute traffic without inspecting content.Final Answer:
Transport layer using IP and port -> Option CQuick Check:
Layer 4 = Transport layer [OK]
- Confusing Layer 4 with Layer 7
- Thinking Layer 4 inspects request content
- Mixing network and transport layers
Solution
Step 1: Identify Layer 7 load balancing features
Layer 7 load balancing works at the application layer and inspects request content like HTTP headers.Step 2: Evaluate options
Operates at the application layer inspecting request content correctly states Layer 7 inspects request content; others are incorrect or misleading.Final Answer:
Operates at the application layer inspecting request content -> Option DQuick Check:
Layer 7 = Application layer content inspection [OK]
- Assuming Layer 7 ignores HTTP headers
- Believing Layer 7 is faster than Layer 4
- Confusing IP-based routing with content-based routing
Solution
Step 1: Understand URL path inspection
URL paths are part of the application data in HTTP requests, which Layer 7 can inspect.Step 2: Match to OSI layer
Layer 7 is the application layer that can route based on URL paths, unlike lower layers.Final Answer:
Layer 7 -> Option BQuick Check:
URL path routing = Layer 7 [OK]
- Choosing Layer 4 for URL-based routing
- Confusing network layers with application layer
- Ignoring request content in routing decisions
Solution
Step 1: Identify load balancer type and performance impact
Layer 7 load balancing inspects request content like HTTP headers, which adds processing overhead.Step 2: Analyze options for cause of slowness
Layer 7 load balancing adds processing overhead due to content inspection correctly explains that Layer 7's content inspection slows processing; others are incorrect.Final Answer:
Layer 7 load balancing adds processing overhead due to content inspection -> Option AQuick Check:
Content inspection causes slower Layer 7 [OK]
- Blaming Layer 4 for inspecting headers
- Ignoring processing overhead of Layer 7
- Assuming Layer 7 cannot route by IP
Solution
Step 1: Identify requirement to inspect authentication tokens
Authentication tokens are in HTTP headers, requiring application-level inspection.Step 2: Choose load balancing approach balancing inspection and throughput
Layer 7 load balancing inspects headers and supports smart routing, suitable despite overhead.Step 3: Evaluate other options
Layer 4 cannot inspect tokens; DNS round-robin lacks smart routing; Layer 2/3 unrelated to content inspection.Final Answer:
Layer 7 load balancing to inspect tokens despite some overhead -> Option AQuick Check:
Token inspection needs Layer 7 [OK]
- Choosing Layer 4 ignoring token inspection need
- Using DNS round-robin for smart routing
- Confusing network layers with application logic
Ready to prove it?
Pass the test and this pattern moves unknown
