TheDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/cloudflare/pingora/llms.txt
Use this file to discover all available pages before exploring further.
pingora-load-balancing crate provides a composable system for managing a set of upstream backends. It combines three orthogonal concerns — service discovery (how backends are found), health checking (whether each backend can serve traffic), and selection (which backend to pick for a given request) — into a single LoadBalancer<S> struct that can be run as a background service alongside your proxy.
The
lb feature flag in the main pingora crate enables this crate and re-exports its public API under pingora::lb. Enable it with features = ["openssl", "lb"] in your Cargo.toml.Backend
ABackend represents a single upstream server. It holds an address, a relative weight, and an open-ended extension map for arbitrary metadata.
Backend with the unit weight of 1:
LoadBalancer
LoadBalancer<S> is the central type. The generic parameter S is the selection algorithm — any type that implements BackendSelection. It wraps a Backends collection (which holds discovery + health), a selector built from the current backend set, and timing controls for periodic updates.
Building from a static list
The simplest way to create aLoadBalancer is from an iterator of addresses:
try_from_iter performs a synchronous DNS lookup for each address (blocking network I/O), builds the backend set, and runs the initial discovery step immediately.
Selecting a backend
Callselect to get the next healthy backend:
select_with to apply a custom acceptance predicate — for example, to skip backends that your application has seen fail recently:
Configuring update frequencies
None means that phase runs only once at startup.
Selection Algorithms
pingora-load-balancing ships three ready-made selection algorithms and a trait for custom ones.
RoundRobin
Sequential, weighted round-robin across all healthy backends. The key argument to select is ignored; backends are served in order, repeating proportionally to their weight.
Consistent (Ketama)
Consistent hashing using the Ketama algorithm from pingora-ketama. The same key will always map to the same backend as long as the backend set does not change. When backends are added or removed, only the minimum necessary fraction of keys are remapped.
FNVHash / Random
FNVHash (also exported as FVNHash for backward compatibility) uses the FNV hash function on weighted backends, giving a consistent mapping without the full Ketama ring overhead. Random picks backends uniformly at random, weighted by weight.
Custom: implement BackendSelection
Health Checks
Health checks probe each backend on a configurable schedule. Unhealthy backends are automatically excluded fromselect results and are re-included once they recover.
TcpHealthCheck
Attempts a TCP (or optional TLS) connection to each backend. A backend becomes unhealthy after consecutive_failure consecutive failures and recovers after consecutive_success consecutive successes (both default to 1).
HttpHealthCheck
Sends an HTTP GET request and validates the response. By default, any 200 OK counts as healthy.
Attaching a health check to a LoadBalancer
127.0.0.1:79 fails its TCP check, lb.select(...) will never return it until the check passes again. The state machine requires consecutive_failure failures before marking a backend unhealthy and consecutive_success successes before marking it healthy again, preventing flapping on transient errors.
Service Discovery
Service discovery controls which backends exist. TheServiceDiscovery trait returns a fresh BTreeSet<Backend> on each call; the LoadBalancer compares this to its current set and rebuilds the selector only when something changes.
Backends object and then build the LoadBalancer:
Built-in: Static
For fixed backend sets, use discovery::Static. It is what try_from_iter uses under the hood:
ServiceDiscovery trait directly rather than using Static.
Timing Information
After each successfulupdate() call, timing data is stored and can be retrieved:
update_frequency accordingly.