Scrivio Interview library
ScenarioAdvanced6 min read · updated 13 November 2026

A social app keeps 40 billion follow edges in a MySQL cluster sharded by hand on user id ranges. Several shards are at 90 percent disk, and the team must move to a new horizontally partitioned store while the app handles 30,000 follow and unfollow writes per second.

Plan the migration of the follow graph to the new store with no downtime and no lost or resurrected edges.

Keep MySQL authoritative at first and stream its changes with change data capture into a log such as Kafka, applying them to the new store idempotently, keyed by edge and carrying a version, so a reordered or replayed event can never resurrect a deleted edge. Backfill history from consistent snapshots that start from a recorded log position, keep unfollows as versioned tombstones so a late backfill row cannot reinsert them, and verify counts and sampled edge sets per user until mismatches are near zero. Then shift reads gradually from shadow to all traffic, and finally make the new store authoritative for writes while replicating back to MySQL as the rollback path.

Why this question is asked

Moving a live dataset is one of the riskiest jobs in backend engineering, because the data keeps changing while we copy it. With 40 billion edges and 30,000 writes per second, a copy takes days, and every follow and unfollow during those days must land correctly in the new store. The subtle failure is not losing an edge but resurrecting one: a user unfollows an account, and a week later the edge reappears because an old copy of it arrived late. Interviewers ask this to see whether we can sequence a migration so that correctness never depends on luck. In this answer, we will lay out the phases, design the change stream and the versioned apply, run the backfill, verify, shift reads and writes, and keep a way back at every step.

1The Shape of the Migration

A safe online migration has one rule: at every moment, exactly one store is authoritative, and the other is kept in step from it. At the start MySQL is authoritative. We build the new store as a follower, prove it matches, move reads to it, and only then move writes, at which point MySQL becomes the follower and our rollback path. Each phase can be paused or reversed without losing data.

Step throughPhases of the follow-graph migration
  1. Start CDC from MySQLrecord each shard's binlog position
  2. Backfill from snapshotstaken at those positions, applied with versions
  3. Verify continuouslycounts and sampled edges per user
  4. Shadow readsserve from MySQL, compare new store
  5. Shift reads 1% -> 10% -> 100%MySQL still authoritative for writes
  6. Mismatch rate near zero?
    YesFlip writes; replicate back to MySQLNoRepair, keep MySQL authoritative

6 steps in order. Play them, or step through them one at a time.

MySQL stays the source of truth until the last step, and remains the fallback after it.

The phases overlap deliberately. Change capture starts before the backfill, so every write during the copy is captured, and verification runs from the first day so problems surface early, when they are cheap to fix.

2Capturing Changes With Versions

Change data capture reads each MySQL shard's binary log, the ordered record of every committed change, with a tool such as Debezium, and publishes one event per change to Kafka. We key the events by edge, (source_id, target_id), so all changes to one edge go to the same partition and arrive in order. An unfollow becomes a delete event, which we keep as a tombstone rather than dropping.

Every event carries a version that increases for each change to the same edge. The binlog position works because one edge lives on one shard, so its changes appear in that shard's binlog in order; an explicit version column updated on every write is even clearer. The new store keeps the version with each edge, including deleted ones, and the apply rule is simple: write only if the incoming version is newer than the stored one.

Java
// new store row: (source, target, version, deleted, created_at)
void apply(EdgeEvent e) {
store.conditionalWrite(e.source(), e.target(),
row -> row == null || row.version() < e.version(), // only newer versions win
new Edge(e.source(), e.target(), e.version(), e.isDelete(), e.createdAt()));
}
// follow (v=1001) -> row v1001 live
// unfollow (v=1007) -> row v1007 deleted (tombstone kept)
// backfill row for the same edge from an older snapshot (v=990) -> ignored, stays deleted

The last line is the resurrection case. A snapshot taken before the unfollow still contains the edge, and if the backfill applies it after the tombstone, a store without versions would bring the edge back. With versions, the old row loses. This is why tombstones must be kept at least until the backfill and verification are finished; after that they can be compacted.

3Backfilling 40 Billion Edges

The backfill copies existing edges. For each MySQL shard we record the binlog position, take a consistent snapshot as of that position, for example from a replica with replication paused or with a consistent-read export, and write its rows into the new store through the same versioned apply, giving snapshot rows the version of the snapshot position. Change capture has been running from that position, so every change after the snapshot is in the stream, and every change before it is in the snapshot. Nothing falls between them.

Throughput matters at this size. At 300,000 rows per second across parallel workers, 40 billion edges take about 37 hours per copy, so the backfill runs for days if we include both directions and throttling to protect the shards that are near full disk. We throttle by shard and pause during peak hours, and the stream keeps the new store current throughout.

4Verifying Before Trusting

Verification compares the stores continuously, not once at the end. A job samples users, for example 100,000 per hour weighted towards active and large accounts, and compares their following and follower counts and the full sets of edges in both stores, or hashes of those sets for large accounts. Each mismatch is classified: in-flight changes that will catch up, bugs in the apply, or bad source data. Real mismatches are repaired by re-reading the edges from MySQL.

The cutover gate is a number agreed in advance, such as a mismatch rate below 1 in a million sampled edges for a full week, with every remaining mismatch explained. Without that gate, the decision to cut over becomes a feeling.

5Shifting Reads, Then Writes

Reads move first because they are reversible instantly. In shadow mode the service serves every read from MySQL and also queries the new store, comparing results and latency without affecting users. Then a feature flag sends 1 percent of real reads to the new store, then 10, then 100, watching latency and error rates at each step.

Writes move last. We stop writing to MySQL for a short moment per user range, or use the version to make the switch race-free, and from then on the service writes to the new store, which becomes authoritative. A reverse stream replicates its changes back into MySQL with the same versioned rule, so if the new store misbehaves in the first weeks, we can flip writes back without losing anything written in the meantime. Once the new store has run cleanly for an agreed period, we stop the reverse stream and retire MySQL.

Who is authoritative in each phase
PhaseWrites go toReads come fromRollback
Backfill and CDCMySQLMySQLdrop the new store
Shadow readsMySQLMySQL, new store comparedturn shadow off
Read shiftMySQLnew store 1% to 100%flag back to MySQL
Write cutovernew storenew storereverse stream keeps MySQL current
There is always a single authoritative store and a way back.

6Follow-up questions

Why not dual-write from the application to both stores?

Because the two writes are not atomic: a crash between them leaves the stores different, and two concurrent requests can apply in different orders in each store. CDC from the authoritative store gives a single ordered stream of what really committed, which is much easier to make correct.

How do you handle the shards at 90 percent disk during the migration?

We buy time first, for example by archiving old data or adding disk to the worst shards, because a full disk would stop writes entirely. The backfill reads from replicas where possible, so it adds little load on the primaries, and the throttle keeps the snapshot work from competing with production traffic.

What if a bug in the apply code is found after the read shift?

We move reads back to MySQL with the flag, fix the bug, and repair the affected edges by replaying the stream from a position before the bug or re-backfilling the affected users. Because MySQL is still authoritative at that point, no user data is at risk.

7Conclusion

In this answer, we planned a zero-downtime migration of 40 billion edges. MySQL stays authoritative while CDC streams its changes into the new store, applied idempotently with per-edge versions so that deletes, kept as tombstones, can never be resurrected by late backfill rows. The backfill starts from recorded binlog positions so nothing is missed, continuous verification sets a measurable gate, reads shift gradually from shadow to all traffic, and writes move last with reverse replication to MySQL as the rollback path.

What to say in the interviewAbout 80 seconds aloud

Open with

I would keep MySQL authoritative until the very end. First I start CDC on every shard, recording each binlog position, with Debezium into Kafka keyed by the edge, so each edge's changes stay in order.

Then, if they want more

The new store applies events idempotently with a version per edge, writing only if the version is newer, and unfollows are kept as versioned tombstones. Then I backfill from consistent snapshots taken at those recorded positions, through the same versioned apply.

That means nothing between snapshot and stream is lost, and a late snapshot row for an edge that was unfollowed has an older version and cannot resurrect it. I throttle the backfill so the near-full shards are not hurt.

From day one I verify by sampling users, comparing counts and edge sets in both stores, repairing differences, and I set a gate like a mismatch rate under one in a million for a week. Then reads move: shadow reads compared with MySQL, then 1, 10 and 100 percent behind a flag.

Finally writes move to the new store, with reverse replication into MySQL so I can roll back. When it has run cleanly for a while, I stop the reverse stream and retire MySQL.

About this answer

Last updated
13 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 official documentation for the version you use. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
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 "Plan the migration of the follow graph to the new store with no downtime and no lost or...": https://scrivio-interview-library.netlify.app/architecture/system-design-problems/b1183b3500/