Back to Blog
Lesson 4 of the System Design: System Design Fundamentals course
ArchitectureJuly 15, 20264 min read

The Role of the Load Balancer: Scalability and Traffic Management

Learn the essentials of load balancing, from L4 vs L7 differences to configuring Nginx. Build a scalable system by mastering traffic distribution.

system designload balancingnginxscalabilityweb architecture
Close-up of a private property loading zone only sign with urban backdrop.

Previously in this course, we covered understanding DNS and domain resolution to map traffic to your infrastructure. Now, we add the load balancer: the critical component that sits between your users and your servers to ensure no single machine becomes a bottleneck.

Why We Need Load Balancing

As your application grows, a single server will eventually run out of CPU, RAM, or network bandwidth. To scale, you add more servers. But how does the internet know which server to talk to?

A load balancer acts as a "traffic cop," sitting at the entry point of your network. It receives incoming client requests and distributes them across a pool of backend servers, ensuring high availability and preventing any single node from being overwhelmed.

L4 vs. L7 Load Balancing

When designing your architecture, you must choose between two primary levels of operation:

FeatureLayer 4 (Transport)Layer 7 (Application)
VisibilityIP and TCP/UDP portsHTTP/HTTPS headers, URLs, cookies
SpeedExtremely fast (low overhead)Slower (requires payload inspection)
LogicSimple forwardingContent-based routing
Use CaseRaw throughput/DB trafficAPI routing, microservices

Layer 4 (L4) load balancers operate at the transport layer. They make routing decisions based on the IP address and port. They don't look at the content of the packets, making them incredibly fast.

Layer 7 (L7) load balancers operate at the application layer. Because they can read HTTP headers or cookies, they can make smarter decisions—like routing /api/v1/users to a user-service cluster and /api/v1/orders to an order-service cluster.

Configuring a Basic Nginx Load Balancer

Nginx is a powerhouse in the industry, often used as a reverse proxy or a load balancer. To get started, we'll configure it to use round-robin distribution, the default algorithm where the load balancer cycles through a list of servers one by one.

Worked Example

Assume you have two application servers running on your private network: 10.0.0.1 and 10.0.0.2. Open your nginx.conf (or your site configuration file) and add the following:

NGINX
http {
    # Define the group of servers
    upstream my_app_servers {
        server 10.0.0.1:8080;
        server 10.0.0.2:8080;
    }

    server {
        listen 80;

        location / {
            # Proxy the requests to the defined group
            proxy_pass http://my_app_servers;
        }
    }
}

In this configuration, Nginx automatically applies round-robin. The first request goes to 10.0.0.1, the second to 10.0.0.2, the third back to 10.0.0.1, and so on. This keeps the load evenly split between both nodes. If you need more complex traffic shaping later, you can explore Nginx Load Balancer: Weighted Round Robin and Health Checks.

Hands-on Exercise

  1. Setup: If you have Docker installed, spin up two simple web containers (like nginx:alpine or python:http.server) on different ports.
  2. Configure: Create a local Nginx configuration file that points to these two ports using the upstream block shown above.
  3. Test: Use curl or your browser to hit the Nginx port repeatedly. Watch your access logs for the two backend servers to verify that the traffic is alternating between them.

Common Pitfalls

  • Session Persistence: If your application stores user sessions in local server memory, round-robin will break your logins because users will bounce between servers that don't share that state. You'll need to implement Session Persistence in Clusters: Scaling Laravel Infrastructure or move session state to an external store like Redis.
  • Ignoring Health Checks: If one of your backend servers crashes, a basic load balancer might keep sending traffic to it, resulting in 502 errors for the user. Always ensure your load balancer is configured to stop sending traffic to "unhealthy" nodes.
  • SSL Termination: Performing SSL decryption on every backend server is expensive. Usually, you terminate SSL at the load balancer so the traffic between the load balancer and the internal servers stays unencrypted (and fast).

FAQ

Q: Does the load balancer become a single point of failure? A: Yes. In production, you typically deploy at least two load balancers in an Active-Passive setup with a floating IP (using tools like Keepalived) to ensure that if one fails, the other takes over.

Q: Can I use Nginx for database traffic? A: Yes, but you must use the stream module instead of http because database traffic (like MySQL or PostgreSQL) is usually TCP, not HTTP.

Recap

We've moved beyond single-server setups by introducing the load balancer as our traffic distribution layer. By understanding the trade-offs between L4 and L7 and implementing a basic round-robin strategy with Nginx, you have taken a major step toward building a system that can handle real-world traffic.

Up next: Introduction to Client-Server Communication — we will trace the journey of a request from the browser to your server and analyze the HTTP headers that make it all possible.

Similar Posts