Sharding & replication
medium 30 minScale 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 hear | Say | Because |
|---|---|---|
| Data beyond one machine | Shard by the key in the main query | One shard per request |
| Too many reads | Cache and replicas first | Reads do not need shards |
| Too many writes | Shards | Each replica repeats every write |
| Must not lose a payment | Wait for a replica | Asynchronous copies can miss it |
| Profile shows the old name | Read your own writes | A 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.