Load Balancing

2 min read

Load balancing is the practice of distributing incoming network traffic or workloads across multiple servers, instances, or resources to ensure no single server becomes overwhelmed. By spreading requests evenly, load balancers improve application availability, reliability, and responsiveness. They are a critical component of any scalable web architecture, enabling applications to handle traffic spikes, server failures, and growing user bases without degradation in performance.

Load balancers operate at different layers of the network stack. Layer 4 (transport layer) load balancers route traffic based on IP address and TCP/UDP port information, offering fast, efficient distribution without inspecting the actual content. Layer 7 (application layer) load balancers make routing decisions based on the content of HTTP requests, including URL paths, headers, cookies, and request bodies, enabling more intelligent traffic management like routing API calls to one server pool and static content to another.

Several algorithms determine how traffic is distributed across servers:

  • Round Robin: Requests are distributed sequentially to each server in turn, simple and effective for equally capable servers.
  • Least Connections: New requests go to the server with the fewest active connections, ideal for varying request durations.
  • Weighted distribution: Servers receive traffic proportional to their capacity (e.g., a more powerful server handles more requests).
  • IP Hash: The client's IP address determines which server receives the request, ensuring session persistence.
  • Health-based: Continuously monitor server health and route traffic only to healthy instances.

Popular load balancing solutions include NGINX, HAProxy, AWS Elastic Load Balancer (ELB), and Google Cloud Load Balancing. A well-configured load balancer keeps applications fast and available under any traffic conditions, whether it's a steady stream of users or an unexpected spike.