A release needs to rename a column that the running app reads and writes. How do you do it with zero downtime?
IntermediateScenarioData and databases
Answer
Situation read → During a rolling deploy, old and new versions run together. A plain rename breaks whichever version doesn't know the new name. Use expand and contract.
What I'd do →
- Expand: add the new column (nullable), deploy code that writes to both and reads the old.
ALTER TABLE users ADD COLUMN email_address text;
- Backfill in small batches to avoid long locks:
UPDATE users SET email_address = email WHERE id BETWEEN 1 AND 10000 AND email_address IS NULL;
- Switch reads to the new column in the next release (still writing both).
- Contract: stop writing the old column, and a release or two later drop it.
Why → Each step is backward compatible, so you can roll back the app at any point without a data fix.
Interview tip → Say "the database change must be compatible with both the old and new app version"; that's the core rule.
More on Data and databases
How do you prove your database backups actually work?IntermediateDesign backups for an RDS PostgreSQL database with RPO of 5 minutes and RTO of 1 hour, including a region failure.AdvancedYour migrations run in an init container and two pods ran them at the same time during a deploy, leaving the schema half-applied. How do you fix the approach?IntermediateUpgrade RDS PostgreSQL from 13 to 17 with minimal downtime. How?AdvancedYou need to add an index and a column to a 500-million-row table that's written to constantly. How do you avoid locking it?AdvancedUsers see stale data right after saving because reads go to a replica with lag. How do you handle it?Intermediate