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:
- Expand: add the new column/table/index. Nothing reads it yet; nothing breaks.
- Dual-write: deploy code writing old and new. Reads still use old.
- Backfill:
UPDATE ... WHERE id BETWEENin batches of a few thousand with a sleep — not one 50M-row transaction holding locks and bloating WAL. - Switch reads: deploy reads against the new column.
- 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?
Keep reading
Related articles from the SudoSecurity blog.
One Slow Call, Three Runtimes: How PHP-FPM, Python ASGI and Node Fall Over When a Dependency Takes 200 ms
I put the same 200 ms wait behind PHP-FPM, FastAPI on uvicorn and a plain Node server, then hit each one with 50 concurrent requests. Handled correctly, the async runtimes served 205-211 req/s against FPM's 19.6. One blocking call in the wrong place took both of them down to 4.98, slower than...
PHP OOP Explained Through a Real Kitchen Brigade System
Escoffier's brigade de cuisine solved the same problem PHP's class visibility solves: who gets to touch what, and who answers to whom. Part 2 of the kitchen series, and the one where we build something real.
The Slow Death of vBulletin, and Why It Isn't Over
Forum Archaeology #1: I built plugins for this software when it ran half the internet's communities. Sixteen years after the exodus that should have killed it, vBulletin is still getting hit with critical, unauthenticated remote code execution.
AWS Emulator Puts AWS on Your Laptop. Keep Your IAM Tests Off It.
AWS released AWS Emulator on November 11, a free local server that answers AWS API calls on 127.0.0.1 with per-run keys and fresh certificates.