Skip to content
StackDecode
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.

4911189728339475130859398885272411989603859464713934689N1: 8N2: 8N3: 3N4: 11
  • 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

  1. 1Hash the key
  2. 2Take it modulo the number of shards
  3. 3Route to that shard
  4. 4Rebalance when shards change

Where it is used

Even spread of user data.

Watch out for

Changing the shard count reshuffles most keys.

Related