From: Willy Tarreau Date: Fri, 13 Nov 2026 09:39:55 +0000 (+0100) Subject: MEDIUM: backend: avoid picking the same server twice in random draws X-Git-Url: https://git.kaiwu.me/ngx_http_access_module.html?a=commitdiff_plain;h=e6ed42090abca3ecd232004b95b8593fba3e9aea;p=haproxy.git MEDIUM: backend: avoid picking the same server twice in random draws Aleksandar Lazic suggested [1] that the random() distribution could be improved to avoid picking the same server twice, comparing it with itself and choosing it, possibly even if suboptimal. His mesurements showed that a slow server was picked more often than desired. It turns out that we don't need extra draws to avoid this situation, because get_server_rnd() uses chash_get_server_hash(), which supports a server to avoid (normally used for redispatch, but usable here as well to avoid picking the same server twice), so we can pass there the pointer to the currently chosen best server and count on the function to pick another one. Before the patch, a config with 1 slow server responding in 50ms, and N-1 fast servers responding in 5ms would give this distribution for 100k requests at 100 concurrency: 4 srv: req rate= 11.1k/s 8 srv: req rate = 15.8k/s srv max total srv max total s0 63 8525 s0 29 2248 s1 27 28296 s1 17 13874 s2 28 31049 s2 17 13869 s3 29 32130 s3 18 13996 s4 18 14316 s5 17 14325 s6 16 13758 s7 16 13614 Now with the patch: 4 srv: req rate= 14.6k/s 8 srv: req rate = 16.6k/s srv max total srv max total s0 29 3572 s0 17 1669 s1 28 31380 s1 15 14007 s2 29 32513 s2 16 14010 s3 29 32535 s3 15 14059 s4 16 14205 s5 16 14215 s6 16 13968 s7 15 13867 It's clear that the distribution is much better and follows the ratios of response times, with little to no difference in maximum server load, thus being much closer to a leastconn algo. The average rate is also better, +33% for 4 servers and +5% for 8. Note that in order to avoid wasting CPU cycles in degenerate farm cases, the two previous patches are strongly recommended: OPTIM: lb-chash: don't look for more servers when there is only one OPTIM: lb-chash: limit the scan to few consecutive nodes [1] https://www.mail-archive.com/haproxy@formilux.org/msg47740.html --- diff --git a/src/backend.c b/src/backend.c index 2f0fe7ea2..ae2154f8e 100644 --- a/src/backend.c +++ b/src/backend.c @@ -584,7 +584,7 @@ struct server *get_server_rnd(struct stream *s, const struct server *avoid) do { prev = curr; hash = statistical_prng(); - curr = chash_get_server_hash(px, hash, avoid); + curr = chash_get_server_hash(px, hash, avoid ? avoid : prev); if (!curr) break;