Quick revision
The short answer
We make safe schema changes the default path: every change follows expand and contract so old and new code both work during a deploy, an automated linter rejects risky DDL before review, every migration runs with lock_timeout, statement_timeout and retries, large backfills run as jobs rather than migrations, and MySQL tables use INSTANT or INPLACE algorithms or an online schema change tool. Migrations run as their own pipeline step before the rollout, with lock-wait monitoring and a rollback plan.
What to remember
In this answer, we designed a schema change process for frequent deploys. Expand and contract keeps every change compatible with old and new code. A linter rejects operations that take long blocking locks. Every migration runs with lock_timeout, statement_timeout and retries, and big backfills run as batch jobs. MySQL uses INSTANT or INPLACE online DDL or gh-ost and pt-online-schema-change for big tables. Migrations run as a separate pipeline step with lock monitoring, and teams review their own changes inside guardrails owned by the platform team.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We make safe schema changes the default path: every change follows expand and contract so old and new code both work during a deploy, an automated linter rejects risky DDL before review, every migration runs with lock_timeout, statement_timeout and retries, large backfills run as jobs rather than migrations, and MySQL tables use INSTANT or INPLACE algorithms or an online schema change tool. Migrations run as their own pipeline step before the rollout, with lock-wait monitoring and a rollback plan.
Why this question is asked
Deploying several times a day means schema changes stop being rare events handled by an expert and become routine work done by every team. With 80 services and 30 databases, someone will eventually add an index without CONCURRENTLY or change a column type on a large table, and the result is a lock that stops a service. Interviewers ask this design question to see whether we can turn a set of DBA tricks into a process that works without heroics. In this answer, we will lay out the expand and contract pattern, automated checks, the required settings for every migration, the MySQL specifics, and how migrations fit into the deployment pipeline, ending with who should review them.
1Why DDL Locks Break Frequent Deploys
Most DDL statements take a strong table lock, ACCESS EXCLUSIVE in PostgreSQL or an exclusive metadata lock in MySQL, that blocks every other query on the table while it is held or while it waits in the lock queue. Some statements hold that lock for milliseconds, others for as long as it takes to rewrite or scan the whole table. A process that allows frequent deploys must make sure that only the first kind reaches production unattended, and that even the fast kind gives up quickly if it cannot get its lock.
Compatibility is the second risk: during a rolling deploy, old and new versions run side by side, so a renamed column breaks the old version the moment the migration runs.
2Expand and Contract: Every Change Is Backward Compatible
Expand and contract splits a breaking change into steps that are each compatible with the code running before and after them. Suppose we rename orders.addr to orders.shipping_address. We never rename in place. First we expand: add the new column, which old code simply ignores. Then we deploy code that writes both columns and reads the new one with a fallback. A backfill job copies old values. Once all readers use the new column, a later release contracts: it stops writing the old column, and a migration weeks later drops it.
Each step is small and reversible, and the dangerous moment, dropping data, happens last, after we have proof that nothing reads the old column. This also means migrations and code can be deployed independently, which is what makes several deploys a day possible.
- Expand: add shipping_addressnullable, metadata only
- Deploy code that writes bothreads new, falls back to old
- Backfill job copies old valuesbatches, outside migrations
- Switch all reads to the new column
- Any reads of addr in a week?YesFind and fix them firstNoContract: stop writing addr
- Later release drops addrafter a backup and a waiting period
6 steps in order. Play them, or step through them one at a time.
Every step is compatible with the code running on either side of it.
3Automated Linting Rejects Risky Operations
People forget rules; a linter does not. Every migration file is checked in the service's pull request build by a migration linter. For PostgreSQL, squawk is a widely used open-source example; for MySQL, teams often write a small set of custom rules or rely on the online schema change tool's own checks. The linter rejects operations known to take long blocking locks and explains the safe alternative.
A few rules catch most incidents: CREATE INDEX without CONCURRENTLY, volatile defaults and column type changes that rewrite the table, foreign keys or CHECKs without NOT VALID, direct SET NOT NULL without a validated check, and any migration without lock_timeout. Teams can override a rule with a comment explaining why, visible in review.
The linter runs in the pull request, so the fix costs a minute rather than an incident; rule names depend on the tool version.
4Required Settings for Every Migration
Every migration runs with a short lock_timeout, for example three seconds, so a statement that cannot get its lock gives up instead of queueing in front of traffic, and with a statement_timeout so a statement that turned out to be slower than expected is stopped. The pipeline retries a failed migration a few times with a pause. We set these centrally, on the migration role or through the migration tool's connection initialisation, so no author has to remember them. In MySQL the equivalent is a low lock_wait_timeout for metadata locks.
Large data changes are not migrations. Backfills run as idempotent batch jobs, a few thousand rows per transaction with pauses, so they can be stopped and resumed.
5MySQL Specifics: Online DDL and Schema Change Tools
MySQL 8 has online DDL. We ask for it explicitly, so that MySQL refuses the change instead of silently choosing a blocking method. ALGORITHM=INSTANT changes only metadata and works for adding columns and some other changes. ALGORITHM=INPLACE, LOCK=NONE rebuilds or builds in place while allowing reads and writes, for example when adding a secondary index. Both still take a brief exclusive metadata lock at the start and end, so a long transaction can block them, which is why the short lock_wait_timeout matters.
For big tables, or changes MySQL cannot do online, gh-ost or pt-online-schema-change build a copy with the new structure, copy rows in chunks, keep it in sync (gh-ost from the binary log, pt-osc with triggers) and swap the tables with a quick rename, throttling when replicas lag.
6Migrations as Their Own Pipeline Step
Migrations run as a separate step before the rollout, not on application startup, where 20 instances would compete for the migration lock and a slow migration would delay every health check. One migration job per database has one log and one clear result.
While the migration runs, the pipeline watches lock waits and the age of the oldest transaction on that database, and it aborts if waits climb. Because every change is expand-only, rollback rarely means reversing DDL: we roll back the application, and the new column or index simply stays unused until we fix forward. For contract steps that drop data, the plan includes a backup or a soft delete period before the drop.
7Who Reviews Migrations
A central DBA team approving every migration for 80 services becomes a bottleneck, and teams start batching changes, which makes each riskier. Teams own and review their migrations inside guardrails; the platform or DBA team owns the lint rules, tooling and monitoring, and reviews only what the linter flags as high risk.
8Follow-up questions
Who should own reviewing migrations: DBAs, platform or teams?
Teams own their migrations and review them like code, because they know the data and the deploy plan. The platform or DBA group owns the guardrails, meaning linting, required timeouts, online schema change tools and lock monitoring, and reviews only what the guardrails flag as high risk. Central approval of every change does not scale to several deploys a day.
How do you handle a migration that must rewrite a large table?
We avoid the rewrite with a new column and a backfill whenever possible. When a rewrite is unavoidable, in MySQL we use gh-ost or pt-online-schema-change; in PostgreSQL we build a new table, copy in batches while keeping it in sync, and swap it in a short transaction, or use logical replication. Either way it is scheduled and watched, not part of a routine deploy.
9Conclusion
In this answer, we designed a schema change process for frequent deploys. Expand and contract keeps every change compatible with old and new code. A linter rejects operations that take long blocking locks. Every migration runs with lock_timeout, statement_timeout and retries, and big backfills run as batch jobs. MySQL uses INSTANT or INPLACE online DDL or gh-ost and pt-online-schema-change for big tables. Migrations run as a separate pipeline step with lock monitoring, and teams review their own changes inside guardrails owned by the platform team.
Open with
I would make the safe way the easy way, with five parts. First, expand and contract: no change breaks the running code. To rename a column we add the new one, deploy code that writes both, backfill, switch reads, and drop the old column in a later release.
Then, if they want more
That also lets migrations and code deploy independently. Second, automated linting in every pull request, for example squawk on PostgreSQL. It rejects non-concurrent index creation, constraints without NOT VALID, table rewrites from volatile defaults or type changes, and any migration without a lock timeout.
Third, required settings: lock_timeout of a few seconds and a statement_timeout set on the migration role, a short lock_wait_timeout on MySQL, and retries in the pipeline. Backfills are batch jobs, not migrations. Fourth, MySQL specifics: we always state ALGORITHM=INSTANT or INPLACE with LOCK=NONE so MySQL fails instead of blocking, and for big tables we use gh-ost or pt-online-schema-change.
Fifth, migrations run as one pipeline step per database before the rollout, while we watch lock waits and the oldest transaction, and since changes are additive, rollback means rolling back code. Teams own their migrations; the platform team owns the guardrails and reviews only what they flag as risky.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- MySQL 8
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- PostgreSQL documentation, MySQL reference manual
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Design a schema change process for 80 services and 30 databases (PostgreSQL and MySQL)...": https://scrivio-interview-library.netlify.app/databases/database-locking/370353d202/