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.
| Strategy | How it works | Trade-off |
|---|---|---|
| Hash mod N | shard = hash(user_id) % N | Even spread, but changing N moves almost every key |
| Consistent hashing | Keys and shards sit on a ring; a key belongs to the next shard clockwise | Adding a shard moves only ~1/N of the keys. Used by Cassandra and DynamoDB |
| Range | user_id 1–1M → shard 1, 1M–2M → shard 2 | Range queries are easy, but new users all hit the last shard |
| Directory / lookup | A table says which shard each tenant lives on | Fully flexible, but the directory becomes a critical dependency |
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
Partitioning strategies
Partitioning divides one large table into smaller pieces by a rule (by date, by region, by hash, or by columns), so each query only reads the pieces that can contain its answer.
8 min read →Horizontal vs vertical scaling
Vertical scaling means a bigger server; horizontal scaling means more servers sharing the work. Vertical is simpler, horizontal goes further.
6 min read →Read replicas
A read replica is a copy of the database that receives every change from the primary and answers read queries, so you can add read capacity without touching writes.
7 min read →Consistent hashing
Put servers and keys on the same hash ring; each key belongs to the next server clockwise. When a server joins or leaves, only the keys of one arc move, about 1/N of them, instead of almost all.
6 min read →