Skip to main content
We started with a simple question: When a request arrives for a user, how does the system know which shard contains that user’s data? In the earlier example we partitioned users by taking the user’s numeric ID, dividing it by 4, and using the remainder to pick a shard:
  • ID % 4 == 0 → shard 0
  • ID % 4 == 1 → shard 1
  • ID % 4 == 2 → shard 2
  • ID % 4 == 3 → shard 3
The column used to decide where a row lives is called the shard key — in this example the shard key is the user ID. Why user ID? We didn’t choose it at random. You could shard by username, country, year joined, or any other column — whatever column you base the partitioning on becomes the shard key. When a request arrives for a user, the system applies the same rule (for numeric IDs, the modulo operation) and goes directly to the mapped shard to fetch the row. There is no searching and no central lookup. Example (pseudo-code):
Choosing the shard key is critical because it determines how data and load are distributed. Changing the shard key later is difficult and potentially requires moving every row in the cluster. For instance, switching from sharding by user ID to sharding by country would force many rows to be relocated so that all users from the same country end up on the same shard — a large, cluster-wide operation.
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.
Below are the five properties you should look for when choosing a shard key.
  1. High cardinality: many distinct values
    If you shard by country, 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_id typically has high cardinality (millions) and can be spread across many shards.
  2. 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.
  3. 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 by post_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.
A diagram illustrating scatter-gather database sharding: a user labeled "Leo" sends requests to an app that routes them to four shards (Shard 0–3). Each shard shows a sample table of posts (post_id, user_id, text) and arrows indicate the app's access to each shard.
Your shard key should match the dominant access patterns so most queries can be served by a single shard instead of by scatter-gather.
  1. 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 by username and users can rename, each rename requires moving rows. user_id is 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.
  1. 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_id for users, post_id for 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.
Why numeric IDs helped in the examples Our simple 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:
Hashing lets you use arbitrary shard keys (e.g., 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.
Links and references
A diagram explaining database sharding where the shard key is the user ID and IDs are assigned to four shards using ID mod 4. Example user tables (ID, username, country, joined) are shown under shards 0–3.

Watch Video