Bootstrap FreeKB - HAProxy (Load Balance) - Load Balancing Algorithms (round robin, leastconn and source)
HAProxy (Load Balance) - Load Balancing Algorithms (round robin, leastconn and source)


This assumes you have installed HAProxy.

Let's say you have two servers, server1.example.com and server2.example.com, and these servers both serve the www.example.com web pages. By default, HAProxy will use round robin to alternate the requests between server1.example.com and server2.example.com.

There are 3 different load balancing algorithms that can be used.

  • round robin alternates requests between servers
  • leastconn routes requests to the server with the least number of connections
  • source routes requests to the server based on a hash of the IP address from the client

One option would be to use the listen block.

  • The httploghttp-request and log-format directives are not needed but are really helpful for debugging. Refer to HAProxy Logging for more details on logging.
listen web
    bind *:80
    mode tcp
    balance roundrobin
    option httplog
    http-request capture req.hdrs len 1024
    log-format "%ci:%cp [%tr] %ft [[%hr]] %hs %{+Q}r"
    server www1 www1.example.com:80 check www1
    server www2 www2.example.com:80 check www2

 

Or frontend and backend blocks can be used. This is more commonly used for more complex situations, such as when there are two servers serving multiple domains.

frontend main
    bind *:80
    balance source
    default_backend stage

backend stage
    server server1 10.0.0.1:12345 check server1
    server server2 10.0.0.2:12345 check server2

 

Or in each backend.

frontend main
    bind *:80
    default_backend foo

backend foo
    balance roundrobin
    server server1 10.0.0.1:11111 check server1
    server server2 10.0.0.2:11111 check server2
    
backend bar
    balance source
    server server1 10.0.0.1:22222 check server1
    server server2 10.0.0.2:22222 check server2    

 

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 1745d2 in the box below so that we can be sure you are a human.