Blue/green and canary deployments
Two ways not to change everything at once
Section titled “Two ways not to change everything at once”A rolling deployment replaces instances in place, and the old version is gone from an instance as soon as the new one takes it over. Two other strategies keep the old version around, for different reasons.
Blue/green keeps the whole old fleet. Martin Fowler describes two identical production environments, one live (blue) and one that is being prepared (green). When the software works in green, you switch the router so that all incoming requests go to green. If anything goes wrong you switch the router back to blue.
A canary release keeps the old fleet in service for most of the traffic. Fowler describes it as rolling out a change slowly to a small subset of users to reduce the risk, and if problems are found, rerouting users back to the old version. Google’s Site Reliability Workbook defines canarying as a partial and time-limited deployment of a change in a service and its evaluation, which is done to decide whether to go on with the full rollout.
The two ideas differ in one thing: blue/green moves all the traffic at once, to a version that has been started but has not served anyone, and a canary moves it in steps, so that a few users find a problem before the many do.
Mental model. Rolling puts the new version in place of the old one. Blue/green puts it next to the old one and switches. A canary puts it next to the old one and turns it up slowly. The more of the old version you keep, and the longer you keep it, the cheaper going back is, and the more the extra instances cost.
Blue/green: a second fleet and a switch
Section titled “Blue/green: a second fleet and a switch”The lab starts a second fleet of 10 instances next to the first. While it starts, the old fleet serves everything. When the new fleet is ready, a routing change moves all the traffic to it, and the old fleet is kept, idle, for the time set by Keep the old fleet for, 15 min (900 s) by default.
Choose Blue/green under Strategy shown, with the lab as it opens (a bad release: it fails 8% of requests more than the old version). Lowest serving capacity is 100%, because the old fleet carries the traffic until the new one is ready, and Requests refused is 0. Extra instances at the peak is 10, and the detail line says 38.7 extra instance-minutes.
A fleet of identical instances serves the traffic. A new version, worse than the old one, has to replace it. Choose how: all at once, rolling, blue/green or canary. All four run on the same traffic, with the same alert, so the table shows what each costs in users hit, in time to notice, in time to undo, in capacity and in money.
- Old version (v1), serving
- Old version (v1), kept idle
- New version (v2), serving
- Starting, not serving yet
At 0:52: 1 instances serve the new version, 10 the old one and 0 are starting. 5% of the traffic goes to the new version. The fleet can serve 1,100 requests a second, and 623 arrived this second. The alert has fired.
The deployment run
- Users hit by the release
- 0.008%expected share with a bad request caused by the release
- Time to detect
- 2 sfrom the first request to the new version to the alert
- Time to roll back
- 2 sfrom the alert; a canary rolls back by itself
- Lowest serving capacity
- 100%Busythen the traffic is 60% of what is left
- Requests refused
- 00% of all requests, for lack of capacity
- Extra instances at the peak
- 10.9 extra instance-minutes in all
- Rollout length
- Rolled backuntil the new version has all the traffic
Canary: the alert fired at 0:52, 2 s after the first request reached the new version. The old version served everything again at 0:54, 2 s after the alert. From the first request to the new version until the old version served again took 4 s, and 0.008% of the users were hit.
Traffic on the new version, and capacity
- Traffic on the new version
- Serving capacity
Users hit so far
- Users hit
Instances paid for
- Instances paid for
- Fleet size
Drag on a chart, or use the moment slider, to move through the run.
| Strategy | Result | Users hit | Time to detect | Time to roll back | Lowest capacity | Refused | Extra instances · minutes |
|---|---|---|---|---|---|---|---|
| All at once | Rolled back | 7.4% | <1 s | 3.9 min | 0% | 59,638 | 0 · 0 |
| Rolling | Rolled back | 11.4% | 50 s | 7.2 min | 80% | 0 | 0 · 0 |
| Blue/green | Rolled back | 7.4% | <1 s | 3 min | 100% | 0 | 10 · 38.7 |
| Canary · shown above | Rolled back | 0.008% | 2 s | 2 s | 100% | 0 | 1 · 0.9 |
Each row is one run of 14 minutes of simulated time, on the same traffic sample. Choose another sample to see how much a single run can differ.
How long a canary needs to be sure
A release that is bad for 8.5% of requests, against 0.5% for the old version, takes about 20 requests to tell apart, with a 5% risk of a false alarm and an 80% chance of seeing the regression. The alert in this lab fires once 100 requests have been seen; the table counts the 100 requests that a careful judgement needs.
| Canary share | 60 req/s in all | 600 req/s in all | 6,000 req/s in all |
|---|---|---|---|
| 1% | 2.8 min | 17 s | 2 s |
| 5% | 33 s | 3 s | <1 s |
| 10% | 17 s | 2 s | <1 s |
| 25% | 7 s | <1 s | <1 s |
| 50% | 3 s | <1 s | <1 s |
A simple model: independent requests, a known normal rate, one fixed rate for the new version. It gives an order of magnitude. A smaller regression, or a noisier measure, needs many more requests.
The run as text and as a table
What happened with Canary
- At 0:00, the rollout starts.
- At 0:50, the canary reaches step 1: 5% of the traffic.
- At 0:50, the first request reaches the new version.
- At 0:52, the alert fires.
- At 0:53, the rollback starts.
- At 0:54, the old version serves all the traffic again.
| Elapsed | Old (v1) | New (v2) | Starting | Traffic to v2 | Capacity (req/s) | Refused | Failed so far |
|---|---|---|---|---|---|---|---|
| 0:00 | 10 | 0 | 1 | 0% | 1,000 | 0 | 3 |
| 1:10 | 10 | 0 | 0 | 0% | 1,000 | 0 | 205 |
| 2:20 | 10 | 0 | 0 | 0% | 1,000 | 0 | 408 |
| 3:30 | 10 | 0 | 0 | 0% | 1,000 | 0 | 626 |
| 4:40 | 10 | 0 | 0 | 0% | 1,000 | 0 | 849 |
| 5:50 | 10 | 0 | 0 | 0% | 1,000 | 0 | 1,057 |
| 7:00 | 10 | 0 | 0 | 0% | 1,000 | 0 | 1,252 |
| 8:09 | 10 | 0 | 0 | 0% | 1,000 | 0 | 1,471 |
| 9:19 | 10 | 0 | 0 | 0% | 1,000 | 0 | 1,681 |
| 10:29 | 10 | 0 | 0 | 0% | 1,000 | 0 | 1,874 |
| 11:39 | 10 | 0 | 0 | 0% | 1,000 | 0 | 2,091 |
| 12:49 | 10 | 0 | 0 | 0% | 1,000 | 0 | 2,303 |
| 13:59 | 10 | 0 | 0 | 0% | 1,000 | 0 | 2,501 |
Illustrative values, not benchmarks of any real product.
Model notes
Assumptions
- Requests arrive at random at a steady average rate: each second brings a random number of them (a Poisson count). All four strategies get the identical arrivals, and the identical random numbers for routing and for failures, so a difference between two rows comes from the strategy and not from the luck of the draw. Choose another traffic sample to see how much one draw can differ from another.
- Every instance serves up to 100 requests a second (2 at a time, 20 ms each). Requests beyond what the instances that are ready can serve are refused. An instance serves nothing while it starts and while its readiness check is passing, and it is paid for all that time.
- A request is bad when it fails or takes longer than the latency objective. With the old version, 0.5% of requests are bad. The new version adds the share of failures and of slow requests that you set. Each request is bad or not independently of every other.
- The alert counts the requests since the routing last changed, and fires when at least 100 have been seen and the share of bad ones reaches the threshold. A canary watches only the requests that reach the new version. The other strategies watch the whole service, as an alert on the service’s error rate does, so a bad version that serves a small share of the traffic is diluted. Requests refused for lack of capacity are not counted by the alert.
- Only the canary rolls back by itself. For the other strategies a person acts after the time you set. A rollback that is only a change of routing (blue/green before the old fleet is removed, a canary before its last step) takes about two seconds: the alert is checked every second, and the change then takes a second to reach every request. Any other rollback starts the old version again, a batch at a time for a rolling deployment and all at once for the others, and takes as long as a start.
- A user’s visit is 5 requests, all to the same version. The share of users hit is the expected share of visits, over the whole run, that go to the new version and have at least one request that is bad because of the release. It is an expected value from the model, not a count. Users are not followed from one visit to the next.
Simplifications
- A canary step ends when its minimum time has passed and the new version has served 100 requests during the step; the instances for the next step are assumed to be started by then. A canary’s share is a routing weight, and the capacity of the old and the new instances is pooled.
- The table of times to be sure uses the sample size of a one-sided test of a proportion, with a 5% risk of a false alarm and an 80% chance of seeing a real regression, by the normal approximation. It assumes independent requests, a known normal rate and a regression that is the same for every request. It gives the order of magnitude, not a promise: real regressions depend on the request, the user and the time of day, and a rate of one in a thousand needs far more requests than a rate of one in ten.
- The alert looks at one measure, the share of bad requests, against a fixed threshold. Real canary analysis compares several measures (latency percentiles, saturation, business figures) and often compares the canary with a control group of instances of the same age rather than with a fixed rate. Checking again every second against a fixed threshold also raises the chance of a false alarm, which the run shows when the release is good.
- Databases, caches that need to warm up, long connections and sessions that move from one version to another are not modelled. A rollback is assumed to be safe, which it is not when the new version has changed data in a way the old one cannot read: that is why schema changes are done separately, in steps (expand and contract).
- Feature flags are not modelled here. A flag turns code that is already deployed on for a share of users and can turn it off in seconds without moving any instance: the same arithmetic as a canary, with a rollback that costs no start-up time.
- A container image pull, a readiness probe and the load balancer’s connection draining are reduced to one start time and one readiness time. Autoscaling during the rollout is not modelled.
Parameter values are illustrative. They are not benchmarks of any real product.
The price of keeping the old fleet shows best with a good release. Set Extra failures to 0%: the run then ends without a rollback, and the cost line for blue/green reads 158.3 extra instance-minutes. That is 10 × (50 + 900) / 60: 10 more instances, for the 50 s the new fleet takes to start and the 900 s that the old one is kept. Put Extra failures back to 8% afterwards.
Under the default settings, blue/green rolls back in Time to roll back of 3 min, and Users hit by the release is 7.4%, the same as all at once. The switch back takes about 2 s, so the 3 min are almost entirely the time for a person to react, which A person reacts after sets at 180 s. A fast rollback mechanism does nothing for the users if nobody uses it for three minutes. Set A person reacts after to “at once” and blue/green rolls back in 2 s. In the same run, all at once takes 51 s, since going back means starting the old version again, and it refuses 59,765 requests, or 16.64%, while it does. The next lesson looks at this delay more closely.
What if the old fleet is already gone? Set Keep the old fleet for to 60 s, and put A person reacts after back to 180 s. The old fleet is shut down at 1:50, long before anyone acts at 3:51, so going back means starting it again. Time to roll back is now 3.9 min, Users hit by the release is 9.4%, and the extra cost falls to 26.7 instance-minutes. The money saved on the idle fleet is paid for in users. Fowler also notes the complication of a database in this pattern: a schema change complicates the switch, and his answer is to change the database in a way that supports both versions before the upgrade, which is the subject of the last lesson of this module.
Canary: a small share first
Section titled “Canary: a small share first”A canary uses the old fleet differently. It does not wait to be ready and switch: it starts one instance of the new version, sends it a small share of the requests, and decides from what happens to that share. The lab’s canary starts at First step of 5% of the traffic and goes through Steps of 4, with at least Least time per step of 5 min at each, so the line under the controls reads: Steps: 5% → 37% → 68% → 100% of the traffic. A step is finished when its time has passed and the new version has served enough requests for the alert to judge it. The alert watches only the requests that reach the new version, and rolls the release back by itself.
The caution costs time. With Extra failures at 0% so that nothing is rolled back, the canary’s Rollout length is 15.8 min, against 50 s for blue/green and 4.2 min for rolling. It needs only 1 extra instance at the peak and 15.8 extra instance-minutes, because only a small part of the fleet is ever duplicated. Put Extra failures back to 8%.
With the bad release, choose Canary. The alert fires at 0:52, 2 s after the first request reached the new version, and the old version serves everything again at 0:54. Time to detect is 2 s, Time to roll back is 2 s, and Users hit by the release is 0.008%. For all at once the figure is 7.4%, blue/green 7.4% and rolling 11.4%.
| Strategy | Users hit | Time to roll back | Lowest serving capacity | Extra instances at the peak |
|---|---|---|---|---|
| All at once | 7.4% | 3.9 min | 0% | 0 |
| Rolling | 11.4% | 7.2 min | 80% | 0 |
| Blue/green | 7.4% | 3 min | 100% | 10 |
| Canary | 0.008% | 2 s | 100% | 1 |
Why the canary was caught in 2 seconds
Section titled “Why the canary was caught in 2 seconds”Do not read 0.008% as what a canary does. It is what a canary does to this release, on this traffic, and the lab shows why. Start from the How long a canary needs to be sure panel. With the settings as they open it says that a release that is bad for 8.5% of requests, against 0.5% for the old version, takes about 20 requests to tell apart, with a 5% risk of a false alarm and an 80% chance of seeing the regression. The alert in this lab fires once 100 requests have been seen; the table counts the 100 requests that a careful judgement needs.
Three things made 2 s enough:
- The failure is large. A release that fails 8.5% of its requests, against 0.5% for the old one, is a gross bug. Twenty requests are enough to see it.
- The traffic is high. 5% of 600 requests a second is 30 requests a second to the canary, so 100 requests take about 3 s. The panel’s table gives the same figure for a 5% canary at 600 requests a second: 3 s.
- The alert is instant. In the lab there is no delay between a request and the alert. The alert is checked every second, from exact counts, and the canary rolls back by itself. A real monitoring pipeline aggregates its metrics over an interval, and the Site Reliability Workbook advises keeping the interval of your metrics the same as, or shorter than, the canary’s duration.
The same panel shows what happens when those change. Its rows are the canary’s share, and its columns three levels of traffic. For a 1% canary the time to collect the requests is 2.8 min at 60 requests a second, 17 s at 600 and 2 s at 6,000.
What happens if the canary sees too little
Section titled “What happens if the canary sees too little”Change Traffic to 100 requests a second. The canary now gets 5 requests a second. Time to detect is 19 s and Users hit by the release is 0.044%. Set Traffic to 20 and the canary gets one request a second: Time to detect is 114 s and Users hit by the release is 0.2%. It is still far better than 7.5% for all at once in the same run, but about 25 times the 0.008% of the first run, and the cause is only that the canary’s evidence arrived more slowly. Set Traffic back to 600.
A smaller regression has the same effect, and it can be worse: with Extra failures at 1%, the release fails 1.5% of requests, below the alert’s threshold of 3%, and no strategy catches it. The next lesson works through that. For now the lesson is that a canary is only as good as the evidence it collects: the share it gets, the traffic behind that share, and the alert that reads it. The Workbook adds two more conditions. A canary population has to be large enough to represent the kinds of request the service sees. And the canary should be compared with a control population, not with the same service before the change, because time is one of the biggest sources of change in observed metrics.
- Blue/green runs two fleets and switches all the traffic at once; while the old fleet is kept, going back is a routing change of about 2 s. It costs 10 extra instances at the peak, and 158.3 extra instance-minutes for a good release when the old fleet is kept for 15 min.
- A person who takes 3 min to react makes blue/green’s quick switch irrelevant: users hit are 7.4%, as for all at once. With a reaction time of zero, it rolls back in 2 s.
- After the old fleet is retired, a rollback means starting it again: 3.9 min and 9.4% of the users hit with the old fleet kept for only 60 s.
- A canary sends 5% of the traffic first. In the default run it is caught after 2 s and hits 0.008% of the users, against 7.4% for all at once and 11.4% for rolling, and its price is a rollout of 15.8 min.
- The 2 s depends on a gross failure, 30 requests a second to the canary, and an instant alert. At 100 requests a second in all the canary is caught after 19 s (0.044%), and at 20 after 114 s (0.2%).
Your progress
Built by GhaziAlibi.
© ShardCraft. All rights reserved.