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

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.

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

Total weight

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:

YAML
  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

Hands-on · See the split with your own eyes

Run two distinguishable backends:

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

Shell
for i in $(seq 1 100); do curl -s localhost:10000/i; done | sort | uniq -c

You should see roughly:

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

Mirror traffic for zero-risk testing

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.

Real-world example · Any team running a service mesh
Problem
Deploying a new pricing service to 100% of users is terrifying — a bug means refunds. The team wants to roll out gradually: 1% → 10% → 50% → 100%, with the ability to pause or roll back at any step.
Envoy solution
In Istio, they write a VirtualService with weight on each subset. Istio translates it into weighted_clusters on the right Envoy routes. The team bumps the weight in CI, Argo CD reconciles, istiod pushes new config to every Envoy in seconds. If the success metrics dip, they revert the YAML — Envoy serves the old split before anyone notices.

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.

Quick check

In a `weighted_clusters` block with two clusters at weight 90 and weight 10, what is the default `total_weight`?

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