Updated October 2026: Added minimal HAProxy and NGINX configuration examples for Layer 4 and Layer 7, and corrected the claim that TLS offloading is a Layer 7-only feature.

In modern web application environments, load balancing plays a critical role in distributing traffic across multiple servers to ensure optimal performance, reliability, and scalability. Load balancing techniques and tools are deployed at various network layers, each with its own set of advantages and disadvantages. In this article, we’ll explore different types of load balancing at different network layers and provide examples.

I. Load Balancing at Layer 4 (Transport Layer)

Description: Layer 4 load balancing, according to the OSI (Open Systems Interconnection) model, operates based on information available in the packet headers, such as IP addresses and ports.

Tools: Popular tools for Layer 4 load balancing include HAProxy (mode tcp), NGINX (TCP/UDP load balancing with the stream module), and AWS Network Load Balancer.

Pros:

  • High performance and reliability.
  • Easy deployment and configuration.

Cons:

  • Inefficient for applications requiring decision-making based on packet content.

Example: Configuring HAProxy to balance TCP connections arriving on a port (here PostgreSQL on 5432) across several servers without looking inside the traffic:

frontend pg_in
    mode tcp
    bind :5432
    default_backend pg_servers

backend pg_servers
    mode tcp
    balance leastconn
    server db1 10.0.0.11:5432 check
    server db2 10.0.0.12:5432 check

The same idea with NGINX uses the stream block, which sits next to http, not inside it. The stream module is not built by default when compiling from source (--with-stream), but the official nginx.org packages include it:

stream {
    upstream pg_servers {
        least_conn;
        server 10.0.0.11:5432;
        server 10.0.0.12:5432;
    }

    server {
        listen 5432;
        proxy_pass pg_servers;
    }
}

II. Load Balancing at Layer 7 (Application Layer)

Description: Layer 7 load balancing makes decisions based on application data in packets, such as URLs or HTTP headers.

Tools: Common tools for Layer 7 load balancing include NGINX (using HTTP Load Balancing), HAProxy (in HTTP mode), and AWS Application Load Balancer. Envoy and Traefik are also widely used as Layer 7 proxies, especially in Kubernetes and service-mesh setups.

Pros:

  • Intelligent load balancing based on application content.
  • Support for features like URL-based routing, header/cookie-based routing and HTTP-aware health checks.

TLS (SSL) offloading is often listed as a Layer 7 feature, but it is not exclusive to it: a Layer 4 balancer can terminate TLS too (for example an AWS Network Load Balancer with a TLS listener, or HAProxy mode tcp with a certificate on bind). What Layer 7 adds is the ability to route on the decrypted HTTP request.

Cons:

  • Requires more resources compared to Layer 4 load balancing.
  • Complex configuration and management.

Example: Using NGINX to perform load balancing based on the URL of HTTP requests, sending /api/ to one pool and everything else to another:

http {
    upstream api_servers {
        server 10.0.1.11:8080;
        server 10.0.1.12:8080;
    }

    upstream web_servers {
        server 10.0.2.11:3000;
        server 10.0.2.12:3000;
    }

    server {
        listen 80;
        server_name example.com;

        location /api/ {
            proxy_pass http://api_servers;
        }

        location / {
            proxy_pass http://web_servers;
        }
    }
}

III. Global Load Balancing

Description: Global load balancing distributes traffic to data centers worldwide to optimize performance and ensure service availability.

Tools: Services like Cloudflare Load Balancing, AWS Global Accelerator, and Google Cloud Load Balancing.

Pros:

  • Enhanced scalability and availability.
  • Provides routing based on user location and path.

Cons:

  • High cost and complex management.
  • Depends on the global network infrastructure of the provider.

Example: Utilizing Cloudflare Load Balancing to distribute traffic to server regions worldwide.

IV. Conclusion

Load balancing at different network layers offers unique benefits and drawbacks, and the choice of which type to use will depend on the specific requirements of the application and deployment environment. By understanding different load balancing techniques and corresponding tools, you can build a robust and scalable network infrastructure for your application.