| Users / Data Size | 100 Users | 10K Users | 1M Users | 100M Users |
|---|---|---|---|---|
| Data Distribution | Single shard or few shards, simple key | Multiple shards, risk of uneven load if poor key | Many shards, key must evenly distribute data | Hundreds/thousands shards, key must avoid hotspots |
| Query Performance | Direct queries, no cross-shard | Some cross-shard queries if key poorly chosen | Cross-shard queries costly, key must minimize them | Cross-shard queries expensive, key critical for locality |
| Scalability | Vertical scaling enough | Horizontal scaling starts, key affects balance | Horizontal scaling essential, key affects shard size | Massive horizontal scaling, key must support re-sharding |
| Maintenance | Simple backups | Shard rebalancing rare | Shard rebalancing needed if key causes hotspots | Frequent re-sharding, key must support smooth migration |
Shard key selection in HLD - Scalability & System Analysis
When the shard key is poorly chosen, some shards get much more data or traffic than others. This causes hotspots where a few servers are overloaded while others are idle. This imbalance breaks performance and scalability first, even if hardware is sufficient.
- Choose a high-cardinality key: Use a key with many unique values to spread data evenly.
- Use composite keys: Combine multiple fields to improve distribution.
- Hash-based sharding: Hash the key to randomize distribution and avoid hotspots.
- Range-based sharding with careful ranges: Define ranges that balance load.
- Re-sharding: Migrate data to new shards when imbalance occurs.
- Caching and read replicas: Reduce load on hot shards by caching frequent queries.
- Monitoring: Continuously monitor shard load to detect imbalance early.
Assuming 1M users, each making 1 request per second:
- Total requests per second: ~1,000,000 QPS
- Single shard DB handles ~5,000 QPS → Need ~200 shards
- Storage per user: 1MB → Total 1TB data
- Network bandwidth per shard: 5,000 QPS * 1KB/request ≈ 5MB/s (40Mbps)
- Shard key must evenly distribute 1TB and 5,000 QPS per shard
Start by explaining what a shard key is and why it matters. Then discuss how poor key choice leads to hotspots and bottlenecks. Next, describe how to pick a good key (high cardinality, hashing, composite keys). Finally, mention monitoring and re-sharding as ongoing solutions. Use examples and relate to real data distribution challenges.
Your database handles 1000 QPS. Traffic grows 10x to 10,000 QPS. What do you do first?
Answer: Identify if the shard key evenly distributes load. If not, implement or improve sharding with a better key or hashing to spread traffic across more shards. Also consider adding read replicas and caching to reduce load.
Practice
Solution
Step 1: Understand shard key role
The shard key determines how data is divided across multiple servers or shards.Step 2: Identify main benefit
Splitting data helps balance load and speeds up data retrieval by parallelizing queries.Final Answer:
To split data across servers for better load distribution and faster access -> Option DQuick Check:
Shard key purpose = split data load [OK]
- Confusing shard key with encryption key
- Thinking shard key compresses data
- Assuming shard key is for backup
Solution
Step 1: Identify shard key traits
A good shard key should have many unique values to avoid data concentration.Step 2: Understand distribution importance
Even distribution prevents hotspots and balances load across shards.Final Answer:
Has many unique values and distributes data evenly -> Option BQuick Check:
Good shard key = unique + even distribution [OK]
- Picking keys with few unique values causing hotspots
- Assuming timestamp is always best shard key
- Using large text fields that slow down indexing
country_code which has only 5 unique values. What is the likely problem with this shard key?Solution
Step 1: Analyze unique values count
Only 5 unique values means few shards and many users per shard.Step 2: Understand load distribution
Popular countries will have more users, causing some shards to be overloaded.Final Answer:
Some shards will be overloaded due to uneven data distribution -> Option CQuick Check:
Few unique keys = uneven shard load [OK]
- Assuming fewer shards always improve speed
- Thinking shard key encrypts data
- Ignoring uneven data distribution effects
user_id % 10 as a shard key but notices uneven shard sizes. What is the likely cause?Solution
Step 1: Understand modulo shard key
Modulo operation expects uniform distribution of user IDs.Step 2: Identify distribution problem
If user IDs cluster in certain ranges, modulo results cluster causing uneven shards.Final Answer:
User IDs are not uniformly distributed modulo 10 -> Option AQuick Check:
Non-uniform IDs cause uneven modulo shard keys [OK]
- Assuming modulo always balances shards
- Confusing shard key type with encryption
- Ignoring data distribution patterns
user_id and order_date. Which shard key choice best supports fast queries and balanced load?Solution
Step 1: Match shard key to query patterns
Users query by both user_id and order_date, so shard key should support both.Step 2: Evaluate shard key options
Sharding by user_id alone may cause hotspots; order_date alone may cause uneven load by time.Step 3: Use composite key benefits
Composite key balances load and supports queries filtering by either field efficiently.Final Answer:
Shard by a composite key ofuser_idandorder_date-> Option AQuick Check:
Composite shard key matches queries and balances load [OK]
- Choosing single field shard keys ignoring query patterns
- Using random UUID causing inefficient queries
- Ignoring load balancing in shard key choice
Ready to prove it?
Pass the test and this pattern moves unknown
