9.3 KiB
Load balancing algorithms and location backend groups
Every reverse-proxy Vhost has one backend pool. Since 0.40.2 the pool can also balance by a consistent hash of a request key (path, URI, host, header, cookie, query parameter or client IP), and each custom location can send its requests to its own backend group with its own algorithm — the RustProxy equivalent of a HAProxy use_backend rule pointing at a backend with a different balance line.
Configure it in the panel under Vhosts → Edit → Backends → Balancing (whole Vhost) and Vhosts → Edit → Custom locations → Backends & balancing (one location).
Algorithms
algorithm |
Behaviour | HAProxy equivalent |
|---|---|---|
least_connections (default) |
Fewest in-flight requests per weight. | balance leastconn |
round_robin |
Smooth weighted round robin. | balance roundrobin |
least_traffic |
Fewest bytes per second (EWMA) per weight. | — |
adaptive |
In-flight requests × measured latency + traffic, per weight. | — |
random |
Two random members, the less loaded one wins (power of two choices). | balance random(2) |
hash |
Consistent hash of the key configured in hash. |
balance hash … / uri / hdr() / url_param + hash-type consistent |
ip_hash |
Consistent hash of the client address (kept for compatibility; equivalent to hash with source: "ip"). |
balance source + hash-type consistent |
Weights, backup, drain, health checks, passive ejection, slow start and concurrency limits apply to every algorithm.
Consistent hashing (hash)
RustProxy ranks the members for each key with weighted rendezvous hashing (highest random weight). Hashing is always consistent — there is no map-based mode:
- the same key always selects the same member while that member is eligible;
- when a member fails, is drained, disabled or removed, only the keys of that member move, and they return when it recovers;
- a member with weight 3 receives about three times as many keys as a member with weight 1;
- when the preferred member is at its
max_concurrent_requestslimit the next member in the key's order is used for that request.
load_balancing.hash settings (all optional):
| Field | Default | Meaning |
|---|---|---|
source |
path |
path (path without query, as received from the client, before location rewrites), uri (path + query string), host (host name without port), header, cookie, query (one query parameter) or ip (client address seen by RustProxy). |
name |
— | Header, cookie or query parameter name; required for header, cookie and query, rejected otherwise. Header values from repeated lines are joined with ,; the first matching cookie/parameter is used; values are not URL-decoded. |
path_depth |
0 |
For path/uri: hash only the first N directories (/music/album/01.mp3 with depth 2 hashes /music/album), so a whole directory stays on one member. 0 hashes the whole path. Range 0–32. |
ignore_case |
false |
Lower-case the key before hashing (useful for file names that differ only in case). |
balance_factor |
0 |
Consistent hashing with bounded loads, like HAProxy hash-balance-factor: a member may take a new request only while its in-flight requests stay within this percentage of its weighted share of the pool load; otherwise the next member for that key is used. 0 disables the bound; otherwise 101–1000 (125 is a common choice). Hot keys spill over deterministically and return when the load drops. |
fallback |
round_robin |
Algorithm for requests without a key value (missing header, cookie or parameter): round_robin, least_connections, least_traffic, adaptive or random. |
Sticky affinity, when enabled, takes precedence over the algorithm: an ip_hash sticky mode always hashes the client address, and a valid affinity cookie selects its member before hashing.
The client IP is the transport peer (or the address accepted from trusted forwarding settings); X-Forwarded-For sent by a client cannot change it.
Example: the whole Vhost hashes by path
"load_balancing": {
"algorithm": "hash",
"hash": { "source": "path", "ignore_case": true, "balance_factor": 150 },
"health_check": { "mode": "http", "path": "/health" }
}
Example: per-tenant routing by header, round robin without the header
"load_balancing": {
"algorithm": "hash",
"hash": { "source": "header", "name": "X-Tenant", "fallback": "least_connections" }
}
Location backend groups
A custom location with the Proxy action chooses its backends with backend_ids and, optionally, its own balancing with load_balancing:
| Location field | Meaning |
|---|---|
backend_id |
Pin one backend (unchanged). Cannot be combined with the two fields below. |
backend_ids |
Backend group: ids of Vhost backends (up to 64, no duplicates). Empty means the whole Vhost pool. Members whose pool_eligible is false (“Include in the default load-balancing pool” off) are used only by locations that list them. |
load_balancing |
Location balancing: algorithm, hash (same fields as above) and sticky (off default, cookie or ip_hash). Without it the location inherits the Vhost algorithm, hash settings and affinity. |
Members are shared with the Vhost pool: active and passive health, ejection, draining, concurrency limits, retries, slow start and statistics are the same objects, and the Backends status view shows them once. Retries stay within the location's group. A location affinity cookie is named <vhost cookie>__<location id> so it never competes with the Vhost cookie, and affinity cookies of the Vhost and its locations are stripped before requests reach an origin.
Example: HAProxy use_backend mp3_shards if is_mp3 with balance hash path
HAProxy:
frontend https_front
acl is_mp3 path_end -i .mp3
use_backend mp3_shards if is_mp3
backend mp3_shards
balance hash path
hash-type consistent
server mp3_1 10.0.0.11:80 check
server mp3_2 10.0.0.12:80 check
server mp3_3 10.0.0.13:80 check
RustProxy Vhost:
{
"id": "music",
"frontend": { "hosts": ["music.example.com"], "listener_ids": ["https"] },
"backends": [
{ "id": "app", "url": "http://10.0.0.5:8080", "allow_private": true },
{ "id": "mp3_1", "url": "http://10.0.0.11:80", "allow_private": true, "pool_eligible": false },
{ "id": "mp3_2", "url": "http://10.0.0.12:80", "allow_private": true, "pool_eligible": false },
{ "id": "mp3_3", "url": "http://10.0.0.13:80", "allow_private": true, "pool_eligible": false }
],
"load_balancing": { "algorithm": "least_connections", "health_check": { "mode": "tcp" } },
"routes": [
{
"id": "mp3",
"match_type": "regex_insensitive",
"path_prefix": "\\.mp3$",
"backend_ids": ["mp3_1", "mp3_2", "mp3_3"],
"load_balancing": { "algorithm": "hash", "hash": { "source": "path" } }
}
]
}
path_end -i .mp3 becomes a case-insensitive regex location; check corresponds to the Vhost health check (TCP by default). Everything else on the host keeps using app with least connections.
In the panel: add the three backends with Include in the default load-balancing pool off, add a custom location, choose Backends → Backend group, tick the three backends, turn on Own balancing for this location and click Files / cache shards by path.
Locations can additionally require request conditions (header, cookie, query parameter, method, client IP, host, country), and a match type any turns a location into a rule for the whole host — together the full HAProxy use_backend <group> if <acls> pattern. See Custom locations → Request conditions.
Panel helpers
- Quick setup buttons fill typical combinations: files by path, session cookie, tenant header, query parameter, client IP, or even load spreading.
- Each algorithm shows a short explanation, and hash settings show the HAProxy equivalent lines for comparison.
- Test distribution runs in the browser on the unsaved draft. Paste sample paths, URLs or key values to see the hash key and the selected backend for each, the share per backend, and — after ticking Simulate failure of — which keys would move. The simulator uses the same SHA-256 rendezvous scoring as the proxy (checked by
tools/dev/test_balance_simulator.js); load-based algorithms show the expected share by weight instead. It does not account for current concurrency limits, bounded-load spill-over or live health.
Compatibility and limits
- Configurations without
hash,backend_idsor locationload_balancingload and behave exactly as before; these fields are omitted when unset, so saved files stay unchanged.ip_hashkeeps the same client → backend mapping as earlier versions. - A configuration that uses the new fields is rejected by 0.40.1 and older.
- Hashing reads the request as received. Changing a member id changes its keys; renaming the Vhost id reshuffles all keys (the Vhost id is part of the hash input).
- Consistent hashing spreads keys, not requests: a few very popular keys still load their members more. Use
balance_factorfor hot keys, or weights. - Headless JSON uses the same fields; the panel and API validate them before activation.