Quick revision
The short answer
During a rolling update, instances of the old and new application versions run side by side against the same database for minutes. If the migration renames the column, every old instance still querying the old name fails at once. Instead we use expand and contract: add the new column, write to both, backfill, switch reads to the new column, and only drop the old one in a later release, so every step works with both the current and the previous application version.
What to remember
In this answer, we saw that a rolling update runs the old and new application versions against the same schema at the same time, so a rename breaks every old instance the moment it is applied, and a rollback breaks all of them. Expand and contract splits the change into compatible steps: add the new column, write both, backfill, switch reads, stop the old writes and drop the old column in a later release, so every step works with both the current and the previous version.
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.
During a rolling update, instances of the old and new application versions run side by side against the same database for minutes. If the migration renames the column, every old instance still querying the old name fails at once. Instead we use expand and contract: add the new column, write to both, backfill, switch reads to the new column, and only drop the old one in a later release, so every step works with both the current and the previous application version.
Why this question is asked
Renaming a column looks like the smallest possible schema change, one line of SQL. In a world where we stop the application, migrate and start it again, it is that simple. But modern deployments replace instances one by one without downtime, and that changes the rules for every schema change. Interviewers ask this question because it checks whether we think of a deployment as a moment or as a period of time. In this answer, we will see what a rolling update really looks like from the database's point of view, reproduce the failure, then walk through the expand and contract pattern step by step and the compatibility rule behind it.
1What a Rolling Update Looks Like to the Database
In a rolling update, the platform replaces the instances of a service gradually. With 6 instances, it may start one new instance, wait until it is healthy, stop one old instance, and repeat. For several minutes, some instances run version 1 of the code and others run version 2, and all of them use the same database. If the deployment fails halfway, the platform may roll back, so version 1 instances can come back even after version 2 started.
The migration runs once, at the beginning of this period, usually when the first new instance starts or in a step before the rollout. From then on the schema is the new one, but much of the traffic is still served by old code. So the schema must work for both versions at the same time.
2Reproducing the Failure
Suppose we want to rename customer.mail to customer.email. Version 2 of the code uses the new name and ships with this migration.
Notice that the rename itself is instant and the new instance works perfectly. The outage comes from the old instances, which keep running until the platform replaces them. Rolling back makes it worse: version 1 comes back everywhere, and now every instance fails, because the column has the new name and version 1 does not know it.
3Expand and Contract, Step by Step
The fix is to split the rename into steps spread over several releases, so that no single step breaks the version running before it. First we expand the schema by adding what the new code needs, then we move the code and data across, and only at the end we contract the schema by removing what is no longer used.
| Release | Schema | Code writes | Code reads |
|---|---|---|---|
| 1 expand | mail and email | both | |
| 1 backfill | mail and email (filled) | both | |
| 2 switch reads | mail and email | both | |
| 3 stop old writes | mail and email | ||
| 4 contract | email only |
Each step is boring on purpose. In release 1, old code ignores the new column, so it is unaffected. New code writes both columns, so whichever version handles a request, both columns stay correct for new data, and the backfill fixes old data. In release 2, reads move to email, but because both columns are still written, a rollback to release 1 code is safe. In release 3, nothing reads or writes mail anymore. Only then, in release 4, do we drop it, at a point when no running version and no version we might roll back to uses it.
4The Rule Behind It: Compatible With Two Versions
The general rule is that every migration must be compatible with both the application version currently running and the version being deployed, and ideally with the version we might roll back to. Adding a nullable column, adding a table or adding an index is compatible, because old code does not notice it. Renaming, dropping, changing a type in a way that rejects old values or adding a NOT NULL column without a default is not, because old code relies on the old shape.
So destructive changes always come last and alone, in a release after the code stopped depending on the old structure. If we have to decide whether a change is safe, the question is simple: could the previous version of the application run against the schema after this migration? If not, split it.
5Doing It in a Spring Application
With JPA, the in-between phase can be expressed directly in the entity. In release 1, the entity maps both columns and a setter writes both; in release 2, the getter reads the new one. Hibernate's ddl-auto must be validate or none, so only the migrations change the schema. Reads in native queries, reports and other services also need to switch before the contract step, which is often the slowest part, because other teams' queries are easy to forget. A useful check before dropping a column is the database's own statistics or query logs, which show whether anything still reads it.
6Follow-up questions
Can a database view help with renames?
Yes. Some teams rename the table or column and create a view with the old name so old code keeps working during the transition. It works for reads, and some databases allow simple views to be updated, but it adds its own complexity. The expand and contract steps are usually clearer and work for any change.
Is a trigger better than writing both columns from the code?
A trigger that copies mail to email on every insert and update keeps the columns in step even for writes from other systems that we cannot change. It is a good choice when many writers exist. The cost is hidden logic in the database that must be removed in the contract step.
Do we really need four releases?
The number depends on the deployment style, but the separation of steps does not. Some steps can share a release when nothing could roll back across them. What must never share a release is the destructive step and the code change that stops using the old structure.
7Conclusion
In this answer, we saw that a rolling update runs the old and new application versions against the same schema at the same time, so a rename breaks every old instance the moment it is applied, and a rollback breaks all of them. Expand and contract splits the change into compatible steps: add the new column, write both, backfill, switch reads, stop the old writes and drop the old column in a later release, so every step works with both the current and the previous version.
Open with
A rolling update means that for several minutes old and new instances of the application run at the same time against the same database. The migration runs at the start of that window.
Then, if they want more
If it renames customer.mail to email, the new instances work, but every old instance still selects mail and fails immediately, and if we roll back, all instances fail. So I never rename in one step; I use expand and contract.
In the first release, the migration adds email as a new nullable column and the code writes both columns while still reading mail. Then I backfill existing rows in batches. In the next release, reads switch to email while writes still go to both, so a rollback is still safe.
After that, the code stops writing mail, and only in a later release does a migration drop mail, when no running version and no rollback target uses it. The rule behind it is that every migration must be compatible with both the current and the previous application version.
Adding things is compatible; renaming, dropping or tightening constraints is not, so destructive steps come last and alone. Before the final drop, I check query logs or statistics to make sure nothing else, like reports or another service, still reads the old column.
About this answer
- Last updated
- 12 November 2026
- 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
- Spring Data JPA reference, Hibernate ORM documentation, Spring Framework reference, Transaction management
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 "Why can't you simply rename a column in a migration when the application is deployed...": https://scrivio-interview-library.netlify.app/databases/schema-migration/345624ff15/