Lab · Distributed data

Sharding

When one database can't hold all the data or take all the writes, you split rows across shards by a shard key. The key you pick decides whether load is even, which queries stay cheap, and how painful adding a shard will be.

Try it

Lab time 3.0 s
Shard router, hash(user_id)Shard routerhash(user_id)Shard 0, 23 rowsShard 023 rows8 writes/sShard 1, 31 rowsShard 131 rows10 writes/sShard 2, 22 rowsShard 222 rows8 writes/sShard 3, 24 rowsShard 324 rows8 writes/s

The lab is busy, so only some requests are drawn as dots, few enough to follow. The numbers still count every request.

  • Shard router — hash(user_id) — healthy
  • Shard 0 — 23 rows — healthy — 8 writes/s
  • Shard 1 — 31 rows — healthy — 10 writes/s
  • Shard 2 — 22 rows — healthy — 8 writes/s
  • Shard 3 — 24 rows — healthy — 8 writes/s
  • Write
  • Hot key
Hottest shard vs average
1.2×
Healthy: load is spread
Rows written
100
Reading one user's posts
1shard
how many shards to ask
Search posts by hashtag
4shards
not the shard key → ask all
Rows moved by last resize
—
press "Add shard"

Numbers add up:100 rows across shards = 100 rows writtenThe lab checks this sum as it runs, so a counting mistake would show here.

Controls

Shard key

Spread users evenly by hashing their id. Good balance, but a range query must ask every shard.

Workload
30/s
Who posts

A few users post a lot more than others.

Cluster

Writes per second by shard

  • Shard 08
  • Shard 110
  • Shard 28
  • Shard 38

The tick marks the average. A bar far past it is a hot shard.

Rows stored by shard

  • Shard 023
  • Shard 131
  • Shard 222
  • Shard 324

What just happened

Nothing yet. Start traffic or run an experiment.

What this model simplifies
  • Only writes are modelled; each shard's load is its writes per second over the last 2 s.
  • Resharding is instant here. Real migrations copy data in the background, double-write or redirect during the move, and verify before cutting over.
  • Each shard is a single node. In production every shard is also replicated.