Load balancing
Spread traffic across replicas with the right policy.
What "load balancing" actually means here
Once a cluster has more than one endpoint, Envoy needs to decide which one
gets the next request. That decision is the load-balancing policy, set
per-cluster with the lb_policy field.
Five policies cover almost every real use case:
| Policy | One-sentence summary | When to reach for it |
|---|---|---|
ROUND_ROBIN | Next endpoint in order, on every request. | The default. Works for stateless replicas of similar capacity. |
LEAST_REQUEST | Pick the endpoint with the fewest active requests (sampled). | Replicas of uneven speed; protects you from one slow pod. |
RANDOM | Uniformly random pick. | Cheap; useful when other policies behave pathologically. |
RING_HASH | Consistent hash on a request attribute → same key always lands on same endpoint. | Sticky sessions, cache affinity, sharded backends. |
MAGLEV | Like RING_HASH but with better distribution and faster lookup. | Same goals as RING_HASH, generally preferred. |
There's also CLUSTER_PROVIDED, LOAD_BALANCING_POLICY_CONFIG, and a few
exotic ones — leave them for later.
A round-robin cluster with three endpoints
clusters:
- name: api
type: STRICT_DNS
connect_timeout: 1s
lb_policy: ROUND_ROBIN
load_assignment:
cluster_name: api
endpoints:
- lb_endpoints:
- endpoint: { address: { socket_address: { address: api-1, port_value: 8080 }}}
- endpoint: { address: { socket_address: { address: api-2, port_value: 8080 }}}
- endpoint: { address: { socket_address: { address: api-3, port_value: 8080 }}}
Three endpoints, round-robin. Successive requests will hit api-1, then
api-2, then api-3, then back to api-1.
Sticky sessions with RING_HASH
For sticky behavior you need two things: a policy that hashes (RING_HASH
or MAGLEV) and a route that says what to hash.
Same user-id always lands on the same pod — that's the whole point.
routes:
- match: { prefix: "/" }
route:
cluster: api
hash_policy:
- header: { header_name: "x-user-id" } # hash by user id
clusters:
- name: api
lb_policy: RING_HASH
# endpoints as above
Now every request with the same x-user-id value goes to the same backend.
Cache hit rates skyrocket; session state stays put.
Spin up three tiny "I am replica X" servers using nginx and a shared HTML trick — or, simpler, with Python:
docker run -d --rm --name r1 -p 5101:80 python:3.12-alpine \
sh -c "echo replica-1 > /tmp/i && python -m http.server 80 --directory /tmp"
docker run -d --rm --name r2 -p 5102:80 python:3.12-alpine \
sh -c "echo replica-2 > /tmp/i && python -m http.server 80 --directory /tmp"
docker run -d --rm --name r3 -p 5103:80 python:3.12-alpine \
sh -c "echo replica-3 > /tmp/i && python -m http.server 80 --directory /tmp"
Wire them as three STATIC endpoints in one cluster (use host.docker.internal
or your host LAN IP — see Lesson 6). With lb_policy: ROUND_ROBIN:
for i in 1 2 3 4 5 6; do curl -s localhost:10000/i; done
You should see replica-1 replica-2 replica-3 replica-1 replica-2 replica-3.
Now change lb_policy to RANDOM, restart, and run the loop again — order
becomes shuffled. Stop with docker stop r1 r2 r3.
Round-robin without health checks will happily send 1/3 of requests to a
crashed endpoint until passive outlier detection kicks in. Add an
health_checks block to the cluster (HTTP path, interval,
unhealthy_threshold) so Envoy stops sending traffic to dead replicas
within seconds.
What's next
Even one really good replica is no good if the connection between the client and Envoy is plain HTTP in 2026. Lesson 9 — TLS termination.
Which load-balancing policy gives you sticky sessions based on a request header (consistent hashing)?