Databases & Transactions interview question · Most asked
How do you run schema migrations without downtime?
Summary
Use the expand/contract pattern: first make backward-compatible additions (new nullable columns, new tables), deploy code that writes to both or reads the new shape, backfill data in batches, switch reads, then remove old columns in a later release.
Longer Answer
Avoid long locks: add indexes concurrently (CREATE INDEX CONCURRENTLY), avoid table rewrites on huge tables (adding a column with a volatile default), set lock timeouts, and test migrations against production-sized data.
Key Points
- Expand → migrate → contract
- Batched backfills
- Concurrent index creation
- Lock timeouts
- Old and new code must coexist
More most-asked Databases & Transactions questions
- What does ACID mean?
- Explain transaction isolation levels and the anomalies they prevent.
- How do database indexes work and when do they help or hurt?
- What is the N+1 query problem and how do you fix it?
- How do you choose between SQL and NoSQL databases?
- What is connection pooling and how do you size a pool?
- How do you keep data consistent across services without distributed transactions?