Zero-downtime schema changes: expand and contract
The one thing a deployment cannot swap
Section titled “The one thing a deployment cannot swap”A deployment replaces code, and code is an image that can be started and stopped. The database is not replaced: there is one, and every version of the code in service talks to it. So during a rollout, with old and new instances serving side by side, the schema has to suit both.
The lab shows how long that lasts. Set Extra failures to 0% so the release is good, choose Rolling and open the run as text: the first request reaches the new version at 0:50, and the new version has all the traffic at 4:10. For 200 s, which is 3.3 min, both versions are in service. Choose Canary and the same events read 0:50 and 15:50, so 900 s, which is 15 min.
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.
Take a change that looks harmless: rename the column phone to phone_number. If you run the rename first and then deploy, every old instance that is still running asks for a column that is gone, and fails until it is replaced. If you deploy first and then rename, every new instance asks for a column that does not exist yet. There is no order of the two that works, because there is a window in which both versions run. And if the new version has to be rolled back, the old version meets whatever the new version left in the database. Blue/green does not avoid this. Fowler’s page on it points to the same difficulty with schema changes.
Expand and contract
Section titled “Expand and contract”The way out is to never make a change that only one version understands. Martin Fowler calls the pattern parallel change, and the name that is more used is expand and contract. It has three phases:
- Expand. Augment the interface to support both the old and the new version, without removing anything that exists.
- Migrate. Update every client of the old version to the new one, step by step.
- Contract. Remove the old version, so that only the new one is supported.
Fowler notes that the pattern allows code to be released in any of its phases, and that it applies to a database schema as well as to a method signature. In his description of evolutionary database design, a transition phase is a period when the database supports both the old and the new access patterns at once, and his preference is to write migrations so that the application’s database access works with both the old and the new version of the database. The Prisma Data Guide describes the same pattern for moving data to a new structure without affecting uptime.
A worked example: renaming a column
Section titled “A worked example: renaming a column”Each row below is one deployment or one run of a migration. The strategy used to deploy the code, rolling or blue/green or canary, can be any of them, because every row works for the two versions that can serve together.
| Step | Phase | What changes | What the code does | Can you go back one step? |
|---|---|---|---|---|
| 1 | Expand | Add a nullable column phone_number. |
Nothing yet. The code in service ignores it. | Yes. Drop the column. |
| 2 | Expand | Deploy version 2. | It writes phone and phone_number, and reads phone. Version 1 may still serve, and writes only phone. |
Yes. Version 1 works with both columns. |
| 3 | Migrate | Copy phone into phone_number for the rows where it is empty, in small batches, after version 1 has gone. |
Unchanged. | Yes. phone is complete. |
| 4 | Migrate | Deploy version 3. | It reads phone_number and still writes both. |
Yes. Version 2 reads phone, which is still written. |
| 5 | Contract | Deploy version 4. | It stops writing phone. |
Only to version 3. Rolling back to version 2 would read a phone that is no longer updated. |
| 6 | Contract | Drop phone, when no running version reads or writes it. |
Unchanged. | No. The data in the column is gone, and only a restore brings it back. |
Read down the last column. Until step 5 every version of the code works against every version of the schema it can meet, so any deployment can be rolled back, by any of the strategies, without data loss. The Prisma Data Guide’s worked example has the same shape: the client writes to both structures while only reading the old one, and abandoning the new structure and reverting the client stays possible until writes to the old one stop. From step 5 the old column stops being a copy, and from step 6 it is not there at all. The contract steps are the ones that cannot be undone, and so they are done last, after the new version has run long enough that you will not need to go back.
The price is plain. A rename that was one statement becomes six steps over several releases, two columns hold the same fact for a time and must be kept equal, which is the cost of a copy described in the normalisation lesson, and the migration step reads and writes every row, which competes with live traffic. Adding an index in the expand phase has the cost described in the indexing lesson: every write pays to keep it up to date. How a given statement locks a table or rewrites it depends on the database and the statement, and has to be read in its documentation.
What happens if you skip a step
Section titled “What happens if you skip a step”Suppose a team runs steps 3 and 6 together to save time: the backfill, then at once the drop. A rolling deployment of version 3 is still in progress, and for its 3.3 min some instances are version 2, which reads phone. The drop removes the column under them and they fail. If the team rolls back version 3 afterwards, there is nothing for version 2 to read. The sequence is slow on purpose: each step is chosen so that the version before it and the version after it both work, and the last step waits until no one needs the old column.
Fowler adds the discipline: the supplier has to maintain two versions for a while, and the contract phase has to be finished. A schema that is left half-way, with two columns and no plan to remove one, is a copy that someone will one day update in only one place.
Check your understanding
Section titled “Check your understanding”Check your understanding: schema changes
This quiz needs JavaScript to check your answers.
- A deployment runs two versions of the code at once, and the database is shared. In the lab a rolling deployment has both in service for 200 s (3.3 min) and a canary for 900 s (15 min).
- No order of “change the schema” and “deploy the code” works for a rename, because there is a window with both versions.
- Expand and contract makes every step safe for the two versions that can be in service: expand (add the new next to the old), migrate (move writers, then data, then readers), contract (remove the old).
- Everything before the last two steps can be rolled back; stopping writes to the old column limits how far, and dropping it cannot be undone.
- It costs more steps, two copies of a fact for a time, and discipline to finish.
Your progress
Built by GhaziAlibi.
© ShardCraft. All rights reserved.