AlgoViz

Sharding & replication

medium 30 min

Scale a database past one machine by splitting rows over shards, and keep it available by copying every shard.

When it comes up

The moments this idea belongs in a design interview, and the moments it does not.

Signals

When to bring it up

You hearSayBecause
Data beyond one machineShard by the key in the main queryOne shard per request
Too many readsCache and replicas firstReads do not need shards
Too many writesShardsEach replica repeats every write
Must not lose a paymentWait for a replicaAsynchronous copies can miss it
Profile shows the old nameRead your own writesA follower is behind

You must grow from 8 to 12 database machines. How did you place the keys?

Breakshash(key) % 8
How
Changing 8 to 12 gives most keys a new home.
Limit
Two thirds of the rows move at once.
Choose when
Only if the number never changes.
WorksA hash ring with virtual nodes
How
New nodes take pieces from the others.
Limit
The ring and its tokens must be managed.
Choose when
The store offers it, as Cassandra does.
Strong1,024 logical shards and a map
How
Move about a third of the logical shards, one at a time.
Limit
The map must be available and versioned.
Choose when
You run ordinary databases.