Sharding Strategies & Hot Spots
Definition
Sharding
- In plain English
- Sharding means splitting data across several database servers so each holds only part of it.
- Think of it like
- It is like splitting a city's phone book into volumes A–F, G–M and N–Z kept in different libraries.
Shard key
- In plain English
- A shard key is the value used to decide which server a row lives on.
- Think of it like
- It is like the first letter of a surname deciding which volume to open.
Why it exists
Your leader’s storage and write capacity are full. Read followers do not remove the leader’s responsibility for every write.
You need several servers to own different portions of the data. Each server then handles writes for its own portion.
How it works
Hash sharding
- In plain English
- Hash sharding runs the shard key through a formula that scatters keys evenly across servers.
- Think of it like
- Like dealing cards around a table so everyone gets a similar pile.
Range sharding
- In plain English
- Range sharding gives each server a continuous range of keys, such as A to F.
- Think of it like
- Like the A–F, G–M, N–Z phone book volumes.
Directory sharding
- In plain English
- Directory sharding keeps a lookup table saying which server owns each key or group.
- Think of it like
- Like a receptionist with a list of which room each team is in.
Hot spot
- In plain English
- A hot spot is one shard receiving far more traffic than the others.
- Think of it like
- Like one checkout lane with a queue out the door while others sit empty.
Resharding
- In plain English
- Resharding is moving data between shards when you add servers or when shards become uneven.
- Think of it like
- Like rearranging library volumes when a new branch opens.
The app gives a shard key to a router, which chooses one of three database servers holding different data.
| Strategy | Spread | Range queries | Moving data | Example |
|---|---|---|---|---|
| Hash | Often even across many keys | Usually touches many shards | Changing shard count can move many keys | Player ID |
| Range | Depends on key popularity | Nearby keys stay together | Split overloaded ranges | Trade symbol |
| Directory | Chosen per group | Depends on assigned groups | Move a group and update its mapping | City |
Examples
Route a player profile
Every profile query includes player_id. You spread profiles across eight shards.
shard = hash(player_id) % 8
Illustrative hash outputs, not a specific hash algorithm:
hash("player-42") = 1,234 → shard 2
hash("player-99") = 5,679 → shard 7Many keys can spread evenly while one popular key still overloads its shard. Measure requests per shard as well as stored bytes.
Consistent hashing
- In plain English
- Consistent hashing assigns keys to servers so adding or removing a server usually moves only some keys.
- Think of it like
- It is like adding a library branch by moving nearby shelves rather than rearranging every book.
Module 5 explains consistent hashing. Queries, joins, and transactions spanning shards still need extra coordination.
The insight
Hash sharding helps distribute keys when lookups include the shard key.
Spot an overloaded symbol
Your trade history is split into symbol ranges. One popular stock produces 40% of all trades.
Its shard can overload while others stay idle. Splitting alphabetical boundaries does not divide traffic for that one symbol.
- Choose a finer key
Add a time bucket or another subdivision that can spread this symbol’s workload.
- Check the busiest window
The newest time bucket can still be hot; measure before choosing boundaries.
- Plan wider reads
A full symbol history may now require several shards.
The insight
Range shards support range reads but need a plan for popular keys.
Move one city’s group
A directory maps each city to its shard. One city needs a dedicated shard.
An app looks up its city in a directory; Pune maps to shard 1 and Kochi maps to shard 3 after data moves from shard 2.
- Copy safely
Move the city’s records while tracking writes made during the move.
- Switch ownership
Coordinate the mapping change so each write has one owner.
- Verify
Check records and routing before retiring the old copy.
A directory adds a lookup and needs its own reliability plan. Moving an entire city cannot fix a city that exceeds one shard.
The insight
Directory sharding makes placement flexible while adding routing and migration work.
Trade-offs
Pros
- Adds write and storage capacity across shards.
- Some failures affect only one data portion.
Cons
- Cross-shard queries and transactions need coordination.
- Moving data while serving writes is difficult.
- A popular key can still overload one shard.
Use when
- Measured storage or writes exceed one tuned machine.
- Common requests identify a suitable shard key.
Avoid when
- Indexes or a larger machine still suffice.
- The workload mostly needs global joins and transactions.
Quick check
Takeaways
- Sharding spreads data and writes across servers.
- Choose a shard key for common queries and balanced demand.
- Use sharding after simpler capacity improvements prove insufficient.