Skip to content
StackDecode
System Design
5. Advanced Database ArchitectureAnimated

Database Sharding

Splitting data across multiple databases.

The idea

Each shard holds part of the data. This scales storage and writes beyond one machine.

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. 1Choose a shard key
  2. 2Route each row to a shard
  3. 3Queries go to the right shard
  4. 4Add shards as data grows

Where it is used

Very large datasets.

Watch out for

Cross-shard queries and rebalancing are hard.

Related