📁 Sangam Dhakal - Portfolio v2.0 - Netscape Navigator 4.0
_
□
×

📚 Interview question

How do you rename a column with zero downtime when the old and new app versions run at the same time?

Mid-Level Debugging Intermediate

🎯 Key points

  • RenameColumn breaks the old version during a rolling deploy
  • Expand: add the new nullable column
  • Dual write in one transaction, or a sync trigger for old-version writes
  • Batched backfill (e.g. 5,000 rows) to limit locks and log growth
  • Verify: zero rows where old and new differ
  • Switch reads, keep dual writes for rollback
  • Contract: drop the old column in a later release
  • Every step backward compatible with the previous app version

⏱ 30-second answer

Never use RenameColumn while old and new app versions overlap: the old version would query a column that no longer exists. Use expand and contract over several releases. 1. Expand: add the new column (nullable). 2. Dual write: release an app version that writes both columns in the same SaveChanges (same transaction), or add a trigger that copies writes made by the old version, and still reads the old column. 3. Backfill existing rows in small batches (UPDATE TOP (5000) ... WHERE New IS NULL) to avoid long locks and log growth, then verify with a count of mismatches. 4. Switch reads to the new column; keep writing both so a rollback to the previous version still works. 5. Contract: once no running version uses the old column, stop writing it and drop it in a later migration. Each step is backward compatible with the version before it.

🗣 Full answer (senior)

How I would answer this in an interview

"During a rolling or slot-swap deployment both versions run against the same database, so every schema change must work with both. A rename can't: the moment the column is renamed, the old version's queries fail. So I split it into expand, migrate, contract.

Release 1 adds EmailAddress as nullable and ships code that writes both Email and EmailAddress in the same SaveChanges, so both are in one transaction and can't drift, but it still reads Email. Because the previous version is still running for a while and only writes Email, I either add a small trigger that copies its writes or I rely on the backfill running after the old version is gone. Then I backfill in batches of a few thousand rows with a pause between, check SELECT COUNT(*) WHERE Email <> EmailAddress OR EmailAddress IS NULL is zero, and make the column NOT NULL if needed.

Release 2 reads EmailAddress and still writes both, so rolling back to release 1 is safe. Release 3 stops writing Email, and a later migration drops it. It's three releases instead of one, but at no point does a running version see a schema it doesn't understand."


The sequence

Release Schema Writes Reads Safe rollback to
1 Add EmailAddress (nullable) Both Email Previous
Backfill Batched copy, verify — — —
2 (Optional NOT NULL) Both EmailAddress 1
3 — EmailAddress EmailAddress 2
4 Drop Email — — 3
WHILE 1 = 1
BEGIN
    UPDATE TOP (5000) dbo.Customers SET EmailAddress = Email WHERE EmailAddress IS NULL;
    IF @@ROWCOUNT = 0 BREAK;
    WAITFOR DELAY '00:00:01';
END

Production example

A one-step rename on a [12 million]-row table broke [the old App Service slot] for [6 minutes] during a swap; the expand and contract version of the next rename shipped over three releases with zero errors.

🔍 What the interviewer is checking

Checks expand/contract thinking.

↩ Usually asked after

More in EF Core Fundamentals

Last updated 23 Nov 2026.

🌐 Sangam's Webring

⏪ Previous 🏠 Main Next ⏩
💬 Chat with me