What sharding strategies exist?
The main sharding strategies determine what principle data gets distributed between servers (shards) by. This affects load balance, query simplicity, and scalability.
1. Hash sharding
- Each record's hash is computed on a specific key (e.g.
user_id), and the hash result determines which shard it lands in. - Provides even data distribution.
- Downside: aggregate queries that need data from every shard become harder (e.g. aggregates).
Example:
shard_id = hash(user_id) % N, where N is the number of shards.
2. Range sharding
- Data is split by ranges of key values (e.g.
user_idfrom 1 to 1,000,000 is one shard, the next range is another). - Simple to implement, convenient for ordered data.
- Downside: possible uneven load if some ranges are more active than others.
3. Geographic (or attribute-based) sharding
- Data is split by an attribute tied to region, country, company branch, and so on.
- Convenient when users are physically spread across regions, it reduces latency and improves local availability.
- Downside: shards can vary a lot in size.
4. Composite (hybrid) sharding
- Combines several strategies, e.g. first by region, then within each region by hash.
- A flexible, scalable option for large systems.
Summary:
- Hash sharding, even, but harder to aggregate.
- Range sharding, simple, but can produce uneven load.
- Geographic sharding, reduces latency, but requires balancing the data.
- Hybrid sharding, universal, but harder to manage.
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.