Load Balancers
Understanding load balancers in system design and when to use them.
Load Balancers
Load balancers are the traffic directors of your system. They take incoming requests from users and decide which server should handle them.
They help your system by:
- Preventing traffic from reaching unhealthy servers
- Avoiding overload on any single server
- Eliminating single points of failure
Modern load balancers can also handle:
- SSL termination: Decrypt requests and encrypt responses so backend servers don’t have to
- Session persistence: Keep a user’s requests going to the same server if the app doesn’t track sessions
- Simplified certificate management: No need to install certificates on each server
Load balancers can be hardware-based (expensive) or software-based, like HAProxy or Nginx.
Types of Load Balancers
Load balancers can operate at different layers of the network stack. This determines how they inspect and distribute traffic.
Layer 4 load balancers work at the transport layer. They look at IP addresses and ports, but not the content of the requests.
They forward packets to servers using NAT (Network Address Translation).
- High-performance, low-latency routing
- Less CPU overhead
- Works well when traffic doesn’t need content-aware routing
Examples: Layer 4 vs Layer 7
| Feature | Layer 4 LB | Layer 7 LB |
|---|---|---|
| Inspection | IP & port only | Headers, cookies, body content |
| Use Case | Fast packet routing, low CPU overhead | Content-based routing, smarter decisions |
| Example | All traffic balanced equally to backend servers | Video requests → video servers, billing requests → secure servers |
| Performance | Very fast, minimal processing | Higher processing, but flexible |
Benefits of Using Load Balancers
Load balancers make it easy to scale horizontally by adding more commodity servers. This is usually:
- Cheaper than vertical scaling (upgrading a single server)
- More resilient, because failure of one server doesn’t take down the system
- Easier to manage with widely available hardware and talent
Load balancers improve availability, performance, and reliability.
Choosing between Layer 4 and Layer 7 depends on whether you need speed or content-aware routing.