System Design Playbook
05 · Data & databases8 min read

What is database sharding?

When one database server can't hold all the data or handle all the writes, you split the data across several servers. How rows get routed, why the shard key is the most important decision, and what you give up.

In one sentence

Sharding splits one big database into several independent databases (shards), each holding a slice of the rows, so storage and writes scale with the number of machines.

Why shard at all?

A single database server has limits: disk size, memory for hot data, and above all write throughput. Read replicas help with reads, but every write still goes to one primary. When that primary is the bottleneck, you split the data so each server owns only part of it.

Instagram's user data, Discord's messages and Uber's trips are all sharded: no single machine could hold or write them.

Routing: which shard owns this row?

Every row has a shard key (for example user_id). A routing function maps that key to a shard. Try both routing strategies below and add or remove shards.

Spreading users across shards
Routing
Shard key
Shard 1u1037u1111u1148u1222u1370u1407u1481u1740Shard 2u1333u1444u1555u1629u1703u1777u1851Shard 3u1000u1074u1185u1259u1296u1518u1592u1666u1814
Shards
3
Keys moved
0/24 · 0%
Largest shard
9 users
Balance
even
With mod N, changing the number of shards changes almost every key's home: most data has to move.
Amber keys are the ones that had to move to another shard after the last change.
StrategyHow it worksTrade-off
Hash mod Nshard = hash(user_id) % NEven spread, but changing N moves almost every key
Consistent hashingKeys and shards sit on a ring; a key belongs to the next shard clockwiseAdding a shard moves only ~1/N of the keys. Used by Cassandra and DynamoDB
Rangeuser_id 1–1M → shard 1, 1M–2M → shard 2Range queries are easy, but new users all hit the last shard
Directory / lookupA table says which shard each tenant lives onFully flexible, but the directory becomes a critical dependency

The shard key is the big decision

A good shard key has many distinct values, spreads load evenly and matches your most common queries, so they hit one shard instead of all of them.

Good keys

  • user_id for a social app: each user's data lives together
  • tenant_id for a B2B SaaS (if no tenant is gigantic)
  • chat_id for messages

Bad keys

  • country: most users in one place → a hot shard
  • created_at: all new writes go to one shard
  • A key your main queries don't include → every query fans out to all shards

What sharding costs you

  • Cross-shard queries ("top 10 posts globally") must ask every shard and merge results.
  • Joins and transactions across shards are slow or impossible; you design data so related rows share a shard.
  • Resharding (moving data when you add shards) is a big operation. Consistent hashing makes it smaller.
  • Unique IDs can't come from one auto-increment anymore: use UUIDs or ID generators like Snowflake.
  • Much more operational work: backups, migrations and monitoring times N.

That's why sharding is usually the last step: first indexes, caching, read replicas and vertical scaling. Managed databases like DynamoDB, Cassandra, Vitess or Citus do the sharding for you.

Key takeaways

  • 01Sharding spreads rows across independent databases to scale storage and writes.
  • 02The shard key decides everything: even distribution and single-shard queries.
  • 03Consistent hashing keeps data movement small when shards are added.
  • 04Cross-shard queries, joins and resharding are the price; shard only when you must.

Check your understanding

1 of 3

What does sharding split across several databases?

Related concepts