0
0
HLDsystem_design~7 mins

Layer 4 vs Layer 7 load balancing in HLD - Architecture Trade-offs

Choose your learning style9 modes available
Problem Statement
When a single server handles all incoming traffic, it becomes a bottleneck causing slow responses and potential downtime if it fails. Without proper distribution, some servers may be overloaded while others remain idle, leading to inefficient resource use and poor user experience.
Solution
Load balancers distribute incoming network traffic across multiple servers to prevent overload and improve availability. Layer 4 load balancing directs traffic based on network information like IP addresses and ports, while Layer 7 load balancing makes decisions based on application data like HTTP headers and content, enabling smarter routing.
Architecture
Clients
Clients
Layer 4 Load
Layer 4 Load
Srv 1
Clients
Clients
Layer 7 Load
Layer 7 Load
Srv 1

The diagram shows two setups: one with Layer 4 load balancing directing traffic based on network info, and one with Layer 7 load balancing directing traffic based on application data.

Trade-offs
✓ Pros
Layer 4 load balancing is faster and uses fewer resources because it only inspects network headers.
Layer 7 load balancing enables advanced routing like directing requests based on URL paths or cookies.
Layer 7 can perform health checks and modify requests for better control.
✗ Cons
Layer 4 load balancing cannot make decisions based on application content, limiting routing flexibility.
Layer 7 load balancing adds processing overhead, increasing latency and resource use.
Layer 7 requires deeper packet inspection, which can complicate encryption handling.
Use Layer 4 load balancing when high throughput and low latency are critical and routing decisions can be made using IP and port. Use Layer 7 load balancing when application-aware routing, content-based decisions, or advanced features like SSL termination are needed.
Avoid Layer 7 load balancing for extremely high-volume, low-latency systems where added processing delays are unacceptable. Avoid Layer 4 load balancing when routing needs depend on application data or user context.
Real World Examples
Netflix
Uses Layer 7 load balancing to route streaming requests based on user location and device type for personalized content delivery.
Amazon
Employs Layer 4 load balancing for fast distribution of network traffic to backend servers in their global data centers.
Uber
Uses Layer 7 load balancing to route API requests based on request paths and headers to different microservices.
Alternatives
DNS Load Balancing
Distributes traffic by resolving domain names to different IP addresses rather than inspecting packets.
Use when: Use when simple geographic or round-robin distribution is sufficient and low latency is less critical.
Anycast Load Balancing
Routes traffic to the nearest server based on network topology using the same IP address advertised from multiple locations.
Use when: Use for global services needing fast failover and geographic proximity routing.
Summary
Load balancing prevents server overload by distributing traffic across multiple servers.
Layer 4 load balancing routes based on network info, offering speed but limited routing options.
Layer 7 load balancing routes based on application data, enabling advanced routing at the cost of added processing.