You have four backends behind HAProxy on the default roundrobin algorithm. Three are healthy and serving 100 requests per second each; the fourth is a stragger — it’s healthy according to the health check, but every request takes three times longer than the others. The LB happily sends it 25% of the traffic anyway. Tail latency on your SLO dashboard goes from 180 ms p99 to 850 ms p99 overnight. Switching to leastconn brings p99 back to 200 ms in under a minute. The algorithm wasn’t broken — it was the wrong tool for a request shape it doesn’t understand.
What’s happening
Load balancing algorithms answer the question “which backend gets the next request?” The right answer depends on what your requests look like, not on what’s idiomatic for your tooling.
Weighted round-robin sends requests to backends in rotation, weighted by configured capacity. It assumes requests are roughly uniform in cost. When they are, it’s the cheapest algorithm to compute and gives evenly distributed load. When they aren’t, it pins slow requests to the next-up backend regardless of how busy that backend is, and your tail latency reflects whichever backend got unlucky with concurrent slow requests.
Least-connections picks the backend with the fewest active connections (optionally weighted). It is the right answer when request durations vary widely — long-running uploads, mixed-cost API endpoints, slow database queries hitting a per-route timeout. The cost: it requires the LB to track connection counts per backend, which is fine at single-digit RPS-per-backend rates and starts mattering at tens of thousands. It also doesn’t help if all backends are equally saturated.
IP-hash (or any consistent hash on a request attribute) sends a given key to the same backend every time. It’s the affinity-without-cookies pattern. Useful for sticky workloads where adding a session cookie isn’t feasible (raw TCP services, naive HTTP clients, legacy clients you don’t control). The cost: NAT skew (a tenant behind one egress IP overloads one backend), poor distribution when the key space is small, and ugly rebalancing semantics when you add or remove a backend.
Random (often random with a tie-breaker on least-conn) is what big cloud LBs default to under the hood, because it scales horizontally — no shared connection-count state across LB instances. For most workloads it’s indistinguishable from round-robin; the difference shows up at scale where round-robin requires coordinated cursors across LBs.
Resource-aware (leastload, agent-check, etc.) lets the LB consult a backend-reported metric — CPU, queue depth, custom score. The right answer for asymmetric backends or burst-prone workloads, but you’ve added a feedback loop that can oscillate if the metric is noisy.
How to verify
Diagnose which algorithm you actually need by looking at the distribution your current one produces:
# 1. Requests per backend over the last 5 minutes (HAProxy stats socket)
echo "show stat" | socat stdio /run/haproxy/admin.sock \
| awk -F, '$1=="api_backend" && $2!="BACKEND" {print $2, $5, $8}' \
| column -t
# Columns: backend, scur (current sessions), stot (total since start)
# 2. p99 per backend (requires logging the backend in the LB access log)
zcat /var/log/haproxy/access.log.1.gz \
| awk '{print $NF, $(NF-2)}' \
| sort | awk '
{ count[$1]++; sum[$1]+=$2; arr[$1,count[$1]]=$2 }
END { for (b in count) {
n=count[b]; for (i=1;i<=n;i++) a[i]=arr[b,i]; asort(a);
print b, a[int(n*0.99)]
}}'
# 3. Are requests roughly uniform in cost? Look at duration histogram
awk '{print int($duration/100)*100}' /var/log/haproxy/access.log \
| sort | uniq -c | sort -k2 -n
If scur is wildly uneven across backends under round-robin, you have long-tailed request durations and leastconn will help. If stot is even but p99 differs significantly between backends, you have asymmetric backends (different sizes, noisy neighbors) and weighting or resource-aware is the answer.
The fix
Match the algorithm to the traffic shape, not to the tool default:
- Stateless HTTP API, requests roughly uniform in cost (sub-second JSON responses, no large payloads, no streaming): weighted round-robin. Weights proportional to backend capacity. This is most B2B SaaS API tiers.
- Mixed-duration HTTP traffic (PDF generation, image processing, export jobs sharing the same backends as fast endpoints): least-connections. The slow requests pile up on whichever backend is already busy with slow ones, but the fast endpoints route around them.
- WebSocket / long-lived connections: least-connections with a long max-conn budget per backend. Round-robin would assign 25% of new connections to a node that already holds 80% of the long-lived ones.
- Cache-affinity workloads (each backend caches per-tenant state, cache miss is expensive): consistent hash on tenant ID, not IP. A tenant cookie, a JWT claim, or a URL path segment is the hash key. IP-hash for this case is what people reach for and what hurts them six months later.
- Asymmetric backends in the same pool (new larger nodes added during scale-up, before old ones are drained): weighted least-connections during the migration window, then back to plain least-conn or RR once the pool is homogeneous.
Run a synthetic load test against each candidate algorithm in staging with realistic traffic shape — replay a production log, don’t generate ab -n 100000 /healthz. The healthz endpoint is uniform; production isn’t. Then pick.
Stack Harbor tunes LB pools for B2B SaaS on managed cloud compute, including replay-based capacity testing and algorithm changes during scale events. If your tail latency doesn’t match your backend health graph, walk us through the workload.