Canary and traffic shifting
Send 10% of traffic to a new backend with weighted clusters.
Why canary?
A canary release sends a small percentage of real traffic to a new version and watches what happens before promoting it. If the new version is broken, only a few users see it; you roll back instantly by changing one number.
Without a proxy, this is a pain. Apps would need to know about each other's versions; clients would need version-aware routing. With Envoy, it's a five-line config change.
The primitive is weighted_clusters on a route.
The simplest canary
Say you have two clusters, api_v1 and api_v2, both healthy. You want
90% of traffic on v1, 10% on v2.
routes:
- match: { prefix: "/" }
route:
weighted_clusters:
clusters:
- { name: api_v1, weight: 90 }
- { name: api_v2, weight: 10 }
That's it. Restart (or push the change via xDS in a real mesh) and traffic splits 90/10. To shift to 50/50, change the numbers and reload.
By default, total_weight is the sum of the listed weights — so
90+10=100 just works. You only need to set total_weight
explicitly if you want unspecified clusters to make up the rest.
A safer shape: header-based canary
Pure-percentage canaries spray new code at random users. Often you'd rather:
- Internal users first — only employees see v2.
- Region first — only EU users see v2.
- Opt-in — users with a feature-flag cookie see v2; everyone else stays on v1.
You do this by combining a header match with the weighted split:
routes:
# Opt-in beta users
- match:
prefix: "/"
headers:
- name: "x-beta-user"
string_match: { exact: "true" }
route: { cluster: api_v2 }
# Everyone else: 99% v1, 1% v2
- match: { prefix: "/" }
route:
weighted_clusters:
clusters:
- { name: api_v1, weight: 99 }
- { name: api_v2, weight: 1 }
Order matters: the header-matched route is checked first.
Hands-on: 90/10 split with two backends
Run two distinguishable backends:
docker run -d --rm --name v1 -p 5201:80 python:3.12-alpine \
sh -c "echo v1 > /tmp/i && python -m http.server 80 --directory /tmp"
docker run -d --rm --name v2 -p 5202:80 python:3.12-alpine \
sh -c "echo v2 > /tmp/i && python -m http.server 80 --directory /tmp"
Wire two clusters (v1 → 5201, v2 → 5202) and a
weighted route (90 v1, 10 v2). Then run a hundred requests:
for i in $(seq 1 100); do curl -s localhost:10000/i; done | sort | uniq -c
You should see roughly:
88 v1
12 v2
Numbers won't be exactly 90/10 — random variance in 100 samples is large. Run 1000 to tighten it up. Now flip to 50/50, restart, run again.
Stop with docker stop v1 v2.
If you want to exercise v2 with real production traffic but not let it
affect responses, use request_mirror_policies on the route.
Envoy fires a fire-and-forget shadow request to v2 while still answering
the user from v1. You learn whether v2 panics under real load without
risking a single user-visible error.
Module 3 in one sentence
You can now run multiple healthy backends, balance load between them, encrypt traffic to and from Envoy, fail gracefully when things go wrong, and ship new versions safely. That's the back half of every microservice platform.
Module 4 turns on the lights — logs, metrics, and traces.
In a `weighted_clusters` block with two clusters at weight 90 and weight 10, what is the default `total_weight`?