CI/CD · Ship safely, repeatedly
How do you deploy database migrations without downtime?
Short answer
Use expand-and-contract: add a backward-compatible schema change, deploy code that writes both or tolerates both, backfill data in batches, then remove the old structure after all readers have moved.
Application code and schema changes cannot be deployed atomically in a distributed system. At some point old code runs against the new schema and new code runs against the old one, so both directions must be safe.
- Expand: add nullable columns, new tables or indexes without breaking existing queries
- Deploy: write to both old and new shapes, or make reads tolerant of both
- Backfill: populate new data in small batches with throttling and progress tracking
- Switch: move reads to the new shape and verify
- Contract: remove old columns, triggers or dual writes only after every consumer has moved
ALTER TABLE orders
ADD COLUMN status_v2 text NULL;
CREATE INDEX CONCURRENTLY idx_orders_status_v2
ON orders (status_v2);Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1Why can you not simply rename a column in one deployment?
- 2How do you backfill millions of rows safely?
- 3What is the contract phase and when is it safe?
- 4How do you roll back a migration that already ran?
Say it in the interview
I separate schema change from code change using expand and contract. First I add a backward-compatible structure, then deploy code that can read and write both shapes, backfill in throttled batches, and only later remove the old structure. That keeps old and new versions working during a rolling deploy. I also check lock behaviour and replication lag, because a migration that is logically safe can still take the database down.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live CI/CD mock interview and get scored feedback on depth, clarity and confidence.