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
Extra Perks

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.

How Layer 4 Works

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).

When to Use Layer 4
  • High-performance, low-latency routing
  • Less CPU overhead
  • Works well when traffic doesn’t need content-aware routing

Examples: Layer 4 vs Layer 7

FeatureLayer 4 LBLayer 7 LB
InspectionIP & port onlyHeaders, cookies, body content
Use CaseFast packet routing, low CPU overheadContent-based routing, smarter decisions
ExampleAll traffic balanced equally to backend serversVideo requests → video servers, billing requests → secure servers
PerformanceVery fast, minimal processingHigher processing, but flexible

Benefits of Using Load Balancers

Horizontal Scaling

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
Summary

Load balancers improve availability, performance, and reliability.
Choosing between Layer 4 and Layer 7 depends on whether you need speed or content-aware routing.