Data building blocks
Choosing a shard key
A good shard key has high cardinality, even traffic, query locality and stability. Choose it from your most common queries, and plan for the single hot value that breaks even a good key.
The shard key matters more than the partitioning algorithm. It decides which queries stay on one shard and whether any single shard ends up with most of the load. Choose it before you write any code, and choose it from the queries you run most often.
Concept
Before you start
- Cardinality: the number of distinct values a key can take. A key with 3 values cannot spread data across 64 shards.
- Query locality: when a query includes the shard key, it touches one shard instead of all of them.
The big idea
Picking a shard key is like deciding how to file a company's paperwork. File by customer, and each customer's papers sit in one drawer. File by month, and every new paper goes in this month's drawer, which becomes the busiest drawer in the office.
The picture breaks when you need to look across customers. That cost is covered in secondary indexes when sharded.
Four tests
A good shard key has four properties.
- High cardinality. Far more distinct values than shards. Sharding by
countrygives at most about 200 partitions, and one of them is the United States. - Even traffic. Rows are not enough. A key with even row counts but skewed traffic still makes hot shards.
- Query locality. The most frequent queries include the key, so they touch one shard.
- Stability. Changing a record's key means moving it to another shard.
| System | Good key | Why | Bad key | Why |
|---|---|---|---|---|
| Chat messages | conversation_id | Fetching a conversation hits one shard | message_id | Every conversation read scatters |
| Multi-tenant SaaS | tenant_id | Tenant queries and joins stay local | created_at | All writes hit the newest shard |
| Social graph | follower_id | "Who do I follow" is local | followee_id alone | A celebrity's shard holds millions of edges |
| Orders | customer_id | "My orders" is local | status | Five values, massively uneven |
Walking through a key choice
Take an orders table sharded by customer_id across 64 shards.
"My orders" for customer 42 → one shard, fast
"All orders with SKU X" → all 64 shards, mergedIf "all orders with SKU X" is rare, the design may be fine. If it runs on every product page, the design is wrong. Check the query list before you commit to a key.
The single hot value
Even a perfect key fails when one value is hot: a celebrity account, a viral post, or a flash-sale product. In DynamoDB, a single partition key tops out at roughly 1,000 writes per second, no matter how much capacity the table has.
Fixes, from least to most invasive:
- Cache reads in front of the shard. Hot reads are the easy case. See caching.
- Salt write-hot keys. Write to
post:123#0throughpost:123#9, chosen at random, so writes spread over 10 partitions. Reads must fetch all 10 and combine them, for a like counter by summing. - Isolate the key. Give it dedicated capacity, or treat it specially in the application.
Salting multiplies read cost by 10, so salt only keys you know are hot. For how feeds handle celebrities, see news feed.
Common mistakes
- Choosing a low-cardinality key such as country or status.
- Choosing by row count. Traffic is what overloads a shard.
- Sharding by time. Writes then all hit the newest shard.
- Assuming you can change the key later cheaply. Changing it means moving every row.
Check yourself
1. Why is status a poor shard key for orders?
Answer: It has very few values, so data and writes pile into a handful of shards.
2. You salt a like counter into 10 keys. What does a read cost now?
Answer: Ten reads, one per suffix, combined by summing. The read cost goes up tenfold.
Key takeaways
- Choose the shard key from your most frequent queries.
- Test every candidate for cardinality, even traffic, query locality and stability.
- A single hot value needs its own fix: cache it, salt it or isolate it.
- Salting multiplies read work, so salt only the keys you know are hot.
Further learning
Next, see what happens to queries on other fields in secondary indexes when sharded.
Related
Sharding and partitioning
Split data across machines by choosing a shard key, hashing or ranging it, routing requests, handling hot spots, resharding without downtime and generating IDs that stay unique.
Hot keys and celebrities
One key or user can attract far more traffic than the rest. Sharding cannot spread a single key, so you split it, cache it, or change how updates fan out.
Multi-tenant SaaS architecture
Designing one system for many customers, covering isolation models, tenant scoping, noisy neighbors, tenant-aware sharding and per-tenant cost.