Skip to content
KALANBLON
Show contents

Chapter 6 of 7 · 25 min

Project: a bus company's delivery chain

By the end of this chapter, you'll be able to

  • Assemble continuous integration, a versioned image and a canary deployment
  • Define objective criteria to accept or reject a version
  • Automate rollback
  • Measure the benefit of a canary compared to an all-at-once deployment

The chain's rules

The team agreed on written rules, before writing a single line of automation:

  1. no version is deployed if the tests fail;
  2. each image carries its version number, and is never overwritten;
  3. the canary goes through three stages: 5%, 25%, then 100% of the traffic;
  4. at each stage, the version is rejected if its error rate exceeds the current version's by more than 0.5 points, or if its p95 exceeds the current one's by more than 20%;
  5. a rejection sends all traffic back to the previous version, without human intervention.

The complete chain

REFERENCE = {"errors": 0.3, "p95": 420}

def tests(version):
    prices = {"Dakar-Thiès": 3000, "Dakar-Saint-Louis": 7000}
    def total(route, seats):
        return prices[route] * seats
    return total("Dakar-Thiès", 2) == 6000 and total("Dakar-Saint-Louis", 1) == 7000

def problem(measures, reference=REFERENCE):
    if measures["errors"] > reference["errors"] + 0.5:
        return f"errors {measures['errors']}% vs {reference['errors']}%"
    if measures["p95"] > reference["p95"] * 1.2:
        return f"p95 {measures['p95']} ms vs {reference['p95']} ms"
    return None

def deliver(version, stages):
    print(f"== booking:{version}")
    if not tests(version):
        print("   CI: failed, nothing is deployed")
        return "blocked"
    print(f"   CI: tests ✓, image booking:{version} published")
    for share, measures in stages:
        cause = problem(measures)
        if cause:
            print(f"   canary {share:>3}%: {cause} → automatic rollback to the previous version")
            return "cancelled"
        print(f"   canary {share:>3}%: ✓")
    return "deployed"

good = [(5, {"errors": 0.3, "p95": 410}), (25, {"errors": 0.4, "p95": 430}), (100, {"errors": 0.3, "p95": 425})]
faulty = [(5, {"errors": 0.4, "p95": 440}), (25, {"errors": 2.1, "p95": 460})]
print("→", deliver("3.2.0", good))
print("→", deliver("3.3.0", faulty))
== booking:3.2.0
   CI: tests ✓, image booking:3.2.0 published
   canary   5%: ✓
   canary  25%: ✓
   canary 100%: ✓
→ deployed
== booking:3.3.0
   CI: tests ✓, image booking:3.3.0 published
   canary   5%: ✓
   canary  25%: errors 2.1% vs 0.3% → automatic rollback to the previous version
→ cancelled

Version 3.3.0 passed all the tests: its defect only shows up with real traffic (for example, an uncommon route handled badly). The canary detected it at 25%, and the rollback happened without anyone being woken up. The team will analyse the logs the next day, add a test that reproduces the defect, then ship a 3.3.1.

All traffic goes back to the previous version. Only a share of users saw the defect, for a few minutes.

Step 5 of 5
Text description of the animation
  1. The tests pass: the chain continues. Otherwise, nothing would be built.
  2. The image carries its version number and will never be overwritten: we always know what to go back to.
  3. First stage: the gaps stay within the limits set by the rules.
  4. At the second stage, the error rate exceeds the limit: the defect shows up.
  5. All traffic goes back to the previous version. Only a share of users saw the defect, for a few minutes.

Mini-lab: tune your thresholdsPython 3.13

Output

Click “Run”: Python will load in your browser (about 10 MB the first time).

Your code runs in your browser, isolated from the page. · Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor

Your turn

Worked exercise

Worked exercise: what the canary prevented

The site receives 12,000 requests per hour. Each canary stage lasts 20 minutes before the decision. Version 3.3.0 fails on 2.1% of requests instead of 0.3%. Compute the number of extra failed requests (beyond the usual 0.3%) suffered by users with the canary (stages 5% then 25%, detection at the end of the second), then with an all-at-once deployment at 100%, detected after the same 40 minutes.

Hint 1
Requests served by the new version during a stage: 12,000 × (20 / 60) × share of traffic.
Hint 2
Extra failures: requests served × (2.1 − 0.3) / 100.
Show the solution and common mistakes
per_hour = 12_000
stage_duration = 20 / 60
excess = (2.1 - 0.3) / 100

canary = sum(per_hour * stage_duration * share * excess for share in (0.05, 0.25))
all_at_once = per_hour * (2 * stage_duration) * 1.0 * excess

print(f"with canary: {canary:.0f} extra failed requests")
print(f"all at once: {all_at_once:.0f} extra failed requests")
print(f"the canary prevents {1 - canary / all_at_once:.0%}")
with canary: 22 extra failed requests
all at once: 144 extra failed requests
the canary prevents 85%

With an all-at-once deployment, every traveller suffers the defect for 40 minutes; with the canary, only a small share does, and fewer and fewer as detection gets faster.

Common mistakes:

  • Counting all of the new version's failures instead of the excess: the usual 0.3% would have happened anyway.
  • Forgetting to convert minutes to hours: everything is overestimated by a factor of 60.

Summary

  1. 1A delivery chain starts with written rules: when to block, when to advance, when to roll back.
  2. 2Nothing is built or deployed if the tests fail.
  3. 3Each image has an immutable tag: rolling back names a precise version.
  4. 4The canary compares the new version to the current one, stage by stage, on numerical criteria.
  5. 5A defect detected on a small share of the traffic affects far fewer users than an all-at-once deployment.

Your progress is saved on this device. Create a free account to keep your progress on all your devices.