System Design
5. Advanced Database ArchitectureAnimated
Hash-Based Sharding
Shard chosen by hashing the key.
The idea
Hashing spreads keys evenly, avoiding hot spots, but range scans touch every shard.
Watch it work
Press play, pause, or step through. Some animations have settings you can change.
- outlined = moved key
Hash-based: shard = hash(key) mod N spreads keys evenly, but range queries need every shard.
- nodes:
- 4
- moved:
- 0
1/2
Each square is a key; its colour is the shard that stores it.
Step by step
- 1Hash the key
- 2Take it modulo the number of shards
- 3Route to that shard
- 4Rebalance when shards change
Where it is used
Even spread of user data.
Watch out for
Changing the shard count reshuffles most keys.