skip to content

In a health-diary app, an on-device migration must split a free-text dose field across three years of entries, taking 40 seconds on old phones; how do you run it?

level: seniorimportance: should knowfreq 31%

answer

  1. schema change versus data rewrite
  2. who is waiting while it runs?
  3. add columns now, backfill later
  4. batches with a committed progress marker
  5. readers handle both shapes meanwhile

basics

~20 s

Split it: a fast schema step adds the new columns at open, then a resumable backfill converts rows in transactional batches with a committed progress marker while readers handle both shapes. Block with progress only if old rows cannot be shown.

solid answer

~50 s

Adding empty columns is a schema-only change that usually takes moments; parsing every `dose` text into amount and unit is a data migration whose cost grows with the user's history. So the release ships a fast step that adds nullable `dose_amount` and `dose_unit` columns and bumps the version, then a backfill that converts rows in batches of 500, newest first, each batch committed with a progress marker. If the OS ends the process, the next launch resumes after the last committed batch. Until a `backfill done` flag is set, readers use the new columns when filled and parse the old text when not, and new writes fill both. Text that cannot be parsed stays as typed and is flagged, never dropped. A blocking screen with progress is the fallback only when old-shape rows cannot be shown correctly at all.

go deeper

for a junior

Recall that some migrations only add structure while others rewrite every row, and that the second kind can take a long time on a big history.

for a middle

Explain the split into a fast schema step and a batched backfill, and why each batch commits with its progress marker.

for a senior

Show the operational judgment: sizing the work from stated numbers, newest-first ordering, readers that handle both shapes, kills mid-way, and when a blocking progress screen is the honest choice.

for a principal

Weigh the rule for the team: which data rewrites may run in the background, how long dual-shape reads are kept, and when the old column may finally go.

## Two kinds of migration A **schema-only migration** changes structure without touching existing rows' values: adding a nullable column or a new table. It usually finishes in moments regardless of how much data there is. A **data migration** reads and rewrites rows: splitting a field, backfilling a derived value, moving data between tables. Its cost grows with the user's data, and an automatic schema-only migration cannot infer it; one platform's guide gives splitting a table as the case that needs hand-written migration code. The diary's change is a data migration. Each entry holds a free-text `dose` such as `2 x 500mg`, and the new release wants `dose_amount` and `dose_unit` so it can chart totals. ## Sizing it The scenario's own assumptions: a heavy user has about 16,500 entry rows after three years; on the oldest supported phone, parsing and rewriting a batch of 500 rows takes about 1.2 seconds. That is 33 batches, about 40 seconds. Migrations run when the store opens, usually at launch, so done naively that is 40 seconds of a frozen launch screen. The launch budget itself belongs to the startup discussion; here the question is how to keep the data safe while not spending it. ## Three ways to run it | Approach | User experience | Cost | |---|---|---| | Block at open with a progress screen | waits, but sees why | simple; the app is unusable meanwhile, and the user may leave | | Schema step now, background backfill | app usable at once | readers must handle both shapes until done | | Lazy, per row when read or written | no wait at all | old rows may stay unconverted for years; charts over all data must parse anyway | For the diary, the middle option fits: entries are still readable in the old shape, so nothing forces the user to wait. ## Designing the backfill 1. **Schema step first**, in its own transaction: add the two nullable columns and bump the schema version. Fast, so it runs at open. 2. **Backfill in batches**, each in one transaction that converts 500 rows and writes a **progress marker**, such as the last converted entry id, in the same commit. 3. **Newest first**, so the entries the home screen shows are converted in the first seconds. 4. **Resume, do not restart.** The process can be ended at any time: the user switches away and the OS reclaims memory, or the battery dies. The next launch reads the marker and continues from the next batch. Because the marker commits with its batch, a kill can never leave converted rows the marker does not cover. 5. **Finish with a flag**: when the last batch commits, set `backfill done`; only then may readers drop the fallback path. ## Living with two shapes While the backfill runs, the code has to be honest about mixed data: - **reads** use `dose_amount` and `dose_unit` when filled, and parse `dose` when they are null; - **new writes** fill the new columns and keep the old text, so a conversion never loses the original; - **unparseable text** stays exactly as typed and is flagged for the user to review, because guessing a dose is worse than showing the original; - **aggregate screens**, like a monthly total, either wait for the flag or label themselves as still updating. ## When blocking is right A progress screen is the honest choice when old-shape data cannot be shown correctly at all, such as a change in what a stored value means, or when the migration must finish before a feature can work safely. Even then each batch commits with its marker, so a user who leaves mid-way resumes rather than restarts, and the progress shown is real. ## Traps - Running the whole rewrite in one transaction at launch: the user waits, and a kill near the end throws all the work away. - Starting a background backfill without teaching readers the old shape, so half the history renders blank. - Deleting the old column in the same release, removing the fallback before every row is converted. - Converting oldest rows first, so the screens the user opens first are the last to benefit. ## What the interviewer is checking That the candidate distinguishes a structural change from a data rewrite, sizes the rewrite from stated numbers rather than hoping it is quick, and designs for the kill: committed batches, a marker that cannot disagree with the data, and readers that stay correct while the conversion is unfinished.

  • Why commit the progress marker in the same transaction as its batch?
    Then the marker and the data can never disagree. If they were separate, a kill between them could leave converted rows the marker does not know about, which is harmless only if conversion is idempotent, or a marker ahead of the data, which skips rows for good.
  • When can the release drop the old free-text column?
    Only in a later release, after the backfill-done flag is set on the device and no reader depends on the old text. That later release's own step checks the flag and finishes any remaining batches first, because a user may jump straight to it from a build that never started the backfill.

  • Splitting a field is just a schema change, so it costs the same as adding a column.
  • An automatic schema-only migration can work out how to split free text.
  • A background backfill needs no change to the code that reads entries.
  • If the OS ends the app mid-backfill, the conversion must restart from the first row.
  • Rows whose text cannot be parsed can be dropped to finish the migration.