HAProxy Load Balancing: Performance Comparison Between Round Robin vs Least Connections Algorithms


25 views

When configuring HAProxy, the choice between roundrobin and leastconn fundamentally comes down to your application's characteristics:

# Round Robin Example
backend web_servers
    balance roundrobin
    server server1 192.168.1.1:80 check
    server server2 192.168.1.2:80 check

# Least Connections Example  
backend app_servers
    balance leastconn
    server server3 192.168.1.3:8080 check
    server server4 192.168.1.4:8080 check

The standard round-robin algorithm distributes requests equally regardless of server load. This becomes problematic when:

  • Backend servers have different processing capacities
  • Requests have varying processing times (e.g., mixed API endpoints)
  • Persistent connections are enabled

Consider these metrics from a production environment:

# Before switching (roundrobin)
Server1: CPU 85% | Active Connections: 350
Server2: CPU 45% | Active Connections: 325

# After switching (leastconn)  
Server1: CPU 65% | Active Connections: 210
Server2: CPU 60% | Active Connections: 215

For mission-critical deployments, combine leastconn with other HAProxy features:

backend optimized_pool
    balance leastconn
    stick-table type ip size 200k expire 30m
    stick on src
    server server5 192.168.1.5:443 weight 3 check maxconn 500
    server server6 192.168.1.6:443 weight 2 check maxconn 500

Key parameters to monitor during transition:

  • scur (current sessions) in HAProxy stats
  • Backend response time percentiles
  • TCP connection queue depth

For zero-downtime changes:

  1. Deploy new HAProxy config with both algorithms active
  2. Use ACLs to gradually shift traffic
  3. Monitor server metrics every 15 minutes
frontend http_in
    acl use_leastconn path_beg /api/
    use_backend app_servers if use_leastconn
    default_backend web_servers

In HAProxy, both roundrobin and leastconn are fundamental load balancing algorithms, but they operate differently:


backend web_servers
    balance roundrobin
    server server1 192.168.1.1:80 check
    server server2 192.168.1.2:80 check

Round Robin distributes requests sequentially across all available servers, while Least Connections routes traffic to the server with the fewest active connections.

Round Robin works best when:

  • All backend servers have identical hardware specifications
  • Requests are similar in processing time
  • You need predictable, sequential distribution

Least Connections shines when:


backend app_servers
    balance leastconn
    server app1 10.0.0.1:8080 check
    server app2 10.0.0.2:8080 check
    server app3 10.0.0.3:8080 check
  • Backend servers have varying processing power
  • Request processing times vary significantly
  • You need dynamic adaptation to server load

In production environments, we've observed that Least Connections often provides better load distribution when:

  1. Some requests are computationally expensive while others are lightweight
  2. Backend servers have different CPU/RAM configurations
  3. Connection durations vary (like WebSocket vs HTTP requests)

For environments with both static content and API endpoints:


frontend http_in
    bind *:80
    acl is_api path_beg /api
    use_backend api_servers if is_api
    default_backend web_servers

backend api_servers
    balance leastconn
    server api1 10.0.1.1:3000 check
    server api2 10.0.1.2:3000 check

backend web_servers
    balance roundrobin
    server web1 10.0.2.1:80 check
    server web2 10.0.2.2:80 check

Always monitor your load balancing decisions:


listen stats
    bind *:1936
    stats enable
    stats uri /haproxy?stats
    stats refresh 30s

Key metrics to watch:

  • Queue times per server
  • Active connections distribution
  • Response time percentiles

When switching algorithms in production:

  1. First test in staging with realistic traffic patterns
  2. Use the observe directive to compare algorithms
  3. Gradually shift traffic using weights

backend transition
    balance leastconn
    server server1 192.168.1.1:80 check observe layer4 weight 50
    server server2 192.168.1.2:80 check observe layer4 weight 50