Quick revision
The short answer
A good shard key has high cardinality, spreads reads and writes evenly, appears in most queries so they go to one shard, keeps related data together (such as all rows of one tenant) and never changes. A monotonically increasing key such as created_at or an auto-increment id is a bad range shard key because every new row has the largest value so far, so all inserts go to the last shard, which becomes a hot spot while the others sit idle. Hashing the key fixes the spread but gives up efficient range queries on it.
What to remember
In this answer, we saw that a good shard key has high cardinality, spreads reads and writes evenly, appears in most queries, keeps related data together and never changes. A monotonically increasing key such as an auto-increment id or created_at fails with range sharding because every new row has the largest value, so all inserts and most reads hit the last shard. Hashing spreads the load but makes range queries on the key touch every shard. Whatever we pick, we check skew against real data first.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
A good shard key has high cardinality, spreads reads and writes evenly, appears in most queries so they go to one shard, keeps related data together (such as all rows of one tenant) and never changes. A monotonically increasing key such as created_at or an auto-increment id is a bad range shard key because every new row has the largest value so far, so all inserts go to the last shard, which becomes a hot spot while the others sit idle. Hashing the key fixes the spread but gives up efficient range queries on it.
Why this question is asked
Choosing the shard key is the most consequential decision in a sharded design, because it is the hardest one to change later: changing it means moving almost every row. Interviewers ask this question to check that we can reason about load distribution and query patterns together, and the monotonic key trap is the classic example of a key that looks natural and fails under load. In this answer, we will recap what the shard key does, go through the properties of a good key, see exactly why a growing key creates a hot spot with range sharding, look at what hashing fixes and what it costs, and end with how to check a key against real data.
1A Quick Recap of the Shard Key
In a sharded database, the shard key is the column, or set of columns, whose value decides which shard stores a row. The router, either our application code or a proxy, computes the shard from the key for every insert and every query. A query that includes the key goes to exactly one shard. A query without it must be sent to every shard and the results merged, which is slower and puts load everywhere. So the key decides both where data lives and which queries are cheap.
2The Properties of a Good Shard Key
High cardinality comes first: the key needs many distinct values, so it can be divided finely. A country column has about 200 values, and if most customers are in one country, that shard is huge. A user_id with 100 million values can be divided as finely as we like.
Even spread is next, for both writes and reads. Many distinct values are not enough if traffic concentrates on a few of them. Then the key should appear in most queries, so they route to one shard: if 95 percent of our queries say WHERE tenant_id = ?, tenant_id is a natural candidate. It should keep related data together: if all rows of one tenant share the key, a tenant's joins and transactions stay on one shard. And it must be stable: the key of a row should never change, because changing it means deleting the row on one shard and inserting it on another, which is a cross-shard operation.
| Candidate | Cardinality | Spread | In most queries | Verdict |
|---|---|---|---|---|
| tenant_id | High (50,000) | Uneven if a few tenants are huge | Yes | Good, with a directory |
| user_id | Very high | Even | Yes for user data | Good for user-centric apps |
| country | About 200 | Very skewed | Sometimes | Poor |
| created_at (range) | High | All new writes on one shard | Often | Poor for range sharding |
| order status | 5 or so | Skewed, and it changes | Sometimes | Poor: low and unstable |
3Why a Growing Key Creates a Hot Spot
With range sharding, each shard owns a continuous range of the key. Suppose we shard orders by auto-increment id: shard 1 owns ids 1 to 10 million, shard 2 owns 10 million to 20 million, shard 3 owns 20 million and up. Every new order gets the next id, which is always the largest so far, so every new order goes to shard 3. Shards 1 and 2 receive no inserts at all.
The same happens with created_at: every new row has a timestamp of now, so it lands in the range that contains now. And because recent data is usually the most read too (today's orders, this week's events), the last shard takes most of the reads as well. We end up paying for many shards while one does nearly all the work, which is the opposite of why we sharded.
Notice that splitting the last shard does not solve it either. If we split shard 3 into two ranges, the newer range immediately becomes the new hot spot. The problem is not the size of the range but the fact that all new values fall at the end.
4What Hashing Fixes and What It Costs
If we hash the key before choosing the shard, consecutive ids land on unrelated shards. Order 20,000,001 might go to shard 5, order 20,000,002 to shard 1, and so on, so inserts spread evenly across all shards. Hashing is the standard fix for a monotonic key.
The cost is range queries on that key. 'Orders with id between 20,000,000 and 20,001,000' or 'events from the last hour by created_at' were cheap with ranges, because they touched one shard. With hashing, the matching rows are scattered across every shard, so the query goes to all of them. That is fine if such queries are rare or are served by another system, such as an analytics store. Another option is a compound approach: shard by a key that spreads well, such as tenant_id or user_id, and keep created_at as an indexed column within each shard, so 'this user's orders from last week' goes to one shard and uses a local index range.
5Check Skew With Real Data
A key that looks good on paper can still be skewed in practice, so before deciding we measure. We take production data, or a recent sample, and compute how rows and traffic would be distributed under the candidate key and strategy. We look at the largest share any single key value holds, and the largest share any shard would hold. If one tenant has 8 percent of all rows, no hash can put it on less than one shard, so with 32 shards that shard would carry about three times the average load. We also look at traffic, not only rows: query logs show which key values are most read.
6Follow-up questions
What would you choose instead of created_at for an events table?
Usually the entity the events belong to, such as user_id, device_id or tenant_id, with created_at as an indexed column inside each shard. Queries are almost always about one entity's recent events, so they route to one shard and read a local index range.
Can a compound shard key help?
Yes. A key such as (tenant_id, user_id) hashed together spreads a large tenant across shards while keeping each user's data together. The cost is that queries for a whole tenant then touch several shards, so it suits tenants that are too large for one shard.
Is a random UUID a good shard key?
It spreads perfectly and never needs coordination, so it is good for spread. But it carries no meaning, so it keeps no related data together and rarely appears in queries other than lookups by id. It works for entities that are always fetched by their own id.
7Conclusion
In this answer, we saw that a good shard key has high cardinality, spreads reads and writes evenly, appears in most queries, keeps related data together and never changes. A monotonically increasing key such as an auto-increment id or created_at fails with range sharding because every new row has the largest value, so all inserts and most reads hit the last shard. Hashing spreads the load but makes range queries on the key touch every shard. Whatever we pick, we check skew against real data first.
Open with
A good shard key has high cardinality, so the data can be divided finely, and it spreads both writes and reads evenly across shards. It should appear in most queries, so they go to one shard instead of all of them, and it should keep related data together, like all rows of one tenant or one user, so joins and transactions stay on one shard.
Then, if they want more
It also has to be stable, because changing a row's shard key means moving it between shards. A monotonically increasing key like an auto-increment id or created_at is a bad key for range sharding because each shard owns a continuous range, and every new row has the biggest value so far, so all inserts land on the last shard.
Recent data is also the most read, so that one shard becomes a hot spot while the others are idle, and splitting it just moves the hot spot to the new last range.
Hashing the key fixes the spread, because consecutive ids go to unrelated shards, but then range queries on the key, like 'events in the last hour', have to go to every shard. In practice I would usually shard by the owning entity, like tenant_id or user_id, and keep created_at as an indexed column inside each shard.
Before deciding, I would measure skew on real data: the share of rows and traffic held by the biggest key values and the resulting load per shard.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- PostgreSQL documentation, MySQL reference manual
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What makes a good shard key? Why is a monotonically increasing key such as created_at or...": https://scrivio-interview-library.netlify.app/databases/partitioning-and-sharding/1092edebad/