Rollback, Roll-forward & Database Changes
Every deployment strategy promises a way back. Whether that promise holds depends on one thing deployment tools can't handle for you: state. Code can be swapped in seconds; a database column that was dropped can't.
Roll back or roll forward?
| Roll back | Roll forward | |
|---|---|---|
| What | redeploy (or switch to) the previous known-good version | ship a new version with the fix |
| Speed | seconds (blue/green) to minutes (rolling) | as fast as the pipeline: often 15–30 minutes |
| Works when | the old version is compatible with the current data | always, if the pipeline is fast and the fix is understood |
| Risk | the old version may not understand data written by the new one | a rushed fix under pressure |
Default to rolling back when users are affected and the cause is unclear, and make sure that is always possible. Rollbacks & Roll-forward covers the release-side mechanics (reverting tags, yanking packages); this lesson covers running services.
The expand/contract pattern
During a rolling, blue/green or canary deployment, two versions run against one database. Any schema change must therefore work with both the old and the new code. The standard technique splits a breaking change into compatible steps, each its own deployment:
| Step | Release | Schema | Code |
|---|---|---|---|
| 1. expand | v2 | add the new column email_address (nullable) |
write both columns, read the old one |
| 2. migrate | v2 job | backfill email_address from email in batches |
— |
| 3. switch | v3 | — | read the new column, still write both |
| 4. stop writing | v4 | — | read and write only the new column |
| 5. contract | v5 | drop email |
same code as v4 |
At every point, the version before the current one still works, so every deploy can be rolled back by one step. The model plays a rename through two plans and checks, after each deployment, whether the previous code version would still work against the schema (the condition for a safe rollback):
def works(code, schema):
"""A code version works if every column it reads and writes exists."""
return code["reads"] <= schema and code["writes"] <= schema
CODE = {
"v1": {"reads": {"email"}, "writes": {"email"}},
"v2": {"reads": {"email"}, "writes": {"email", "email_address"}},
"v3": {"reads": {"email_address"}, "writes": {"email", "email_address"}},
"v4": {"reads": {"email_address"}, "writes": {"email_address"}},
"v5": {"reads": {"email_address"}, "writes": {"email_address"}},
"v2-big-bang": {"reads": {"email_address"}, "writes": {"email_address"}},
}
def play(name, steps):
print(name)
schema, previous = {"email"}, "v1"
for version, add, drop in steps:
schema = (schema | add) - drop
ok_new = works(CODE[version], schema)
ok_old = works(CODE[previous], schema)
print(f" deploy {version:<12} schema={sorted(schema)} new works: {ok_new} "
f"rollback to {previous} works: {ok_old}")
previous = version
play("expand/contract, contract right after the switch", [
("v2", {"email_address"}, set()),
("v3", set(), set()),
("v5", set(), {"email"}), # contract too early: v3 still writes email
])
play("expand/contract, fixed", [
("v2", {"email_address"}, set()),
("v3", set(), set()),
("v4", set(), set()),
("v5", set(), {"email"}),
])
play("big-bang rename", [
("v2-big-bang", {"email_address"}, {"email"}),
])expand/contract, contract right after the switch
deploy v2 schema=['email', 'email_address'] new works: True rollback to v1 works: True
deploy v3 schema=['email', 'email_address'] new works: True rollback to v2 works: True
deploy v5 schema=['email_address'] new works: True rollback to v3 works: False
expand/contract, fixed
deploy v2 schema=['email', 'email_address'] new works: True rollback to v1 works: True
deploy v3 schema=['email', 'email_address'] new works: True rollback to v2 works: True
deploy v4 schema=['email', 'email_address'] new works: True rollback to v3 works: True
deploy v5 schema=['email_address'] new works: True rollback to v4 works: True
big-bang rename
deploy v2-big-bang schema=['email_address'] new works: True rollback to v1 works: False
The model caught a mistake that is easy to make on paper: dropping email right after the switch breaks the rollback to v3, which still writes the old column. Contract only after a release that stopped using the old column completely (step 4), and every deployment in the plan can be undone. The big-bang rename deploys in one go, and during the rollout (or after a rollback) v1 fails on every query.
Migrations in the pipeline
- Run migrations as their own step, before the new code rolls out (an init job, a pre-deploy job), not in every pod's start-up: twenty pods racing to migrate is a classic outage.
- Only expand-type migrations run automatically (add tables, add nullable columns, add indexes concurrently). Destructive ones (drop, rename, type change) are separate, later, reviewed releases.
- Version the migrations with the code (Flyway, Liquibase, Alembic, Django/Rails migrations, golang-migrate) and test them in CI against a real database (integration tests).
- Large backfills run in batches as background jobs, not in a migration that locks a big table.
- A "down" migration is rarely a safe rollback for data that was written in between; expand/contract makes it unnecessary.
Rollback checklist for a pipeline
- The previous artifact (image digest) is recorded with every deployment and still exists in the registry (retention policies!).
- One documented command or button redeploys it, without rebuilding.
- Config and secrets are versioned with the deployment, so rolling back the code doesn't pair it with new config.
- Feature flags can switch the new behaviour off without any deployment.
- The rollback path is exercised regularly, not only during incidents.
Test yourself
Why must schema changes be backward compatible during a rolling or canary deployment?
- Databases require it
- Old and new code run against the same database at the same time, and a rollback brings the old code back
- Migrations are slow
- Only for NoSQL
In expand/contract, when is the old column dropped?
- In the same release that adds the new one
- In a later release, once no running or rollback-target version uses it
- Never
- Before the backfill
Why shouldn't every pod run migrations on start-up?
- Pods can't connect to databases
- Many pods race to migrate at once, and a failed migration crash-loops the whole rollout
- It's slower to build
- Migrations need root