Sharding and consistent hashing
Updated Nov 10, 2026 · 4 min read
Sharding splits one database into many smaller ones, called shards, each holding a slice of the rows. You do it when one machine can no longer hold the data or keep up with the writes. Replicas can spread reads, but every write still lands on one primary. Sharding is how you spread the writes too. The price is that every query now has to find the right shard, and some can no longer be answered by one machine.
The shard key
Each row has a shard key, and a router uses it to pick the shard. The key decides almost everything about how well sharding works. A good key appears in your most common queries, so they go to one shard. It has many distinct values, so the load spreads. And no single value gets a big share of the traffic.
- For a chat app, the conversation id. Loading a chat touches one shard.
- For a multi-tenant SaaS app, the tenant id. Every query for one customer stays on one shard.
- For a social graph, the user id. A user’s profile, posts and settings live together.
Hash or range
| Hash sharding | Range sharding | |
|---|---|---|
| Places a row by | A hash of the key | Which range the key falls in |
| Spreads writes | Evenly | Badly when new keys are all at one end, like timestamps |
| Range scans | Must ask every shard | Touch one or a few shards |
| Adding a shard | Moves some keys (see below) | Split one range in two |
Range sharding on time is the classic trap. If orders are sharded by month, every new order goes to this month’s shard, which runs flat out while the others sit idle. Hashing spreads new writes evenly, at the cost of cheap range scans.
Why hash mod N hurts
The simple way to hash is shard = hash(key) mod N. It spreads keys evenly, until N changes. Going from 10 shards to 11 changes the answer for about 10 in 11 keys, so nearly all of your data has to move at once.
Consistent hashing
Consistent hashing places both keys and shards on a ring of hash values. Each key belongs to the next shard clockwise. Adding a shard only takes keys from its new neighbour, so about 1 in N + 1 keys move and the rest stay put. Removing one hands its keys to the next shard along.
With one point per shard, the gaps on the ring are uneven, so some shards own far more than others. Virtual nodes fix that. Each shard takes 100 or more points on the ring, so its share evens out, and a new shard takes a little from every other shard instead of a lot from one.
Hot shards
Even a perfect hash can’t split one key. If one celebrity post gets 40,000 likes a second and a shard handles 10,000 writes, that post’s shard is overloaded no matter how many shards you add. The fix is to split the key itself. Write each like to one of several sub-keys, like post:7#1 to post:7#8, which hash to different shards, and add the sub-counters up when reading. Replicas and caches don’t help here, because the problem is writes.
Cross-shard queries
A query without the shard key has to go to every shard and merge the answers. This is called scatter-gather. It works, but it costs every shard some work, and it’s as slow as the slowest shard. If each shard has a 1 in 100 chance of a slow reply, a query across 20 shards hits at least one slow shard about 18% of the time. Keep scatter-gather for rare queries, and for common ones keep a second copy of the data sharded by the other key.
Moving data without downtime
- Start writing every change to both the old and the new shard, so nothing written during the move is missed.
- Copy the existing rows across in the background.
- Compare the two copies, with counts and checksums, until they match.
- Move reads to the new shard, and watch errors and latency. If anything looks wrong, move them back.
- Stop writing to the old shard and delete the moved rows.
Every step can be undone until the last one. That’s the point. You never have a moment where the only copy of some rows is half written.