envoy-guide
Module 3 · Making It Useful·12 min·intermediate

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:

PolicyOne-sentence summaryWhen to reach for it
ROUND_ROBINNext endpoint in order, on every request.The default. Works for stateless replicas of similar capacity.
LEAST_REQUESTPick the endpoint with the fewest active requests (sampled).Replicas of uneven speed; protects you from one slow pod.
RANDOMUniformly random pick.Cheap; useful when other policies behave pathologically.
RING_HASHConsistent hash on a request attribute → same key always lands on same endpoint.Sticky sessions, cache affinity, sharded backends.
MAGLEVLike 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

YAML
  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.

YAML
  routes:
  - match: { prefix: "/" }
    route:
      cluster: api
      hash_policy:
      - header: { header_name: "x-user-id" }   # hash by user id
YAML
  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.

Hands-on · Watch round-robin happen

Spin up three tiny "I am replica X" servers using nginx and a shared HTML trick — or, simpler, with Python:

Shell
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:

Shell
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.

Active health checks

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.

Real-world example · Lyft (and basically every cache-fronted service)
Problem
A real-time pricing service caches per-driver state in memory. With round-robin, the same driver's request bounces between replicas, blowing the cache and producing inconsistent pricing.
Envoy solution
Switch the cluster to MAGLEV and add a route hash_policy that hashes the x-driver-id header. Each driver pins to one replica; that replica's local cache stays warm; pricing stays consistent. When a replica is added or removed, Maglev's distribution minimizes how many drivers get re-pinned.

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.

Quick check

Which load-balancing policy gives you sticky sessions based on a request header (consistent hashing)?

Practical task
Finish the quick check and tick off the task to unlock.
Quick checkTasks 0/3