Sharding & Partitioning

Difficulty
Importance

The problem

Eventually a dataset or workload grows too large for any single machine to hold or serve, no matter how powerful — sharding splits data across many machines so each holds only a slice, letting total capacity grow by adding machines instead of upgrading one.

Why now

Modular arithmetic already introduced the exact mechanism (hashing a key into one of N buckets, and consistent hashing's ring for minimizing remapping) that determines which shard a piece of data belongs to; NoSQL and data modeling tradeoffs already established that access-pattern-driven design matters, which sharding strategy directly depends on.

Mental model

Sharding is hashing, made physical: instead of a key mapping to a bucket in a hash table, it maps to a specific machine holding that slice of data — and consistent hashing (already covered) is exactly the technique used so that adding or removing a shard remaps only a small fraction of keys instead of nearly all of them.

Requires

Used in

Projects

  • Implement basic hash-based sharding (hash(key) mod N) across simulated shards, then implement consistent hashing and compare how many keys remap when a shard is added
  • Design a shard key for a real dataset (e.g. user data) and identify a naive choice that would create a hot spot, versus a better one

Examples

  • Sharding users by hash(user_id) mod 4 spreads them across 4 machines, but adding a 5th machine with plain mod remaps almost every user — consistent hashing avoids this
  • Sharding by signup_date instead of a well-distributed key can create a hot spot where all of today's active users hit one shard

Resources

Mastery checklist