Try
↑ ↓ move↵ openesc close

Interview prep / Partitioning

Senior · Partitioning · System design interview question

A single 4 TB database is at its write limit. You must move to 16 shards without downtime. Lay out the migration plan and where it is most likely to go wrong.

Sketch your own answer first, then compare it with the one below.

A strong answer

First choose the shard key and the key to shard rule. Use many logical shards, for example 256 or 1024 mapped onto the 16 machines, so a later move is a change of assignment and not another rehash. Check the chosen key against real traffic for skew, and find which queries and transactions would cross shards.

Then build the new cluster beside the old one and copy. Take a consistent snapshot and load each shard with its slice of the data, while the old database keeps serving. Keep the new shards in sync by streaming the old database's change log into them, routed by the new rule. Lag falls to seconds.

Verify before cutting over. Compare row counts and checksums per shard, and replay a sample of real reads against the new cluster to compare results. Shift reads first, a percent at a time, using the routing layer, while writes still go to the old database.

Cut the writes over briefly. Stop writes on the old database, wait for the stream to drain, flip routing to the new shards, and resume. This is typically a pause of seconds. Keep the old database in sync in reverse for a while so you can roll back.

Likely failures: cross-shard queries and transactions that assumed one database, auto-increment ids that collide across shards, sequences and unique constraints that were global, a skewed key that still makes a hot shard, and the copy load slowing the live system. Rehearse the whole thing on a copy first.

Sources

Learn it by running it

The partitioning lesson explains the mechanism behind this question, and its lab lets you change the parameters and watch the effect.

Your progress is saved in this browser. Sign in to keep it on every device. Privacy · Terms