Hot Shards, Resharding and Choosing a Shard Key
Overview
Sharding fixes capacity problems but creates new ones. Some shards get more traffic than others, queries that span shards become slow, and growing means moving data.
This lesson explains these problems and how a good shard key avoids most of them.
Detailed Notes
What goes wrong with sharding
Three classic problems:
- Hot shard (hot spot): one shard gets far more traffic than the rest.
- Cross-shard queries: a query needs data from several shards.
- Resharding: changing the number of shards or how data is split.
The problem it solves
Sharding is only useful if work is spread evenly and most queries touch one shard. These three problems are how that goal fails, so a good design plans for them from the start.
How it works
Hot shards. Causes include a bad shard key (for example sharding by country when most users are in one country), a very popular item (a celebrity account), or time-based keys where all new writes go to the latest shard. Fixes:
- Pick a key with many different values and even use.
- Add randomness, such as
celebrity_id + random 0-9, to split one hot key over several shards. Reads then gather from those pieces. - Cache hot items (see the Caching chapter).
- Move or split the hot shard.
Cross-shard queries. If you shard orders by user_id, "orders for user 42" hits one shard. But "all orders over $100 today" must ask every shard and combine the answers. This is called scatter-gather. It is slower, and one slow shard slows the whole answer. Joins across shards are even harder. Common fixes: always include the shard key in queries, keep related data together on the same shard (orders with their user), copy small shared tables to every shard, or keep a separate search or analytics store for global queries.
Resharding. Eventually you need more shards. Moving data while the system is live is delicate: you copy in the background, keep new writes in both places, then switch over. Consistent hashing and many small virtual shards (say 1,000 logical shards on 10 machines, so growing just moves whole logical shards) make this easier. Plan early.
Choosing a shard key. A good key:
- Has high cardinality (many different values).
- Spreads traffic evenly.
- Appears in most queries.
- Rarely changes.
user_id is usually a good key for user-centred apps. A timestamp alone is a poor key for writes.
A simple example
A supermarket assigns customers to checkout lanes by the first letter of their surname. Lane "S" is jammed while lane "X" is empty: a hot shard. And to count everyone who bought milk, you must ask every lane: scatter-gather.
System design example
A social network shards posts by user_id. Loading one user's posts is a single-shard query. When a celebrity with 50 million followers posts, their shard receives a flood of reads, so the team caches those posts and splits that user's data across several shards. Their global "trending posts" page cannot be done by scatter-gather quickly, so a background job builds a separate trending dataset.
Summary
- A hot shard means uneven load; fix with a better key, randomness or caching.
- Queries across shards are slow (scatter-gather); design so most queries use the shard key.
- Resharding is hard; use consistent hashing or many virtual shards to make it easier.
- A good shard key has many values, spreads load and appears in common queries.
Architecture Diagram
Queries with the shard key hit one shard; others must ask all.
- 1With the shard key, the router sends the query to one shard.
- 2Without it, the router sends the query to every shard.
- 3Results from all shards are merged.
- 4The slowest shard decides the response time.
Common Interview Questions
Chapter quiz
Data Partitioning · 5 questions
1.Sharding is best described as:
2.Why is hash(key) mod N a problem when N changes?
3.Which strategy is best for range queries like 'all orders in March'?
4.What is a hot shard?
5.A query that does not include the shard key usually has to:
0 of 5 answered
Finished this lesson?
0 of 91 lessons done · 0% of the course

