Split it into backward compatible steps. Deploy the schema change first in a form the running code tolerates, such as adding a nullable column, then deploy code that writes to both old and new, backfill in batches, switch reads, and only drop the old column in a later release. Avoid long table locks and always test the rollback path.
Why interviewers ask this
Migrations are where careful engineers are separated from people who have only worked on projects small enough to take an outage. The interviewer wants the expand and contract pattern, an understanding that old and new code run simultaneously during a rolling deploy, and awareness of which operations lock a table in Postgres long enough to take the site down.
How to structure your answer
- Name the expand and contract pattern.
- Order the deploys so both code versions work.
- Backfill in batches, never in one statement.
- Call out locking operations to avoid.
Example answer
The rule I hold to is that during a rolling deploy two versions of the code are talking to one database, so every step has to be safe for both. Renaming a column is the classic trap. Instead I expand: add the new nullable column in its own migration, ship code that writes both fields and still reads the old one, backfill existing rows in batches of a few thousand with a pause between them so replication does not fall behind, then flip reads in the next release, and only in a third release drop the old column. Postgres specifics matter too. Adding a NOT NULL constraint on a large table takes a heavy lock, so I add the column nullable, backfill, then validate the constraint separately. Django gives you separate_database_and_state and non atomic migrations when you need them, and I set a short lock_timeout so a blocked migration fails fast instead of queuing every query behind it. Every migration also gets a tested rollback.
Walking into this interview soon? GhostPilot listens to your live call, spots the question the moment it is asked, and puts a structured answer on your screen in real time. Try it on your next mock, or grab a $29 Session Pass, no subscription, for the real thing.
See how it worksFollow-up questions to expect
- What if the backfill takes six hours?
- How do you add an index without blocking writes?
- How do you roll back a deploy after the schema changed?
Related python developer questions
Your interviewer will ask their own version of this. Paste your actual job description into the free Question Predictor and get the 20 questions that role is most likely to ask, with what each one is really probing.
Predict my questions