The interview problem
Move a tenant with 2 TB of database state from shard A to shard B while it receives 8,000 writes per second. Normal reads and writes should continue during copying, and the final write interruption should be bounded. Design snapshot copying, catch-up, validation, cutover and rollback without losing an acknowledged write or allowing two authoritative writers.
Original system-design practice, not a reported company interview. Capacity assumptions and protocol examples are hypothetical. Primary reference retrieved September 27, 2026.
1. Define the unit and ownership contract
Assume a tenant's transactional rows and idempotency records reside together, and its shard key is stable during migration. Inventory secondary indexes, sequences, outbox state and references before copying. Cross-tenant transactions or external side effects require additional coordination and are outside this first design.
The invariant is one authoritative application writer for a tenant at any time. The target may accept replication writes while staging data, but must reject ordinary application writes until activated. A replicated routing registry records tenant, owner, ownership epoch and migration phase. Router caches improve speed; they do not enforce correctness.
Every write path, including background jobs and administrative tools, must use a storage-enforced tenant gate. A transaction holds a compatible gate lock until commit. Closing the gate takes an exclusive lock, waits for earlier writes to finish and durably rejects later writes. Supported shard failovers preserve that gate and acknowledged transaction history.
2. Copy from a snapshot tied to a log position
Create a consistent snapshot S with a database-supported change-log starting position L₀. Start retaining all tenant changes after L₀ before the snapshot can become stale. Copy rows in resumable primary-key ranges into a staging namespace on B; record completed ranges and schema version.
For a simple correctness model, finish the snapshot load before replaying its retained changes. If copy and replay overlap, use a proven per-range watermark/version protocol so an older snapshot row cannot overwrite a newer update. Deletes must be replayed too; omitting tombstones can resurrect rows.
Replay committed source transactions in the required order and atomically advance each apply checkpoint with its data changes. Repeating a batch after a crash must not duplicate its effects. Preserve operation keys and their original outcomes so a client retry after migration remains the same operation.
Pause incompatible schema changes, or provide a versioned transformation with tested replay semantics. If required log history expires before catch-up, stop and restart from a new consistent snapshot; skipping the gap is not recovery.
3. Estimate copy and catch-up time
Use decimal units for this example. At a sustained effective copy rate of 200 MB/s, 2 TB takes 2,000,000 MB / 200 = 10,000 seconds, about 2.78 hours. This assumes that measured throughput already accounts for the source read and destination write bottlenecks; indexes and contention can change it.
At 8,000 writes/s and an average 2 KB of captured change per write, the retained stream grows at 16 MB/s. A copy-then-replay design accumulates about 160 GB over those 10,000 seconds. Provision additional headroom for retries, bursts and a slower copy.
If replay sustains 24,000 writes/s while new writes continue at 8,000/s, net drain is 16,000 writes/s. The 80 million accumulated writes take 5,000 seconds, about 1.39 hours, to drain under these steady assumptions. If apply capacity falls to the incoming rate, the backlog cannot shrink.
Monitor bytes and transaction cost as well as event count. One large transaction may dominate pause time. Throttle copying when source latency or replication lag threatens production, and ensure log retention exceeds the measured worst-case migration duration rather than only the optimistic estimate.
4. Execute a fenced cutover
Use a durable migration state machine: copying, catching up, source-fenced, target-ready, target-active and complete. Each transition has an idempotent recovery action and an ownership epoch. Serialize migration controllers through the registry; stale controllers cannot activate destinations.
First close A's tenant write gate and drain or safely cancel in-flight transactions. Only after no prior writer can still commit, establish a final source commit position L*. Keep A fenced. Replay on B through L* and verify that all required tenant state is applied. Low estimated lag alone is insufficient.
Then conditionally move registry ownership from A at epoch e to B at epoch e+1, recording that B is caught up through L*. B's application gate opens only on that authoritative activation. A remains closed even if a router sends a stale epoch. A crash between these steps may leave a short period with no writer; it must never create two writers.
If the registry changes but B has not opened yet, recovery completes B's activation. An uncertain registry write requires an authoritative status read, not a guessed rollback. Budget the healthy cutover pause across transaction draining, final replay, validation and activation. If that budget expires before activation, cancel only through the fenced cancellation protocol. A registry or shard outage can exceed the desired pause; preserve safety and return retryable errors rather than promising a hard availability bound under arbitrary failures. Buffer requests only within a stated memory and time budget, then return retryable errors with the same operation identity.
5. Trace a write crossing the boundary
Write W enters A at epoch 12 and acquires the tenant gate before fencing begins. The fence waits while W commits at source position 901. The final barrier L* is at least 901, so B must apply W before activation at epoch 13. W's response may be lost; its idempotency result travels with the data.
Write X arrives at a stale router after A's gate closes. A rejects it without committing, the router refreshes ownership and retries on B after activation. The same request key is retained. A router redirect does not itself prove that an earlier attempt failed; destination deduplication resolves an already committed W retry.
A gate checked only at request entry would be unsafe if a transaction could commit after the final barrier. Holding the gate lock through commit is what makes the barrier meaningful. Long-running transactions need a timeout policy before cutover, not an emergency assumption that they finished.
6. Validate data and read behavior
Compare row counts, range checksums and business invariants at aligned snapshot or log positions. Counts alone miss changed values, and comparisons against a moving source can report false differences. Inspect representative rows and deletion cases, then verify the final replay barrier.
Keep reads on A during ordinary copying. During cutover, route consistency-sensitive reads with ownership and applied-position requirements. After B begins accepting new writes, A is stale unless reverse replication has caught up; do not serve strong reads there merely because its data looks complete.
Tag cached results with tenant data/ownership version or invalidate them on activation. A session requiring read-your-writes must reach a destination that includes its commit; preserve or translate that requirement across the migration barrier rather than comparing unrelated shard log offsets.
7. Distinguish cancellation from rollback
Before B accepts application writes, cancellation can keep or restore A as owner through the same authoritative state machine, after proving B never became active. Serialize cancellation against activation, durably invalidate the target activation epoch at B, and confirm its application gate is closed before reopening A. A delayed activation command from an old controller must fail; a one-time observation that B is currently closed is insufficient. Once B accepts a new write, simply changing the route back to A loses that write.
For post-activation rollback, retain a tested B-to-A replication path with loop prevention and compatible schema. Fence B, establish its final barrier, catch A up through it, validate, then activate A at a newer epoch. Without that path, prefer repairing B or conducting a new migration; retaining the old files is not a complete rollback plan.
Keep source data through an agreed observation and recovery window. Cleanup requires evidence that routing, consumers, backups and rollback obligations no longer depend on A. Track duplicate-operation outcomes, rejected stale writers, migration phase age, oldest retained log and per-tenant latency throughout.
8. Failure tests and self-review
Inject a copy-worker crash, duplicate replay, deletion during copying, source failover, lost registry response, stale router, long transaction and target failure immediately after its first new write. Verify every acknowledged operation remains present exactly once in business state and that application gates never overlap.
Follow-ups: What if log retention runs out? Why is zero displayed lag not enough? Can rollback happen after B has accepted writes? How does an old background job discover its stale ownership? What sets the maximum write pause?
Score 0–2 each for snapshot/log consistency, replay and retry identity, storage-enforced fencing, read/validation semantics and reversible cutover. Strong answers state the normal pause budget and the failure conditions that can prolong unavailability while preserving single ownership.
Sources and next steps
- Vitess 24: Reshard workflow — copying, validation, traffic switching and reverse replication.
The tenant gate protocol, failure timeline and sizing calculations are original. This conceptual design is not a claim that arbitrary databases implement these guarantees automatically. See change-data capture for transport details.
Continue your preparation
Choose a connected learning path or browse more System design questions. Read our editorial method and attribution policy.