Skip to content

Rolling deployments and zero downtime

The simplest deployment, and what it costs

Section titled “The simplest deployment, and what it costs”

You have the new image. The most direct way to put it in service is to stop every instance of the old version and start instances of the new one. Kubernetes calls this the Recreate strategy: all existing pods are killed before new ones are created. The lab calls it All at once. It is simple, there is only ever one version, and for as long as the new instances are starting there is nothing to answer a request.

The previous lesson put a number on that time. An instance takes 30 s to start and 20 s to pass its readiness check, so 50 s pass before the first one can serve. The alternative is a rolling deployment: replace the instances a few at a time, so that the others keep serving while a batch is starting. Kubernetes’ RollingUpdate, its default, does this by gradually scaling up a new ReplicaSet while scaling down the old one.

Mental model. A rolling deployment trades time for capacity. It takes longer, because each batch has to start and be ready before the next, and in exchange the service never has all its instances out at once. Whether that is enough depends on what is left.

The lab runs 10 instances that can serve 100 requests a second each, so 1,000 in all, against 600 requests a second of traffic. It opens with a bad release so that you can see what a rollback costs in the next lessons. Here you want to see only the rollout, so set Extra failures to 0%. Then the new version is as good as the old one, nothing needs undoing, and every strategy rolls out completely.

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.

Strategy shown

Send a small share of the traffic to the new version, watch it, and raise the share step by step. A bad version reaches few users and is rolled back by itself.

Fleet and traffic
600 req/s
Instances
10
30 s
20 s
The release
8%
0%

The new version is worse than the old one. Set both to 0% to deploy a release that is as good as the old one and watch for false alarms.

The alert
3%
3 min

A bad request fails or is slower than the latency objective; the old version has 0.5% of them. The alert needs at least 100 requests before it trusts a rate.

Rolling
2
Blue/green
15 min
Canary
5%
4
5 min

Steps: 5% → 37% → 68% → 100% of the traffic.

v1v1v1v1v1v1v1v1v1v1v2
  • 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.

0:52
0:52

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

0%75%150%0:003:307:0010:2913:59Now
  • Traffic on the new version
  • Serving capacity

Users hit so far

0%0.075%0.15%0:003:307:0010:2913:59Now
  • Users hit

Instances paid for

08150:003:307:0010:2913:59Now
  • Instances paid for
  • Fleet size

Drag on a chart, or use the moment slider, to move through the run.

The four strategies on the same traffic and the same release
StrategyResultUsers hitTime to detectTime to roll backLowest capacityRefusedExtra instances · minutes
All at onceRolled back7.4%<1 s3.9 min0%59,6380 · 0
RollingRolled back11.4%50 s7.2 min80%00 · 0
Blue/greenRolled back7.4%<1 s3 min100%010 · 38.7
Canary · shown aboveRolled back0.008%2 s2 s100%01 · 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.

Time at each canary share and traffic level to collect those requests
Canary share60 req/s in all600 req/s in all6,000 req/s in all
1%2.8 min17 s2 s
5%33 s3 s<1 s
10%17 s2 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
  1. At 0:00, the rollout starts.
  2. At 0:50, the canary reaches step 1: 5% of the traffic.
  3. At 0:50, the first request reaches the new version.
  4. At 0:52, the alert fires.
  5. At 0:53, the rollback starts.
  6. At 0:54, the old version serves all the traffic again.
The run at 13 moments
ElapsedOld (v1)New (v2)StartingTraffic to v2Capacity (req/s)RefusedFailed so far
0:0010010%1,00003
1:1010000%1,0000205
2:2010000%1,0000408
3:3010000%1,0000626
4:4010000%1,0000849
5:5010000%1,00001,057
7:0010000%1,00001,252
8:0910000%1,00001,471
9:1910000%1,00001,681
10:2910000%1,00001,874
11:3910000%1,00002,091
12:4910000%1,00002,303
13:5910000%1,00002,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.

Choose All at once under Strategy shown. Lowest serving capacity is 0%: for 50 s no instance can serve, and Requests refused is 29,820, which the detail line gives as 3.94% of all requests in the run. It is what you would expect from 600 × 50, which is 30,000 requests, with the usual random variation in the number that arrive. Rollout length is 50 s.

Now choose Rolling. The default batch is 2 instances at a time, taken out first. Lowest serving capacity is 80%, because 8 of 10 instances are serving while a batch of 2 starts, and the detail line says the traffic is then 75% of what is left, which is 600 / 800. Requests refused is 0. Rollout length is 4.2 min: 5 batches of 2, each taking the 50 s that a start takes. No request was refused, and the price was 200 s more than the 50 s of the all-at-once run.

A rolling deployment has to decide where the batch comes from. Kubernetes gives it two settings, and the lab has one choice with two options.

maxUnavailable is the number of pods that may be unavailable during the update, as a number or a percentage of the desired pods. Kubernetes calculates it from a percentage by rounding down, and its default is 25%. With maxUnavailable at 30%, the old ReplicaSet can be scaled down to 70% of the desired pods when the update starts, so at least 70% are available at all times. This is the lab’s Take old ones out first (a smaller fleet), and the batch is Instances at a time.

maxSurge is the number of pods that may be created above the desired number. Kubernetes calculates it from a percentage by rounding up, and its default is also 25%. With maxSurge at 30%, the new ReplicaSet can be scaled up at once, so long as the total of old and new pods is at most 130% of the desired number. This is the lab’s Start new ones first (extra instances). The two cannot both be 0: Kubernetes does not accept it, because the update could not move.

For a fleet of 10 and Kubernetes’ defaults, that is 10 × 25%, which is 2.5. It rounds down to 2 for maxUnavailable and up to 3 for maxSurge, so at least 8 pods are available and at most 13 exist at any time. The lab does one or the other, not both, so that the effect of each can be seen.

Change Capacity while replacing to Start new ones first (extra instances). Now Lowest serving capacity is 100%, since the old instances are only removed when the new ones are ready, and Requests refused stays 0. Extra instances at the peak is 2, with 8.3 extra instance-minutes in all, and Rollout length is still 4.2 min. The extra instances are not free. The 8.3 is 10 instances × 50 s: the lab pays for 2 instances more for 250 s, and that is what the capacity costs. Taking old ones out first costs nothing extra and leaves the fleet smaller while it works.

What happens if you replace half the fleet at once

Section titled “What happens if you replace half the fleet at once”

Set Capacity while replacing back to Take old ones out first (a smaller fleet) and set Instances at a time to 5. The rollout is twice as fast: Rollout length is 100 s. But Lowest serving capacity is 50%, and the detail line says the traffic is then 120% of what is left: 600 requests a second against 500. Requests refused is 9,813, which is 1.3% of all requests.

The same batch with Start new ones first (extra instances) loses nothing: capacity stays at 100% and no request is refused. Extra instances at the peak is 5, and the extra cost is again 8.3 instance-minutes, because the same 10 instances were started once each. A bigger batch buys speed with either capacity you give up or instances you pay for at the same time.

A rolling deployment is not a guarantee of zero downtime. It is a way of keeping some capacity, and downtime is avoided only when the capacity that is left is more than the traffic. Set Traffic to 900 and the batch back to 2. With the old ones taken out first, Lowest serving capacity is still 80%, which is 800 requests a second, so the traffic is 113% of what is left. Requests refused is 24,702 (2.18% of all requests) for the rolling run, against 44,780 (3.95%) for all at once. With Start new ones first (extra instances), Requests refused returns to 0.

So “zero downtime” has two parts: a strategy that never takes every instance out, and headroom to carry the traffic with the capacity that remains. The earlier lesson on autoscaling discussed the same margin. Choosing surge buys the headroom with money for the length of the rollout, and choosing a small batch buys it with time.

Two other things have to be right, and the lab does not model them. A new instance must not receive traffic before it can serve it, which is the job of the readiness probe: Kubernetes says a failing readiness probe removes the pod from the services that route to it, and a rollout that waits for readiness is why the 20 s of the readiness check are in the lab. And an old instance must not be cut while it is answering: the connection draining lesson counted what stopping a server at once costs, and the lab removes an instance without that cost.

In the good-release run with Rolling selected, open the text version of the run under The run as text and as a table. At 0:50 the first request reaches the new version, and at 4:10 the new version has all the traffic. For those 200 s, which is 3.3 min, both versions serve requests. Every user can reach either one, and so can any request that a user’s browser sends a moment later. A new version that writes data in a format the old version cannot read, or expects a column the database does not have yet, is a problem in exactly that window. Martin Fowler describes the parallel change pattern as one that allows code to be released in any of its phases, so that old and new can coexist. The last lesson of this module is about changing a database that way, so that both versions work.

All at once has no such window, and that is its one real advantage. It pays for it with the outage you just measured.

  • All at once stops the service while the new instances start. In the lab that is 0% capacity for 50 s and 29,820 refused requests, which is 3.94%.
  • Rolling replaces a batch at a time. With a batch of 2 taken out first, the lowest capacity is 80% and nothing is refused, and the rollout takes 4.2 min because each of 5 batches takes 50 s.
  • Kubernetes’ maxUnavailable rounds down and maxSurge rounds up; their defaults are 25%, which for 10 pods is 2 and 3. Starting new ones first keeps capacity at 100% and costs 8.3 extra instance-minutes here.
  • A bigger batch is faster and takes more capacity or more instances: a batch of 5 leaves 50% and refuses 9,813 requests when old ones are taken out first.
  • Zero downtime needs headroom: at 900 requests a second the rolling run refuses 24,702 requests, and surge refuses none.
  • While a rollout runs, both versions serve, here from 0:50 to 4:10, so they must work together.

Next: blue/green and canary deployments, which keep the old fleet or the traffic apart.

Your progress

Built by GhaziAlibi.

© ShardCraft. All rights reserved.