Sub topic 10
Databases and Compatibility: Running Two Versions at Once
Rolling, blue-green, and canary deployments all depend on two versions of your service running at the same time. During a rolling update, old and new pods overlap. During a blue-green cutover, both environments usually share one database. During a canary, the new version serves live traffic while the old one still does. The database is shared state, so it has to work for both versions.
This chapter assumes you know how Deployments and Services work, and that you have used a relational database with schema migrations.
Why the database is the hard part
You can roll back application code by redeploying the old image. You cannot easily roll back a dropped column or rewritten data. If a migration breaks the old version, rolling back the code does not help, because the old code now fails against the new schema.
Two rules follow from this:
- The new code must work with the old schema, in case the migration has not run yet.
- The old code must work with the new schema, in case you roll back after the migration has run.
A change that meets both rules is backward compatible. Each schema change must be backward compatible with the version that will still be running when it is applied.
The expand and contract pattern
The standard technique splits one breaking change into several safe ones. It is called expand and contract, or parallel change.
- Expand. Add the new structure without removing the old one. Add a nullable column, a new table, or a new index.
- Migrate. Deploy code that writes to both the old and new structures. Backfill existing rows.
- Switch. Deploy code that reads from the new structure.
- Contract. After the old version can no longer be rolled back to, remove the old column or table.
Each step is a separate deployment. Each is safe on its own, and each can be rolled back to the previous step.
Example: renaming a column
Suppose you want to rename users.fullname to users.display_name. A direct ALTER TABLE ... RENAME COLUMN breaks every running pod of the old version at once.
Instead, the expand step looks like this:
ALTER TABLE users ADD COLUMN display_name text;
UPDATE users
SET display_name = fullname
WHERE display_name IS NULL;The old version keeps using fullname and ignores the new column. The next release writes both columns. A later release reads display_name. Only after that release is stable, and you no longer plan to roll back past it, do you run:
ALTER TABLE users DROP COLUMN fullname;On a large table, run the backfill in batches. One huge UPDATE can lock rows and slow the live service.
Common errors
| Change | Why it breaks | Safer approach |
|---|---|---|
| Rename or drop a column in one step | Old pods still query it and fail | Expand and contract over several releases |
| Add a `NOT NULL` column with no default | Old code inserts rows without it and the insert fails | Add it nullable, backfill, then enforce the constraint later |
| Change a column type in place | Old code may send or expect the old format | Add a new column and migrate |
| Run a long, locking migration at deploy time | Readiness checks fail and traffic stalls | Use online schema change tools or batched updates |
| Change a message or API format without versioning | Old consumers cannot parse it | Accept both formats first, then switch producers |
The same idea applies outside the database. Caches, queues, and APIs are also shared between versions. A new version that writes a cache entry the old version cannot read causes errors in the old pods.
Ordering migrations with deployments
Decide when the migration runs relative to the rollout. For additive changes, run the migration before the new code. Then the new pods find the schema they need, and old pods ignore it. A Kubernetes Job, or a pre-deploy step in your pipeline, can run it once. Avoid running migrations from inside every starting pod, because several pods may try to apply them at the same time.
With blue-green, this matters even more. The idle environment points at the same database as the live one. If green needs a different schema than blue can tolerate, the instant switch back that makes blue-green attractive no longer works safely.
Exercise
Your team wants to change orders.status from an integer code to a text value, using a rolling update. Why is changing the column type in one migration unsafe, and what sequence of releases would you use?
Show answer
During a rolling update, old pods still run and expect an integer, so an in-place type change makes their reads and writes fail. Use expand and contract. First add a nullable text column, such as status_text. Second, deploy code that writes both columns, and backfill old rows in batches. Third, deploy code that reads status_text. Fourth, once you no longer plan to roll back to the integer-only version, drop the old integer column.