Table of contents
Load Balancing & Failover
This page explains how ProxyAuth distributes traffic across multiple backends and reacts to failures, when a route defines more than one target via backends in routes.yml. See the routes.yml Reference for the basic backends syntax — this page is the deep dive into how it actually behaves at request time.
If a route has no backends list, none of this applies — the request simply goes to target.
When does this kick in?
Only for routes with a non-empty backends list:
- prefix: "/orders"
target: "http://10.0.0.1:8080" # still required, used for scheme/authority fallback
backends:
- url: "http://10.0.0.1:8080"
weight: 3
- url: "http://10.0.0.2:8080"
weight: 3
- url: "http://10.0.0.9:8080"
weight: -1 # last resort only
Every request to /orders then goes through the flow described below instead of being sent directly to target.
Request flow, step by step
flowchart TD
A(["Incoming request"]) --> B{"1. Sticky cache check<br/>recent success for this (method, host)?"}
B -->|hit| C(["Retry that backend first"])
B -->|"miss, or that backend just failed"| D["2. Build SWRR order<br/>for active backends (weight != -1)"]
D --> E["3. Try each active backend in order<br/>skipping any in cooldown"]
E -->|"all active backends failed<br/>or in cooldown"| F["4. Try each last-resort backend<br/>weight == -1, skipping any in cooldown"]
F -->|all failed too| G(["5. 503 Service Unavailable"])
classDef step fill:#f8fafc,stroke:#64748b,stroke-width:1px,color:#0f172a
classDef decision fill:#fef3c7,stroke:#d97706,stroke-width:1px,color:#78350f
classDef ok fill:#dcfce7,stroke:#16a34a,stroke-width:1px,color:#14532d
classDef ko fill:#fee2e2,stroke:#dc2626,stroke-width:1px,color:#7f1d1d
classDef edge fill:#eef2ff,stroke:#4f46e5,stroke-width:1px,color:#1e1b4b
class A edge
class B decision
class D,E,F step
class C ok
class G ko
1. Sticky cache ("last good backend")
After a successful response, ProxyAuth remembers which backend served it, keyed by (HTTP method, target host), for 2 seconds by default. The next matching request tries that same backend first, skipping the full selection process — this smooths out bursts of traffic and avoids unnecessary re-balancing on every single request.
⚠️ Caveat: the cache key is
(method, host), not tied to the specific route (prefix). If two different routes happen to share the same backend host, a successful pick on one route can be reused for the other route's traffic too. This is rarely an issue in practice (most setups use distinct hosts per route), but worth knowing if you see unexpected backend selection across routes that share a host.
2. Weighted backend selection (SWRR)
Active backends (weight != -1) are ordered using Smooth Weighted Round Robin — the same algorithm used by nginx's weighted upstream blocks. It's not random and not strict round-robin; it interleaves picks so that, over any short window of requests, each backend gets traffic roughly proportional to its weight, without bursts of consecutive requests to the same one.
Example with weights A=3, B=3, C=1 — a typical resulting order across 7 picks might look like:
A, B, A, B, C, A, B
rather than A, A, A, B, B, B, C (strict grouping) or a coin flip each time.
- Weight defaults to
1if you use the plain-string form (- "http://...") instead of the object form. - Higher weight = proportionally more traffic, not an absolute priority — a weight-3 backend isn't "preferred until it fails," it just gets picked more often.
3 & 4. Failover order: active backends, then last-resort
- All
weight != -1backends are tried first, in SWRR order, skipping any currently in cooldown (see below). - If every active backend fails or is in cooldown, ProxyAuth falls back to
weight: -1backends — these are never used otherwise, regardless of how loaded the active backends are. Use this for a genuine break-glass node (e.g. a smaller/slower standby), not for normal capacity. - Each backend in the pool is tried at most once per request.
5. Total failure
If nothing works — every backend either failed, timed out, or was in cooldown — the client gets 503 Service Unavailable.
What counts as a "failure"
A backend attempt is treated as a failure, and counts toward its cooldown, in three cases:
- Connection error — couldn't connect, TLS handshake failed, connection reset, etc.
- Timeout — no response within
request_timeout_ms(see Tuning below). - 5xx response — the backend answered, but with a server-error status code. Note that 4xx responses are not treated as failures and are passed straight through to the client without trying another backend — a
404/403/etc. from the backend is assumed to be a legitimate answer, not a backend health problem.
⚠️ Important: on failure, ProxyAuth retries the exact same request (same method, body, headers) against the next backend. This is safe for
GET/HEAD. ForPOST/PUT/DELETE/PATCH, if the failing backend actually applied a side effect before returning a 5xx (e.g. it wrote to its own database, then hit a downstream timeout), the retried request can cause that side effect to happen a second time on a different backend. If you usebackendson a route with non-idempotent methods, make sure your backend handlers are safe to receive the same request twice (idempotency keys, dedup, etc.), or restrictallow_methodstoGET/HEADon load-balanced routes where that's not the case.
Cooldown (circuit breaker)
When a backend fails, it's put into cooldown so subsequent requests skip it instead of repeatedly hitting a backend that's already down.
- First failure: ~2 second cooldown.
- Each additional failure (while it keeps failing) increases the cooldown, up to a cap of ~5 seconds — so a backend that's been failing repeatedly gets left alone slightly longer than one that just failed once, without ever blocking it out for very long.
- Recovery: cooldown state (and the failure count) is dropped entirely once a backend goes ~10 seconds without a new failure, or as soon as it succeeds again (a successful response clears its cooldown state immediately).
There is no active health checking — ProxyAuth never proactively pings backends in the background. Health is purely inferred from real traffic: a backend is only known to be down after an actual request to it fails, and only known to have recovered after an actual request to it succeeds. On a very low-traffic route, this means detecting recovery can take as long as it takes for the next real request to arrive.
Tuning
Three timing/pool parameters come from your config.json (already documented in the config.json Reference) and apply to the load balancer too:
config.json key |
Effect on the load balancer |
|---|---|
client_timeout |
Per-backend request timeout, in milliseconds — how long ProxyAuth waits for a backend before treating the attempt as a timeout/failure. |
max_idle_per_host |
Max idle pooled connections kept open per backend, reused across requests. |
keep_alive |
Keep-alive duration for pooled backend connections. |
The remaining four parameters — sticky-cache TTL (2s), initial cooldown (2s), max cooldown (5s), and cooldown-reset threshold (10s) — are currently hardcoded defaults, not exposed in config.json. If your workload needs different values (e.g. a slower-recovering backend that shouldn't be retried for longer than 5s), that currently requires a code change rather than a config change.
Practical guidance
- Don't use
backendsfor a single target. If you only have one backend, just settargetand skipbackendsentirely — it adds pooling/cooldown/SWRR bookkeeping for no benefit. - Weight backends by real capacity, not just "primary vs backup" — e.g. a bigger instance getting
weight: 3against a smaller one'sweight: 1sends it roughly 3x the traffic, not "prefer it exclusively." - Reserve
weight: -1for genuine last resorts — a maintenance-mode node, a smaller emergency instance, or a different region you'd rather not use under normal conditions. - Be deliberate about non-idempotent routes — either accept the small risk of a rare double-execution on failover, add idempotency handling backend-side, or don't put mutating routes behind multi-backend failover if that risk isn't acceptable for your use case.
- Responses are fully buffered in memory before being returned to the client on this code path (no streaming) — keep this in mind for routes that return large payloads (file downloads, big API responses) under high concurrency.
See also
- routes.yml Reference — the
backendsfield's basic syntax, alongside every other per-route option. - config.json Reference —
client_timeout,max_idle_per_host, andkeep_alive, which feed the tuning parameters above.
📖 Wiki Home · 💻 Source Code · 🐛 Report an Issue · 📜 License
Found an error or something outdated on this page? Open an issue or submit a PR against the wiki repository.