HAProxy – How to Use ‘Set Weight’ for Load Balancing

haproxyload balancingload-testingtraffic-management

Hi I have configured an HAProxy server in haproxy.cfg to balance the traffic flow between two servers, say

server web1 10.10.10.10 weight 50
server web2 10.10.10.11 weight 50

For testing purpose we need to set the weights dynamically, so we are using
set weight <backend>/<server> <weight>[%] command as mentioned in this article http://cbonte.github.io/haproxy-dconv/configuration-1.4.html#hash-type

1) In the document they have mentioned

Relative weights are only permitted between 0 and 100%, and absolute
weights are permitted between 0 and 256.

and if I use set weight for web1 with percentage value 70%, does it means 70% in that allotted 50 for web1(which is initially configured in server) traffic is passed, if so rest of that 30% in that 50 value where it will be passed?

2) Another query is, if we use set weight command as

set weight web1 10.10.10.10 3
set weight web2 10.10.10.11 1

Does it mean that 75% of the traffic to web1 and 25% traffic to web2? How to split traffic in ratio manner and use it in set weight.

If possible please let me know how to use absolute weight for splitting traffic. Thanks for the answer.

Best Answer

Question 1:

The documentation states:

If the value ends with the '%' sign, then the new weight will be relative to the initially configured weight.

So it indeed means 70% "in that allotted 50 for web1". Where the other 30% would go, is not clear from the documentation but it would have to be web2 as it can not be dropped.

Question 2:

The HAProxy documentation states:

The "weight" parameter is used to adjust the server's weight relative to other servers. All servers will receive a load proportional to their weight relative to the sum of all weights, so the higher the weight, the higher the load. The default weight is 1, and the maximal value is 256

So, yes, it means that web1 will receive 75% = 3/(1+3) of traffic and web2 will receive 25% of traffic.

Extra questions:

How to split traffic in ratio manner and use it in set weight.

If possible please let me know how to use absolute weight for splitting traffic.

I don't understand these two questions. Can you elaborate a bit?