Canary Releases & Automated Analysis
A canary release sends a small share of real traffic to the new version, compares its health with the current version, and either increases the share step by step or rolls back. The name comes from the canaries miners carried: a small, early warning before the whole mine is affected.
step 1: 95% → v1 (stable) 5% → v2 (canary) analyse 10 min
step 2: 75% → v1 25% → v2 analyse 10 min
step 3: 0% → v1 100% → v2 done
any failed analysis → 100% back to v1Traffic splitting
| Mechanism | How the split is made |
|---|---|
| replica ratio | 1 canary pod next to 19 stable pods behind one Service ≈ 5 %; coarse, and tied to the replica count |
| weighted routing | Gateway API HTTPRoute backendRefs weights, a service mesh (Istio, Linkerd), an ingress controller's canary feature, AWS ALB weighted target groups |
| header/cookie routing | send internal users or a beta cohort to the canary first |
| progressive delivery controllers | Argo Rollouts, Flagger: run the steps and the analysis for you |
A Gateway API route with a 95/5 split:
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
name: shop
spec:
parentRefs:
- name: public-gateway
rules:
- backendRefs:
- name: shop-stable
port: 80
weight: 95
- name: shop-canary
port: 80
weight: 5The pipeline (or a controller) changes the weights at each step. An Argo Rollouts canary strategy describes the steps declaratively (setWeight, pause, analysis); see the GitOps course.
Analysis: comparing canary with baseline
The decision at each step compares the canary with the stable version over the same time window, not with a fixed number: if both have a 2 % error rate because a downstream service is having a bad day, the canary isn't to blame. Typical checks:
- error rate (5xx / all requests) of canary vs stable;
- latency percentiles (p95, p99);
- saturation (CPU, memory), crashes and restarts;
- business metrics where possible (checkouts per minute).
The model runs a three-step canary for two versions: one good, one with a bug that adds errors on 4 % of requests. Requests are generated with a seeded random generator, so each run gives the same result. The analysis requires at least 200 canary requests, then fails the step if the canary's error rate exceeds the stable one by more than one percentage point:
import random
STEPS = [5, 25, 50, 100] # canary share of traffic, %
BASE_ERROR = 0.01 # both versions see 1% errors from downstream noise
def run_canary(extra_error, seed, requests_per_step=4000):
rng = random.Random(seed)
exposed = 0 # users who hit a canary error caused by the bug
for weight in STEPS:
stats = {"stable": [0, 0], "canary": [0, 0]} # [requests, errors]
for _ in range(requests_per_step):
target = "canary" if rng.random() < weight / 100 else "stable"
p_err = BASE_ERROR + (extra_error if target == "canary" else 0)
failed = rng.random() < p_err
stats[target][0] += 1
stats[target][1] += failed
if target == "canary" and failed and rng.random() < extra_error / p_err:
exposed += 1
if weight == 100:
print(f" 100% canary: all traffic on the new version -> promoted")
break
rate = {k: (e / n if n else 0.0) for k, (n, e) in stats.items()}
print(f" {weight:>3}% canary: {stats['canary'][0]:>4} req, canary {rate['canary']:.2%} "
f"vs stable {rate['stable']:.2%}", end="")
if stats["canary"][0] < 200:
print(" -> not enough data, extend the step")
continue
if rate["canary"] - rate["stable"] > 0.01:
print(" -> FAIL: roll back to stable")
break
print(" -> pass")
return exposed
print("good release:")
run_canary(extra_error=0.0, seed=1)
print("buggy release (+4% errors):")
hurt = run_canary(extra_error=0.04, seed=1)
print(f"requests that hit the bug before rollback: {hurt}")
print(f"with a blue/green switch, the same bug in 4000 requests would hit about {int(4000 * 0.04)}")good release:
5% canary: 216 req, canary 0.46% vs stable 1.24% -> pass
25% canary: 1048 req, canary 1.34% vs stable 1.29% -> pass
50% canary: 1976 req, canary 1.21% vs stable 1.24% -> pass
100% canary: all traffic on the new version -> promoted
buggy release (+4% errors):
5% canary: 228 req, canary 7.46% vs stable 0.95% -> FAIL: roll back to stable
requests that hit the bug before rollback: 16
with a blue/green switch, the same bug in 4000 requests would hit about 160
Statistics matter
With 5 % of a small service's traffic, a canary step may see only a few dozen requests: a single error is then a 3 % error rate, and the analysis flips a coin. Production tools deal with this by requiring a minimum sample size per step (as the model does), running longer steps for low-traffic services, using statistical tests (Kayenta's Mann-Whitney test, for example), and failing only after several consecutive bad measurements. For a service with very little traffic, a canary gives weak evidence; blue/green with good smoke tests may be the honest choice.
Canary checklist
- the canary runs the same artifact that will be promoted (same image digest);
- metrics are labelled by version (
version="v2") so canary and stable can be compared; - rollback is automatic and doesn't need the pipeline that started the release;
- sticky routing when users would notice switching between versions (e.g. UI changes);
- the database and APIs are compatible with both versions (next lesson).
Test yourself
Why compare the canary's error rate with the stable version's in the same window, instead of with a fixed threshold?
- It's faster
- Shared causes (a downstream outage, a traffic spike) affect both; only the difference is the canary's fault
- Fixed thresholds are not allowed
- Stable has no errors
A low-traffic service sends 5% of 300 requests per step to the canary. What is the problem?
- The canary is too big
- About 15 requests are too few for a meaningful error-rate comparison
- Canaries need 50% traffic
- Nothing
In the model, why did far fewer requests hit the bug than a blue/green switch would have caused?
- The bug was fixed
- Only 5% of traffic reached the canary, and the analysis rolled back at the first step
- The canary had more replicas
- Errors were retried