- ID % 4 == 0 → shard 0
- ID % 4 == 1 → shard 1
- ID % 4 == 2 → shard 2
- ID % 4 == 3 → shard 3
A shard key is simply the column (or expression) used to partition rows across database shards. The mapping must be deterministic so the application can route queries directly to the correct shard without searching.
-
High cardinality: many distinct values
If you shard bycountry, there are only ~200 possible values but you might want 64+ shards. That forces many countries per shard and creates hotspots for populous countries (India, China) while other shards stay nearly empty.user_idtypically has high cardinality (millions) and can be spread across many shards. -
Uniform distribution: even spread across shards
Balanced load is essential. If you shard by signup year and most users signed up this year, that shard will be overloaded while older-year shards remain idle. Using numeric IDs combined with modulo or hashing spreads new signups across all shards to keep load balanced. -
Query-pattern alignment: minimize scatter-gather
Shard keys should match how your application reads data. If common queries are “all posts by this user” and posts are sharded bypost_id, those queries become scatter-gather: the application must query every shard and merge results. Aligning your shard key to typical queries lets you route directly to one shard instead.

- Immutability: avoid keys that change
The shard key determines which shard stores a row. If that value changes, the row must be moved to the target shard, creating risk for partial failures and inconsistency. For example, if you shard byusernameand users can rename, each rename requires moving rows.user_idis commonly immutable and avoids this problem.
Changing a shard key is operationally expensive and risky. If the shard key can change for rows, your system must handle moving data and keeping reads/writes consistent during the migration.
- Co-location: keep related data together
Real systems have multiple related tables — users, posts, comments, likes. If those tables use different shard keys (e.g.,user_idfor users,post_idfor posts), a single logical operation (user profile + all posts) may require contacting many shards. Sharding related tables by the same key (co-location) improves performance and reduces cross-shard joins.
ID % 4 mapping works because IDs are numeric. But shard keys need not be numbers — they can be strings (emails, usernames). For arbitrary values we need a deterministic mapping to shard numbers that also produces an even spread. The general approach is hashing.
Hashing maps any value (string or number) to a fixed-size integer, and then you take that result modulo the number of shards:
email) while still spreading data evenly. For many systems, a cryptographic or well-distributed non-cryptographic hash (e.g., MD5, SHA-1, or MurmurHash) is sufficient to avoid hotspots.
A note on re-sharding and consistent hashing
When you add or remove shards, a naive modulo-based mapping will require moving a large portion of data. Consistent hashing is a technique that reduces the number of keys that must be relocated when the number of shards changes. If you expect frequent resharding, consider consistent hashing or another approach designed to minimize data movement.
Summary
- The shard key is the column or expression used to partition data across shards.
- Choose a shard key with high cardinality, uniform distribution, alignment with common queries, immutability, and co-location of related data.
- Use hashing to map arbitrary values to shard numbers.
- Changing the shard key or resizing shards is expensive, so design with future growth and resharding plans in mind.
- Database Sharding (Wikipedia)
- Consistent hashing — Wikipedia
- Designing Data-Intensive Applications — Partitioning and Sharding
