Pick the key that decides where data lives, avoid one overloaded shard, and grow without downtime.
The shard key is the field that decides which shard owns a row. It is the most important choice in a sharded system, and one of the hardest to change later.
The page on Sharding explained why we split data across many databases. This page goes deeper on three questions: which field should decide the split, what happens when one shard gets too busy, and how do we add shards later.
A shard key is chosen once, but it shapes everything: where each write goes, which queries are fast, and which are painful. A query that includes the shard key goes to one shard. A query that does not has to ask every shard.
| Key | Spread | Watch out for |
|---|---|---|
| user_id or customer_id (hashed) | Even for ordinary users | One huge customer or a celebrity can still be hot |
| Timestamp or auto-increment id (range) | Bad: all new rows go to the newest range | A classic hot shard on writes |
| Country or region | Poor: only a handful of values | Cannot use more shards than values, and sizes differ a lot |
| A random id (UUID, hashed) | Excellent | Related rows are scattered, and every list query must ask all shards |
| Composite key (tenant_id + hash of id) | Good, keeps a tenant near each other | More design and a more complex router |
| Key plus a salt suffix (user#0 to user#7) | Spreads one hot key over several shards | Reading that key means asking every bucket and merging |
| Mapping | How it works | Adding a shard |
|---|---|---|
| hash(key) mod N | The remainder after dividing by the shard count | Changes the remainder for almost every key, so most data moves |
| Range | Each shard owns a block of key values | Split one range in two: only that range moves |
| Consistent hashing | Keys and shards share a ring. A key goes to the next shard clockwise | About 1 / (N + 1) of the keys move, and only to the new shard |
| Many small fixed partitions | Create thousands of partitions up front and assign them to machines | Move whole partitions to the new machine, and the key mapping never changes |
| Directory (lookup table) | A table says where each key (or tenant) lives | Move one tenant at a time and update the table |
A teaching model with fixed data and no random numbers. Part 1 replays 1000 writes: a quarter come from one popular user, the rest from 199 ordinary users, with 60% of users in one country. Part 2 hashes 2000 keys with FNV-1a and gives each shard 100 points on the ring. Real systems differ in the details, but the shape of the result is the same.
user_7#0 to user_7#7 at random (or by a counter) so one user's writes spread over up to 8 shards. The cost is on the read side: to read user 7 you ask all 8 buckets and merge. Salt only the few keys you know are hot.| Term | Meaning in easy words |
|---|---|
| Shard key | The field (or fields) that decide which shard stores a row |
| Cardinality | How many different values a field has |
| Hot shard | A shard that gets far more load than the others |
| Hot key | One key that gets a huge share of the requests |
| Skew | Uneven spread of data or traffic |
| Range sharding | Each shard owns a block of key values, so range queries are easy but new rows can pile up |
| Hash sharding | A hash of the key picks the shard, which spreads keys evenly |
| Salt (bucket suffix) | A small extra part added to a key to spread one hot key over several shards |
| Resharding | Changing the number of shards and moving data to match |
| Virtual node or partition | A small logical slice assigned to a machine, so data moves in small pieces |
| Scatter-gather | Asking every shard and merging the answers |
| Choice | We gain | We lose |
|---|---|---|
| Hash the key | Even spread of keys | Range scans and ordered reads must ask every shard |
| Range the key | Fast range scans and easy range splits | A hot shard on growing keys |
| Co-locate related tables on one key | Local joins and local transactions | A big entity can make one shard too big |
| Salt a hot key | Writes spread over several shards | Reads must merge several buckets |
| Many small partitions | Cheap rebalancing | More metadata to track, and more files or connections |
| Directory lookup | Total freedom to place tenants | A new component that must be fast and always up |
hash mod N and then growing. Changing N moves almost every key.