v1.6.0 is released to production. For one whole minute, everyone opening notes-api gets 502 Bad Gateway (nginx's error when the app behind it doesn't answer). Once the new version is up, attachment uploads fail. Going back to the old version means another minute with no app answering.
That's just one way to swap versions. A deployment strategy is how you replace the old version with the new one on servers that are busy serving users. Picture a restaurant changing its menu. It can close for a moment and reopen with the new menu (recreate). It can switch the menu table by table (rolling). It can set up a second, identical kitchen, move every customer over at once, and keep the old kitchen ready in case something goes wrong (blue-green). Or it can serve the new menu to 1 in 20 customers first, watch how they react, and only then to everyone (canary, after the canary birds miners once carried to detect poisonous gas). Here the menu is the app's version, one table is one container, the second kitchen is another set of containers, and the customers are the requests from people using the app.
The same stage for all four strategies
notes-api now runs as 4 containers behind an nginx load balancer. The load balancer spreads the requests over those containers (nginx is covered in the Nginx Basics lesson). The traffic (the number of requests coming in) is 1,000 requests a minute. The running version is v1.5.0, and we want to deploy v1.6.0. Sadly v1.6.0 has a bug: uploading an attachment fails with a 500 error. Attachment uploads are 20% of all requests, so 20% of the requests to v1.6.0 fail. The Lab uses exactly these numbers, and its clock moves 1 minute at each step.
Three measures are compared: downtime (minutes with no server answering), how many users hit the broken version, and how long a rollback takes, going back to the old version.
Recreate: stop everything, start the new one
Every v1.5.0 container is stopped, then the v1.6.0 containers are started. It's much like what happens when docker compose up -d replaces the old container with a container from a new image.
- Minute 1: no container is ready. All 1,000 requests get
502 Bad Gatewayfrom nginx. That's downtime. - Minute 2: 100% of traffic goes to v1.6.0.
- The rollback is a recreate too: another minute of downtime, then v1.5.0 is back.
The simplest option, and it needs no extra servers. Good for staging (a test server before production), or for an app that may go down for a moment at a quiet hour.
Rolling update: replace them one by one
Containers are replaced in turn, one a minute, so the traffic to v1.6.0 goes up step by step: 25%, 50%, 75%, then 100%. During the process, one extra container is started before an old one is stopped, so capacity (the number of containers serving) never drops.
- Zero downtime (no downtime): there's always a container answering.
- During the rollout (the process of swapping versions), v1.5.0 and v1.6.0 run side by side and use the same database.
- The rollback goes one by one too, so it's as slow as the rollout.
For most apps, rolling is a sensible standard choice.
Blue-green: two environments, one switch
Blue is the 4 v1.5.0 containers serving now. Green is 4 v1.6.0 containers started next to them, with no traffic. Blue and green are called two environments. Once green is ready and passes a smoke test (a quick check that the app is alive), the load balancer is switched, and 100% of traffic goes to green at once, like flipping a switch.
- The rollback is instant: flip the switch back to blue, within seconds.
- The cost: 8 containers during the deploy, double.
- Everyone moves at the same time, so a bug in green still reaches everyone until the switch is flipped back.
Canary: a small slice first
At first, v1.6.0 (the canary) gets only 5% of traffic. For one minute, the canary's error rate (the percentage of requests that fail) is compared with a limit, for example 1%. If the error rate is under the limit, the traffic to the canary goes up to 25%, 50%, then 100%. If it goes over the limit, the rollout is cancelled automatically and all traffic goes back to v1.5.0.
Now try it with the broken v1.6.0. Before you continue, guess: if the rollout is cancelled after the first minute, how many of the 1,000 requests reached the broken version?
Minute 1 sends 5% × 1,000 = 50 requests to the broken version, and 20% of those, 10 requests, fail. The canary's 20% error rate is far above the 1% limit, so by minute 2 the canary's traffic is already 0%.
To send 5%, the nginx config uses weights (weight): the 4 stable containers get weight=19 each (76 in total), and the 1 canary container gets weight=4. So 4 out of 80 = 5% of requests go to the canary.
A comparison with the Lab's numbers
In this table, rollback is pressed at the end of minute 2, and the canary is cancelled automatically after minute 1:
| Strategy | Downtime | Requests to v1.6.0 | Failed requests | Rollback | Most containers |
|---|---|---|---|---|---|
| Recreate | 2 minutes | 1,000 | 2,200 | 2 minutes, with downtime | 4 |
| Rolling | 0 | 1,000 | 200 | 2 minutes, step by step | 5 |
| Blue-green | 0 | 1,000 | 200 | within seconds | 8 |
| Canary | 0 | 50 | 10 | automatic, within seconds | 5 |
For example: recreate's 2,200 failed requests = 1,000 (downtime in minute 1) + 200 (20% of the 1,000 requests in minute 2) + 1,000 (downtime during the rollback).
Picking a strategy
In the exercise, the needs are checked in priority order, and the first need that is true decides the result:
- needs a gradual rollout (
gradualRollout): canary - needs an instant rollback (
instantRollback): blue-green - needs no downtime (
zeroDowntime): rolling - puts simplicity first (
simplicity): recreate - anything else: rolling
Why is canary at the top? A canary also has no downtime and a quick rollback. So if a gradual rollout is needed, a canary covers those two needs as well.
⚠️ Right on paper, messy in production
- A migration that breaks the old version. With rolling, blue-green and canary, v1.5.0 and v1.6.0 use the same database at the same time. If a migration (a change to the tables) drops or renames a column v1.5.0 still reads, the old version fails. Add the new column first, and remove the old one in a later release.
- A canary with no monitoring. If nobody watches its error rate, a canary is just a slow rolling update. Set a limit and an automatic cancel.
- Forgetting the cost of blue-green. Blue-green needs double capacity during the deploy, and blue has to stay ready until you're sure green is healthy.
- The wrong priority order in the exercise.
{ instantRollback: true, gradualRollout: true }gives canary, because gradual is checked first.
TL;DR: Recreate stops every old container and then starts the new ones: simple, but with downtime (time with no server answering), the rollback (going back to the old version) included. Rolling replaces containers one by one: no downtime, old and new run side by side, and the rollback is just as slow. Blue-green sets up a second environment (another set of containers) and moves all traffic (the requests coming in) at once: an instant rollback, but double the capacity (the number of containers). Canary sends a small slice of traffic (for example 5%) to the new version. It raises that slice step by step as long as the error rate (the percentage of requests that fail) stays under a limit, so a bug only reaches a few users. The exercise's order: gradual rollout (canary), instant rollback (blue-green), no downtime (rolling), simplicity (recreate), otherwise rolling.
⚡ Recreate: stop everything, start the new one
Meaning: Every container of the old version (v1.5.0) is stopped, then the containers of the new version (v1.6.0) are started. For one minute no container is ready, so the 1,000 requests in that minute get the error 502 Bad Gateway. That's downtime (time with no server answering). A rollback (going back to the old version) works the same way, so there's downtime again. Recreate is the simplest, good for staging (a test server).
⚡ Rolling: replace them one by one
Meaning: Containers are replaced in turn, one a minute, so the traffic (the requests coming in) to v1.6.0 goes up step by step: 25%, 50%, 75%, then 100%. There's always a container answering, so there's no downtime. But during the process, v1.5.0 and v1.6.0 run side by side and use the same database. The rollback goes one by one too, so it's just as slow.
⚡ Blue-green: one switch
Meaning: Blue is the 4 containers of the old version (v1.5.0) serving now. Green (4 v1.6.0 containers) is set up next to them, then the load balancer (which spreads requests over the containers) is switched to green all at once, like flipping a switch. A rollback is just flipping that switch back to blue, within seconds. But during the deploy you need 8 containers, double.
⚡ Canary: a small slice first
Guess first: v1.6.0 has a bug that makes 20% of requests fail. It gets only 5% of traffic and is cancelled after the first minute. Of 1,000 requests, how many reach that broken version?
Only 50 (5% × 1,000), and 10 of those fail. The canary's error rate (the percentage of requests that fail) is 20%, far above the 1% limit, so the rollout is cancelled automatically.
Meaning: A canary sends a small slice of traffic to the new version first, then raises it step by step (5%, 25%, 50%, 100%) as long as the error rate stays under the limit. So a bug only reaches a few users.