Shard Key
The column or field used to determine which shard a piece of data belongs to. A bad shard key creates hot spots; a good one distributes data and queries evenly.
What is Shard Key?
In short
A shard key is the field (or set of fields) a database uses to decide which shard a row or document lives on. The database hashes or ranges that field's value to route every write and read to the correct partition, so the choice of shard key determines whether data and traffic spread evenly or pile up on one overloaded node.
What a shard key actually is
When a single database server can no longer hold all your data or handle all your traffic, you split the data across many servers. Each server holds one slice, called a shard. The shard key is the piece of data the system looks at to decide which slice a given record belongs to.
For example, if you shard a users table by user_id, then user_id is your shard key. Every time you insert or look up a user, the database takes that user_id, runs it through a routing rule, and that rule points to exactly one shard. The record never lives on two shards, and a point query for one user only touches one shard.
A shard key is usually one column, but it can be a compound key made of several columns, such as (tenant_id, created_at). The key is chosen once at design time and is expensive to change later, because changing it means rewriting where every row lives.
How the routing works under the hood
There are two common ways to turn a shard key value into a shard. Range-based sharding sorts the key and assigns contiguous ranges to shards. For example user_ids 1 to 1,000,000 go to shard A, 1,000,001 to 2,000,000 go to shard B. This makes range scans efficient but tends to send all the newest inserts to the last shard.
Hash-based sharding runs the key through a hash function and uses the result to pick a shard. A hashed user_id scatters records across all shards evenly, which kills hot spots on writes, but it destroys ordering so range scans now have to touch every shard.
Many systems add a layer of consistent hashing or a lookup table so they can add or remove shards without rehashing every record. MongoDB keeps a config server that maps key ranges (chunks) to shards. Vitess and Citus keep similar routing metadata. The application usually does not pick the shard itself; a router or proxy reads the shard key off the query and forwards it.
The hard rule is that the shard key has to be present in the query for the router to find the data in one hop. A query that filters only on email when the shard key is user_id cannot be routed, so it fans out to every shard and waits for all of them. That scatter-gather query is slow and scales badly.
Choosing one, and the trade-offs
A good shard key has three properties. It has high cardinality, meaning many distinct values, so data can spread thinly. It has even distribution, so no single value gets a disproportionate share of rows or requests. And it matches your most common query so those queries hit a single shard.
Bad keys create hot spots. Sharding by country puts all of India or the United States on one node. Sharding by a monotonically increasing timestamp or auto-increment id sends every new write to the same shard, so one machine takes all the load while the rest sit idle. Sharding by a low-cardinality status field like active or inactive jams everything into two shards.
The painful trade-off is that one key cannot serve every access pattern. If you shard orders by customer_id, looking up an order by order_id becomes a scatter-gather. Teams handle this with secondary indexes, a global lookup table mapping order_id to shard, or by denormalizing data into a second sharded copy keyed differently. Pick the key that serves your hottest, highest-volume query, and accept that the rarer queries will be slower.
A concrete example
Imagine a multi-tenant SaaS analytics product where each customer (tenant) has millions of events. A natural shard key is tenant_id. Every dashboard query already filters by the logged-in tenant, so those queries route to exactly one shard, and a tenant's data stays physically together.
The risk is a whale tenant. If one customer is 100 times larger than the rest, its single tenant_id value lands all its data on one shard and overloads it while small tenants share another shard with room to spare. The fix is a compound shard key like (tenant_id, event_bucket) that splits a huge tenant across multiple shards while keeping small tenants intact.
This is exactly the kind of decision that is cheap to get right on day one and brutally expensive to fix once you have terabytes spread across the wrong key. That is why shard key selection is treated as an upfront architecture decision, not an implementation detail.
Where it is used in production
MongoDB
Requires you to pick a shard key per collection; it splits data into chunks by key range and a config server routes queries to the right shard.
DynamoDB
The partition key is its shard key; AWS hashes it to place items, and a poorly chosen key creates a hot partition that throttles throughput.
Cassandra
Uses the first part of the primary key as the partition key, hashed and placed on the ring via consistent hashing to spread data across nodes.
Vitess
Powers YouTube and Slack sharding on top of MySQL; a VIndex maps the shard key column to keyspace shards so queries route to one MySQL instance.
Frequently asked questions
- What is the difference between a shard key and a primary key?
- A primary key uniquely identifies a row within a table. A shard key decides which physical shard that row lives on. They can be the same field, but they do not have to be. In Cassandra and DynamoDB the shard (partition) key is only part of the full primary key, and the rest of the key orders rows within that partition.
- Can I change the shard key later?
- Not easily. Changing the shard key means almost every row may need to move to a different shard, which usually requires a full data migration into a new cluster keyed differently. Most systems either forbid changing it or make it a heavy operation. Choose carefully up front based on your highest-volume query.
- What makes a shard key bad?
- Low cardinality (few distinct values), uneven distribution (one value dominates), or a monotonically increasing value like a timestamp or auto-increment id. All three create hot spots where one shard takes most of the load while others sit idle.
- Why are my queries slow even though I sharded the database?
- Most likely your queries do not include the shard key, so the router cannot target one shard and has to fan out to all of them (a scatter-gather). The query is then as slow as the slowest shard. Make sure your common queries filter on the shard key, or add a lookup table to translate other identifiers into a shard.
- Should I use a single-column or compound shard key?
- Use a single column when one field has high cardinality and matches your main query. Use a compound key like (tenant_id, bucket) when a single value would be too large for one shard, such as a whale tenant, so you can spread that value across several shards while keeping smaller values together.
Learn Shard Key hands-on
This page explains the idea. The full lesson lets you step through the ring as servers join and leave, read the implementation, and check yourself with a quiz. It is one of 760+ lessons in the System Design Masterclass, from your first API call to distributed consensus. Eleven Foundation lessons are free, no signup. Lifetime access is ₹499 in India or $7.99 worldwide, one payment, no subscription.
Related lessons
Lessons that touch on Shard Key as part of a larger topic.
See also
Related glossary terms you might want to look up next.
Sharding
Splitting a database into smaller pieces (shards) distributed across multiple servers. Each shard holds a subset of the data.
Consistent Hashing
A hashing technique where adding or removing servers only moves a small fraction of keys. Used by Amazon DynamoDB and Cassandra for data distribution.
Database Partitioning
Dividing a large table into smaller, more manageable pieces while keeping them in the same database. Sharding is partitioning across servers.
CAP Theorem
In a distributed system, you can only guarantee two of three: Consistency, Availability, and Partition tolerance. You must choose your trade-off.
Consensus
The process of getting multiple nodes in a distributed system to agree on a single value. The foundation of distributed databases and coordination services.
Paxos
A family of protocols for solving consensus in unreliable networks. Famously difficult to understand but mathematically proven correct.