Scaling and changing databases · senior
Changing a live database without downtime
Schema changes, version upgrades and data moves must be planned in small, reversible steps, with timeouts and a way back.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe notes
What you must know
- An ALTER TABLE can need a strong lock. If it waits behind a long query, every new query on the table queues behind the waiting ALTER. The lock queue, not the ALTER itself, causes the outage. Always set lock_timeout and statement_timeout, retry with back-off, check for long transactions first, and run off-peak.
- Expand and contract: add the column as nullable, deploy code that writes it, fill old rows in batches with pauses while you watch replica lag, then add the rule. Never rename a column in one step: add the new one, write both, fill, switch reads, then drop.
- NOT NULL without a long scan under lock: add a CHECK (col IS NOT NULL) constraint as NOT VALID, then VALIDATE CONSTRAINT, then SET NOT NULL.
- Use CREATE INDEX CONCURRENTLY, outside a transaction. If it fails, drop the INVALID index and retry.
- MySQL: ALGORITHM=INSTANT for suitable changes, set lock_wait_timeout, and use gh-ost or pt-online-schema-change for changes that need a copy.
- Moving to a new database: bulk copy from a snapshot, then change data capture to keep it in sync, verify with counts, checksums and shadow reads, switch reads gradually, then writes in a short freeze, and keep a reverse sync for rollback.
- Major version upgrade: rehearse on a copy with production-sized data, compare plans of the top queries, check old clients and authentication, build a replica on the new version, and have a rollback plan (downgrades are usually not supported).
The red pen
Common traps: mistake → fix
Mistake: Run the ALTER in a long transaction
Fix: it holds locks longer and risks a queue.
Mistake: One UPDATE for 900 million rows
Fix: it locks, bloats and lags replicas; use batches.
Mistake: Upgrade in place without a rehearsal
Fix: you find problems in production with no way back.
Mistake: Dual writes from the app are simple
Fix: one write can fail; use change data capture or an outbox.
Easy to hard
Questions interviewers ask, easy to hard
- How do you add a NOT NULL column to a 900-million-row table safely?
- Why did the last ALTER stop checkout for six minutes?
- How do you move payments data to a new database with no downtime?
- Plan an upgrade from an old MySQL major version.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.