Concept Simulators / Spreading and protecting data
Sharding
Hash, range and consistent hashing: balance, hotspots and resharding cost.
On the roadmap:Intermediate · step 3: Sharding
Settings
How a key picks its shard
What the keys look like
Change the number of shards
Busiest shard vs. even share
1.01x
Keys that move (4 to 5)
80%
Data to move
1.6 TB
Hot shard
none
Hashing spreads keys evenly, but changing the shard count remaps about 80% of them: moving 1.6 TB of 2.0 TB. Consistent hashing fixes this.
Where 20,000 keys land (Hash (key mod N))
An even share would be 5,000 keys per shard. A red bar holds more than one and a half times that.
Where does one key live?
In plain words
Sharding splits one big table across several machines, each holding a slice. A rule decides which machine owns each key: hash it, give each machine a range of keys, or place the machines on a ring.
Why it matters
One machine cannot hold or serve everything forever. Splitting the data lets you add machines, but the rule you choose decides two things that hurt in practice: whether some machines get far more traffic than others (a hotspot), and how much data has to move when you add or remove a machine.
How real systems do it
The simulation is a simplified model. Here is what production systems add or do.
- Cassandra and other Dynamo-style stores place keys on a ring of tokens, so adding a node moves only a slice of the data.
- Range sharding keeps neighbouring keys together, which makes range scans cheap, but time-ordered keys all land on the newest range.
- A single very popular key cannot be split by a hashing rule. Common fixes are to split the key into several by adding a suffix, to give it a shard of its own, or to cache it in the application.
- Google's consistent hashing with bounded loads caps how far above average a server can go. Vimeo used it in HAProxy and reported a large drop in cache bandwidth.
Study: Sharding
Once you have played with it, learn the same idea from people who build these systems. Watch first for the picture, then read for the detail.
9 minWhat is DATABASE SHARDING?
Gaurav Sen · 1.1M views
Then
24 minDatabase Sharding and PartitioningArpit Bhayani · 292K views
31 minSharding in System Design Interviews w/ Meta Staff EngineerHello Interview · 192K views
4 minApache Kafka 101: Partitioning (2023)Confluent, an IBM Company · 179K views
19 min2. CAP Theorem (Hindi) | High Level Design for Beginners | CAP Partition Tolerance explainedConcept && Coding - by Shrayansh · 142K views