The Wild

Load Balance Round Robin: Step-by-Step Setup Guide

· 5 min read · Birds

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Round-robin load balancing distributes incoming requests across multiple servers in a rotating order, so each server gets an equal share of traffic. It is one of the simplest and most effective ways to improve availability and responsiveness for web services, APIs, or databases. Before you start, you need at least two backend servers (or instances) running the same application, and a load balancer (software like HAProxy, Nginx, or a cloud LB such as AWS ALB) that you can configure. This guide walks through the core concept and a practical Nginx example, but the steps translate to other tools.

What You'll Need

  • Two or more backend servers (physical, virtual, or containers) with the same service running and listening on a port (e.g., 8080).
  • A load balancer machine or service with network access to all backends.
  • Admin/root access to configure the load balancer.
  • Basic familiarity with the command line and editing config files.
  • Optional: a domain name pointing to the load balancer's public IP.

Step-by-Step

  1. Verify your backend servers are healthy. From the load balancer, test each backend by sending a request (e.g., curl http://backend1:8080/health). Ensure they respond correctly and have identical content or behavior.
  2. Install the load balancer software. For Nginx on Ubuntu/Debian: sudo apt update && sudo apt install nginx. For HAProxy: sudo apt install haproxy. For cloud LBs, skip this step and use the console/CLI.
  3. Open the main configuration file. With Nginx, edit /etc/nginx/nginx.conf or create a new file in /etc/nginx/conf.d/. With HAProxy, edit /etc/haproxy/haproxy.cfg.
  4. Define an upstream (backend) group. In Nginx, inside the http block, add:
    upstream backend_servers {
        server 192.168.1.10:8080;
        server 192.168.1.11:8080;
    }
    In HAProxy, under backend, add:
    server backend1 192.168.1.10:8080 check
    server backend2 192.168.1.11:8080 check
    The check option enables health checks.
  5. Configure the load balancer to use round-robin. In Nginx, round-robin is the default, so no extra directive is needed. In HAProxy, add balance roundrobin inside the backend section. For cloud LBs, select “Round robin” as the algorithm when creating the target group.
  6. Set up the frontend listener. In Nginx, add a server block that listens on port 80 (or 443 for HTTPS) and proxies to the upstream:
    server {
        listen 80;
        location / {
            proxy_pass http://backend_servers;
        }
    }
    In HAProxy, add a frontend section:
    frontend http_in
        bind *:80
        default_backend backend_servers
  7. Test the configuration syntax. For Nginx: sudo nginx -t. For HAProxy: sudo haproxy -c -f /etc/haproxy/haproxy.cfg. Fix any errors before continuing.
  8. Reload the load balancer. Use sudo systemctl reload nginx or sudo systemctl reload haproxy. For cloud LBs, deploy the new configuration.
  9. Verify round-robin distribution. Send several requests to the load balancer's IP (e.g., for i in {1..6}; do curl http://your-lb-ip/; done). Check the access logs on each backend to confirm requests alternate between them.
  10. Monitor and adjust. Keep an eye on backend health and performance. If one server is slower, consider weighted round-robin (e.g., server 192.168.1.10 weight=3 in Nginx) to give it fewer requests.

Common Mistakes

  • Not enabling health checks. Without health checks, the load balancer will keep sending traffic to a dead server, causing errors for users.
  • Forgetting to test backends before configuring. If a backend is misconfigured, round-robin will simply spread failures.
  • Using different session state. If your app stores user sessions locally on each server, round-robin will break sessions as users hop between servers. You need sticky sessions or a shared session store (like Redis).
  • Ignoring firewall rules. The load balancer must be able to reach the backends on the service port; otherwise connections time out.
  • Misunderstanding round-robin vs. least-connections. Round-robin ignores current load; if one server is slower, it still gets the same number of requests. Use least-connections for uneven workloads.

Tips

  • Always configure health checks (e.g., Nginx's max_fails and fail_timeout, or HAProxy's check). This automatically removes unhealthy servers.
  • Use sticky sessions only if necessary. If your app can handle statelessness, round-robin works best without stickiness.
  • Consider weighted round-robin when servers have different capacities (e.g., CPU/RAM).
  • Log the backend server IP in your application or load balancer logs to verify distribution and debug issues.
  • Start with a small test using two backends and a simple static page before moving to production traffic.

Troubleshooting

  • All requests go to one server. Check that the upstream group lists all servers and that health checks pass. Also verify that you reloaded the configuration after changes.
  • 502 Bad Gateway errors. The load balancer cannot reach a backend. Test each backend directly with curl. Check firewall rules and that the backend service is listening on the correct IP/port.
  • Round-robin seems uneven. If you have few requests, the pattern may not be perfectly alternating due to keep-alive connections. Use more requests or check logs over time.
  • Session data lost between requests. Your app is stateful. Implement sticky sessions (e.g., Nginx's ip_hash) or move session storage to a shared cache.
  • Configuration syntax errors after editing. Run the syntax test command (as in Step 7) and look for line numbers in the error output.

Frequently Asked Questions

Q: Does round-robin load balancing work for HTTPS?
A: Yes, but you need to terminate SSL on the load balancer (e.g., using a certificate) and forward plain HTTP to backends, or use SSL pass-through. Most setups terminate SSL at the LB.

Q: What is the difference between round-robin and weighted round-robin?
A: Round-robin gives each server an equal number of requests. Weighted round-robin lets you assign a weight (e.g., 3, 1) so more powerful servers receive proportionally more traffic.

Q: Can I use round-robin for database load balancing?
A: Yes, but only for read replicas. Writes must go to a single primary database. Round-robin reads across replicas works, but be aware of replication lag.

Q: What happens if one backend server goes down?
A: With health checks enabled, the load balancer stops sending traffic to that server until it recovers. Without health checks, users may get errors.

Q: Is round-robin the best algorithm for all scenarios?
A: No. If your servers have different specs or handle long-lived connections, least-connections or IP hash may be better. Round-robin is simple and works well for homogenous, stateless services.

Q: How do I verify that round-robin is actually working?
A: Check the access logs on each backend. After sending N requests, each backend should have received approximately N/(number of backends) requests. You can also add a response header with the server IP.