Skip to content

Lesson 12 of 13 · 18m

Zero-Downtime Schema Changes and Deploys

Expand-contract migrations, lock convoys and `lock_timeout`, `CREATE INDEX CONCURRENTLY`, and rolling/blue-green/canary deploys that keep N and N+1 compatible.

ALTER TABLE users ADD COLUMN plan text NOT NULL DEFAULT 'free' on Postgres 10 takes an ACCESS EXCLUSIVE lock and rewrites every row — on a large table, writes queue behind it for minutes. The deploy "took the site down" without a single error. Since Postgres 11 (2018), a constant default is metadata-only: the catalog updates, existing rows read the default lazily, and the statement finishes instantly. The difference between those two sentences is a production incident.

The lock is the killer

The dangerous migration isn't slow — it's queued. An ALTER TABLE waiting on ACCESS EXCLUSIVE sits behind one long query, and every write arriving next queues behind it: the lock convoy. Set lock_timeout (5s is fine) on migrations so a queued ALTER aborts instead of damming the write path, with statement_timeout as a backstop.

CREATE INDEX CONCURRENTLY is the same story inverted: it skips the write-blocking lock, costs 2–3x longer, cannot run inside a transaction — Django migrations need atomic = False, Alembic needs it outside the transaction wrapper — and leaves an INVALID index on failure to DROP or REINDEX CONCURRENTLY (PG12+) and rebuild.

The same trick exists for constraints: ADD CONSTRAINT c CHECK (price >= 0) NOT VALID skips the table scan; VALIDATE CONSTRAINT c in a second statement takes the weaker SHARE UPDATE EXCLUSIVE lock and lets writes through. ALTER COLUMN ... TYPE rewrites the table like the old ALTER did — the expand-contract path is a new column plus backfill, never a type change in place.

And note the asymmetry: code rollbacks are a redeploy; schema rollbacks don't exist. Every migration is one-way, which is why contract steps wait until no binary references the old shape — you can't un-drop a column mid-incident.

Expand-contract is the whole trick

Never mutate schema and code in the same deploy. The sequence:

  1. Expand: add the new column/table/index. Nothing reads it yet; nothing breaks.
  2. Dual-write: deploy code writing old and new. Reads still use old.
  3. Backfill: UPDATE ... WHERE id BETWEEN in batches of a few thousand with a sleep — not one 50M-row transaction holding locks and bloating WAL.
  4. Switch reads: deploy reads against the new column.
  5. Contract: drop the old column — only after no running binary references it.

Renames are just drop + add. Rename name to full_name in one deploy and every pod still on the old code crashes on INSERT. The multi-deploy version: add full_name, dual-write, backfill, switch, drop name later. Feature flags sit on top of the same idea — decouple "deployed" from "enabled" so rollback is a toggle, not a revert. The discipline is deletion: a flag that never gets removed is debt with a UI.

Deploy strategies are the same shape

  • Rolling: replace instances one at a time. Old and new run simultaneously — which is why expand-contract is mandatory: version N and N+1 must interoperate.
  • Blue-green: two full environments, cut traffic at the balancer, rollback is a config flip. Double the infra for instant rollback.
  • Canary: 1% of traffic to the new version, watch SLOs, ramp or halt. Catches what tests miss because real traffic is weird.

GitHub built gh-ost for online MySQL migrations at their scale — shadow table, binlog catch-up, controlled cutover; Percona's pt-online-schema-change is the older incarnation. The point isn't the tool: "the migration finishes without locking the write path" is a requirement you engineer, not a default.

The wrong model

"Migrations run during deploys, so deploys are risky" confuses the mechanism. Locking schema changes during deploys are risky. Unlocked by CONCURRENTLY, expand-contract, and mixed-version-safe code, a deploy is a boring event. Keep it boring.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.