Show contents
Chapter 4 of 7 · 20 min
Deploying without downtime
By the end of this chapter, you'll be able to
- Number versions with semantic versioning
- Compare rolling, blue-green and canary deployments
- Simulate a rolling deployment that stops on its own when there is a problem
- Evolve a database without breaking the old version
Numbering versions
Semantic versioning (SemVer) numbers a version MAJOR.MINOR.PATCH, for example 2.4.1:
- the patch goes up for a compatible bug fix:
2.4.1→2.4.2; - the minor for a compatible new feature:
2.4.2→2.5.0; - the major for a breaking change (an API that changes shape):
2.5.0→3.0.0.
Every deployed image carries a precise version number, never just latest: you know exactly what is running, and what to go back to.
Three strategies
- rolling update: servers switch to the new version one by one. No downtime, but both versions coexist during the deployment;
- blue-green: a complete environment with the new version (green) is prepared next to the current one (blue), then all traffic is switched at once. Rolling back means switching back: instant, but it takes twice as many machines during the switch;
- canary: first a small share of the traffic (1%, then 10%…) goes to the new version, and its indicators are compared to the old one's before going further. The name comes from the canaries miners took with them to detect gas: if there is a problem, few users suffer from it.
A rolling deployment that watches itself
For each updated server, its health check is verified: an address such as /health that answers if the application works. At the first failure, we stop and roll back:
def deploy(servers, new, healthy):
versions = {s: "2.4.1" for s in servers}
for s in servers:
versions[s] = new
if not healthy(s, new):
print(f"{s}: health check failed, rolling back")
for t in servers:
versions[t] = "2.4.1"
return versions
print(f"{s}: {new} ✓")
return versions
servers = ["web-1", "web-2", "web-3", "web-4"]
print(deploy(servers, "2.5.0", lambda s, v: True))
print(deploy(servers, "2.5.1", lambda s, v: s != "web-3"))
web-1: 2.5.0 ✓
web-2: 2.5.0 ✓
web-3: 2.5.0 ✓
web-4: 2.5.0 ✓
{'web-1': '2.5.0', 'web-2': '2.5.0', 'web-3': '2.5.0', 'web-4': '2.5.0'}
web-1: 2.5.1 ✓
web-2: 2.5.1 ✓
web-3: health check failed, rolling back
{'web-1': '2.4.1', 'web-2': '2.4.1', 'web-3': '2.4.1', 'web-4': '2.4.1'}
The second deployment stops at the third server: web-4 never received the faulty version, and the others went back to 2.4.1. Orchestrators like Kubernetes apply exactly this principle, with probes and a maximum number of servers unavailable at once.
The new version has proven itself on real traffic before being rolled out to everyone.
Text description of the animation
- A single canary server receives a small share of the requests.
- The canary is compared to the old version, on the same indicators.
- The indicators are identical: the rollout widens gradually.
- At each stage, the same check. At the slightest gap, all traffic goes back to 2.4.1.
- The new version has proven itself on real traffic before being rolled out to everyone.
Mini-lab: a canary that decides on its ownPython 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: computing the next version
Write a function next_version(version, change) that takes a SemVer version ("2.4.1") and the type of change ("patch", "minor" or "major"), and returns the next number. Careful: bumping the minor resets the patch to zero, and bumping the major resets the other two.
Hint 1
map(int, version.split(".")) gives the three numbers.Hint 2
"major" case first: it resets everything else.Show the solution and common mistakes
def next_version(version, change):
major, minor, patch = map(int, version.split("."))
if change == "major":
return f"{major + 1}.0.0"
if change == "minor":
return f"{major}.{minor + 1}.0"
if change == "patch":
return f"{major}.{minor}.{patch + 1}"
raise ValueError(f"unknown change: {change}")
for c in ("patch", "minor", "major"):
print(c, "→", next_version("2.4.1", c))
print("2.9.0 + minor →", next_version("2.9.0", "minor"))
patch → 2.4.2
minor → 2.5.0
major → 3.0.0
2.9.0 + minor → 2.10.0
The last line is a reminder that the numbers are integers, not decimals: after 2.9.0 comes 2.10.0, and 2.10.0 is more recent than 2.9.0.
Common mistakes:
- Comparing versions as strings:
"2.10.0" < "2.9.0"isTruein Python, because"1"comes before"9". Compare tuples of integers. - Forgetting to reset the patch when bumping the minor:
2.5.1would suggest a fix of 2.5.0, which never existed.
Summary
- 1SemVer: MAJOR for a breaking change, MINOR for a feature, PATCH for a fix.
- 2Rolling, blue-green, canary: three ways to ship without downtime, with a fast rollback.
- 3A health check verified at each step lets a deployment stop on its own.
- 4A canary compares the new version to the old one on a small share of the traffic before widening.
- 5A database migration must stay compatible with the old version: expand, then contract.
Your progress is saved on this device. Create a free account to keep your progress on all your devices.