How Load Balancers Work: Layer 4 vs Layer 7, Health Checks, and Failover

A load balancer is a critical component in most modern software architectures, acting as a traffic controller for your servers. Its primary function is to distribute incoming network traffic across a group of backend servers, often called a server farm or backend pool. This distribution improves application availability and scalability. By spreading the load, it prevents any single server from becoming overwhelmed, and by automatically redirecting traffic away from failed servers, it ensures the application remains online even when individual components fail.

Understanding how load balancers operate is essential for designing, building, and troubleshooting reliable systems. The core mechanism involves intercepting traffic destined for a single public endpoint and forwarding it to one of several backend servers based on a configured algorithm. This process involves decisions made at different network layers, primarily Layer 4 and Layer 7, and relies on constant health monitoring to maintain a reliable pool of servers. Modern implementations range from dedicated hardware appliances to highly integrated cloud services and sophisticated software proxies running within container orchestration platforms.

A Mechanical View of Load Balancing

At its heart, a load balancer solves the problem of a single server's limitations. A lone server has a finite capacity for handling requests and represents a single point of failure. The first step in scaling beyond one machine is to create multiple, identical copies of the application server. The load balancer then sits in front of this server pool, presenting a single IP address and hostname to the outside world. To a client like a web browser, it appears as if it's communicating with a single, powerful server.

When a client sends a request, the following steps occur:

  1. The client's request, destined for the load balancer's public IP address, arrives at the load balancer.
  2. The load balancer inspects the request. The depth of this inspection depends on whether it's an L4 or L7 load balancer.
  3. It consults its list of available, healthy backend servers.
  4. Using a configured algorithm (like Round Robin or Least Connections), it selects one backend server from the pool.
  5. It modifies the network packet and forwards it to the selected server.
  6. The backend server processes the request and sends its response back to the load balancer.
  7. The load balancer receives the response and forwards it back to the original client, rewriting the source address so the response appears to come directly from the load balancer itself. The client remains unaware of the backend server pool's existence.

This entire process of receiving, selecting, forwarding, and returning happens for every request, ensuring traffic is distributed and the system can scale horizontally by simply adding more servers to the pool.

The OSI Model and Load Balancer Types: Layer 4 vs. Layer 7

The Open Systems Interconnection (OSI) model[1] is a conceptual framework that standardizes the functions of a telecommunication or computing system into seven abstract layers. Load balancers are most commonly categorized by the layer at which they operate: Layer 4 (the Transport Layer) or Layer 7 (the Application Layer).

Layer 4 (Transport Layer) Load Balancing

A Layer 4 load balancer operates at the transport level. It makes its routing decisions based on information from the first packet in the TCP or UDP stream (such as the TCP SYN), specifically the source and destination IP addresses and ports. It does not inspect the actual content of the packets. This means it understands protocols like TCP[2] and UDP[3] but is blind to application-level protocols like HTTP.

The mechanism behind an L4 load balancer is typically Network Address Translation (NAT)[4]. When a client request arrives, the load balancer selects a backend server and rewrites the destination IP address on the packet to the IP address of the chosen server before forwarding it. It tracks these connections in an internal state table. When the server responds, the load balancer uses this table to perform the reverse translation, rewriting the source IP address to be its own, so the client receives a response from the expected source.

Because they only inspect a small part of the packet and don't terminate connections, L4 load balancers are extremely fast and have very low overhead. They are essentially high-speed packet forwarders. However, their simplicity is also their main limitation. They cannot make routing decisions based on application-specific data. For instance, they can't route a request for /api/v1 to a different server pool than a request for /images/logo.png.

Cloud examples include the AWS Network Load Balancer[5] and the external passthrough Network Load Balancers in Google Cloud[6].

Layer 7 (Application Layer) Load Balancing

A Layer 7 load balancer operates at the application layer, the highest layer in the OSI model. This means it can understand application-level protocols, most commonly HTTP[7]. Unlike an L4 load balancer that just forwards packets, an L7 load balancer acts as a full proxy. It terminates the client's network connection, inspects the application-level content, and then establishes a new, separate connection to a selected backend server.

This deep packet inspection allows for much more sophisticated routing decisions. An L7 load balancer can read HTTP headers, the request path, query parameters, and cookies. This enables powerful features:

  • Path-Based Routing: Send requests for /video-processing to a pool of powerful GPU servers while sending requests for /user-profiles to a standard server pool.
  • Host-Based Routing: Route traffic for api.example.com and shop.example.com to different backend services, even though both DNS records point to the same load balancer IP.
  • SSL/TLS Termination: The load balancer can handle HTTPS decryption, offloading the computational work from the backend servers. The traffic between the load balancer and the backends can then be unencrypted (within a secure private network) or re-encrypted.
  • Header Manipulation: Add, remove, or modify HTTP headers before forwarding a request to a backend.

This intelligence comes at a cost. Terminating connections and parsing application data requires more CPU and memory than the simple packet forwarding of L4 load balancers, resulting in slightly higher latency. For most modern web applications, the flexibility and features of L7 balancing far outweigh this performance difference. Common examples include software like NGINX[8] and HAProxy[9], and cloud services like the AWS Application Load Balancer[10].

Core Algorithms for Server Selection

Once a load balancer has a pool of healthy servers, it needs a strategy for choosing one. This is determined by its configured algorithm.

  • Round Robin: This is the simplest algorithm. The load balancer cycles through the list of servers sequentially, sending each new request to the next server in line. When it reaches the end of the list, it starts again from the beginning. It's fair but doesn't account for server load or capacity.
  • Weighted Round Robin: A variation where administrators can assign a 'weight' to each server. A server with a weight of 2 will receive twice as much traffic as a server with a weight of 1. This is useful when servers in the pool have different hardware capacities.
  • Least Connections: The load balancer tracks the number of active connections to each backend server and sends the next request to the server with the fewest active connections. This is generally a more effective strategy than Round Robin for traffic with varying request times and long-lived connections, as it helps prevent any one server from becoming overloaded.
  • IP Hash: The load balancer calculates a hash of the client's source IP address and uses this hash to select a server. This ensures that requests from a specific client IP will always be directed to the same backend server. This technique, a form of session persistence or "stickiness," is useful for stateful applications that store session data locally on the server. The downside is that it can lead to uneven load distribution if some IPs generate a large volume of traffic.

Health Checks and Failover: The Key to Reliability

Distributing traffic is only half the job. The true power of a load balancer for high availability comes from its ability to detect and route around failures. This is accomplished through health checks.

A health check is a periodic probe sent by the load balancer to each backend server to verify it's operating correctly. If a server fails its health check, the load balancer temporarily removes it from the routing pool. No new traffic will be sent to it until it becomes healthy again. This process is automatic and is the core of automated failover.

Health checks can be configured with several parameters:

  • Check Type: A simple TCP check just tries to open a connection on a specific port. A more robust check makes an application-level request, for example, GET /health, and expects a specific response, like an HTTP 200 OK[11] status code. Application-level checks are superior because they can detect cases where the server is running but the application is frozen or misbehaving.
  • Interval: How often to perform the check (e.g., every 10 seconds).
  • Timeout: How long to wait for a response before considering the check a failure (e.g., 5 seconds).
  • Thresholds: How many consecutive failures are needed to mark a server as unhealthy (e.g., 2 failures), and how many consecutive successes are required to mark it as healthy again (e.g., 5 successes). These thresholds prevent "flapping," where a server is rapidly added and removed from the pool due to transient network issues.

When a server is marked unhealthy, the load balancer fluidly redirects its share of traffic to the remaining healthy servers in the pool, ensuring service continuity for users.

A Concrete Example: Layer 7 Path-Based Routing with NGINX

Software load balancers like NGINX make it easy to implement sophisticated L7 routing. The following configuration demonstrates how to route traffic to different backend pools based on the request URL.

# Defines two groups of backend servers
upstream api_backend {
 # 'least_conn' sends new requests to the server with the fewest active connections
 least_conn;
 server 10.0.1.10:8080 weight=5; # Can handle more traffic
 server 10.0.1.11:8080;
}

upstream webapp_backend {
 server 10.0.2.20:80;
 server 10.0.2.21:80;
}

server {
 listen 80;
 server_name example.com;

 # For requests starting with /api/, proxy to the api_backend pool.
 # NGINX automatically handles health checks by not sending traffic to
 # servers that are down or fail to respond.
 # The open source version's checks are basic; NGINX Plus offers active health checks.
 location /api/ {
 proxy_pass http://api_backend;
 proxy_set_header Host $host;
 proxy_set_header X-Real-IP $remote_addr;
 }

 # For all other requests, proxy to the webapp_backend pool.
 location / {
 proxy_pass http://webapp_backend;
 proxy_set_header Host $host;
 proxy_set_header X-Real-IP $remote_addr;
 }
}

In this example, defined in an nginx.conf file, we have two server pools (upstream blocks) named api_backend and webapp_backend. The api_backend uses the least_conn algorithm and a weighted server. The server block listens for traffic on port 80. The location blocks implement the L7 logic. Any request whose path begins with /api/ (e.g., http://example.com/api/users) is proxied to the api_backend. All other requests fall through to the second location block and are proxied to the webapp_backend. This configuration effectively creates two distinct services behind a single public endpoint, managed by one NGINX instance. The ngx_http_upstream_module[12] handles the load balancing and basic failure detection.

Tradeoffs and Decision-Making for Leaders

Choosing the right load balancing strategy requires understanding the costs and failure modes.

Hardware vs. Software: Historically, load balancing was dominated by expensive hardware appliances from vendors like F5 Networks[13] and Citrix[14]. These offered high performance but came with high capital costs and vendor lock-in. Today, software load balancers (like NGINX, HAProxy) running on commodity hardware or cloud-native services have become the default for most applications. They offer excellent performance, greater flexibility, and fit better with modern CI/CD and infrastructure-as-code practices.

Layer 4 vs. Layer 7: The decision lens is clear. Choose L4 when you need maximum network throughput and low latency for non-HTTP or simple TCP/UDP traffic where the application logic doesn't require content inspection. Examples include real-time video streaming or high-volume database query distribution. Choose L7 for most web-based applications. The ability to perform path-based routing, terminate SSL, and gain deeper visibility into application traffic is invaluable for building and operating microservices and standard web architectures.

Common Misconceptions and Failure Modes:

  • "The load balancer is a single point of failure." This is a frequent but incorrect assumption. Production-grade load balancers are always deployed in a high-availability (HA) pair. Two or more load balancers are configured to monitor each other. If the primary (active) load balancer fails, the secondary (passive) one automatically takes over its IP address and responsibilities. This failover is often managed by a protocol like the Virtual Router Redundancy Protocol (VRRP)[15] or by the cloud provider's underlying infrastructure.
  • Misconfigured Health Checks: Poorly configured health checks can be a source of instability. Checks that are too aggressive can cause "flapping," removing healthy servers during brief network blips. Checks that are too lenient can leave failing servers in the pool for too long, resulting in user-facing errors. The health check endpoint itself must be a reliable indicator of service health; a simple TCP check is not enough if the application process can freeze while the server's network stack remains responsive.

The State of Load Balancing Today

The principles of load balancing are stable, but their implementation has evolved significantly.

Cloud-Native Dominance: The vast majority of new applications are deployed using managed load balancers from cloud providers like AWS Elastic Load Balancing[16], Google Cloud Load Balancing[17], and the Azure Load Balancer[18]. These services abstract away the complexity of setting up HA pairs, scaling the load balancers themselves, and integrating with other cloud services.

Kubernetes and Ingress: In containerized environments, load balancing is a core concept. A Kubernetes Service[19] provides basic L4 load balancing between pods. As of 2026, the Kubernetes Gateway API is the standard for L7 capabilities, succeeding the older Ingress API[20]. An Ingress object defines HTTP routing rules, which are implemented by an Ingress controller[21], typically a specialized NGINX or Envoy[22] proxy running within the cluster.

Service Mesh: The latest evolution is the service mesh, with popular implementations like Istio[23] and Linkerd[24]. A service mesh injects a lightweight proxy (a "sidecar") next to each service instance. This creates a decentralized data plane where every service-to-service communication is intercepted. This enables extremely granular load balancing, automatic mTLS encryption, advanced traffic policies like circuit breaking and retries, and detailed observability, all without any changes to the application code itself. This is often called "East-West" traffic management (service-to-service), complementing the traditional "North-South" traffic (user-to-service) handled by edge load balancers.

Global Server Load Balancing (GSLB): For applications deployed across multiple geographic regions, GSLB provides another layer of load balancing. It uses the Domain Name System (DNS)[25] to direct users to the closest or best-performing data center, improving performance and providing disaster recovery by routing around regional outages. This is load balancing on a planetary scale.

Sources (25)
  1. Wikipedia: OSI model
  2. RFC 9293: Transmission Control Protocol (TCP)
  3. RFC 768: User Datagram Protocol
  4. RFC 2663: IP Network Address Translator (NAT) Terminology and Considerations
  5. AWS Documentation: What is a Network Load Balancer?
  6. Google Cloud Documentation: External passthrough Network Load Balancer overview
  7. RFC 9112: HTTP/1.1
  8. NGINX Official Website
  9. HAProxy Official Website
  10. AWS Documentation: What is an Application Load Balancer?
  11. MDN Web Docs: HTTP 200 OK
  12. NGINX Documentation: ngx_http_upstream_module
  13. Wikipedia: F5, Inc.
  14. Wikipedia: Citrix Systems
  15. RFC 5798: Virtual Router Redundancy Protocol (VRRP) Version 3
  16. AWS Elastic Load Balancing
  17. Google Cloud Load Balancing
  18. Azure Load Balancer
  19. Kubernetes Documentation: Service
  20. Kubernetes Documentation: Ingress
  21. Kubernetes Documentation: Ingress Controllers
  22. Envoy Proxy Official Website
  23. Istio Official Website
  24. Linkerd Official Website
  25. RFC 1034: Domain Names - Concepts and Facilities