Suppose I have two Redis database and want to distribute 70% of requests to DB1 and the other 30% to DB2. How can i calculate the weight parameter?
global
maxconn 20000
log 127.0.0.1 local0
user haproxy
chroot /usr/share/haproxy
pidfile /run/haproxy.pid
daemon
defaults REDIS
mode tcp
timeout connect 4s
timeout server 30s
timeout client 30s
listen stats
bind :9000 # Listen on localhost:9000
mode http
stats enable # Enable stats page
stats hide-version # Hide HAProxy version
stats realm Haproxy\ Statistics # Title text for popup window
stats uri /haproxy_stats # Stats URI
stats auth admin:123 # Authentication credentials
frontend ft_redis
bind 127.0.0.1:5000 name redis
default_backend bk_redis
backend bk_redis
balance roundrobin
option tcp-check
tcp-check connect
tcp-check send PING\r\n
tcp-check expect string +PONG
tcp-check send info\ replication\r\n
tcp-check expect string role:master
tcp-check send QUIT\r\n
tcp-check expect string +OK
server redis_6379 localhost:7000 check inter 1s weight 179
server redis_6380 localhost:7001 check inter 1s weight 77
Best Answer
weight
has a valid range of 1 through 256 for each server, but you don't have to use 256 as a basis for the calculation.The weight of each server is the ratio of that server's declared weight to the sum of all declared weights, so with 2 servers you can just use the values 30 and 70 and the distribution will be what you would expect: 30 ÷ (30 + 70) = 0.3 and 70 ÷ (30 + 70) = 0.7. The server that "weighs more" receives proportionally more requests. You could also use 3 and 7 or 33 and 77 or combination within the 1 - 256 range.
The values you are using, 77 and 179, should give similar results, since 77 ÷ (77 + 179) ≈ 0.3 and 179 ÷ (77 + 179) ≈ 0.7... it's just harder to make the mental calculation. Keeping your configuration so all of the weights add to a total sum of 100 is a more human-friendly solution.