Hash-Based Sharding
What you'll learn: How to use hash functions to automatically and evenly distribute data across database shards.
The Core Idea
Hash-based sharding applies a hash function to your shard key (the field you chose to partition data by) to determine which shard stores each record. The hash function converts the key into a number, then you use modulo arithmetic to map it to a specific shard.
For example, if you have 4 shards and a user ID of 12345:
- Hash the ID:
hash(12345) = 98237498234 - Apply modulo:
98237498234 % 4 = 2 - Store in shard #2
Why Hash-Based Sharding Works
The beauty of hash functions is they produce uniform distribution. Even if your shard keys aren't evenly distributed (like if most user IDs are sequential), the hash function scrambles them pseudo-randomly across all shards.
Analogy: Imagine dealing cards from a shuffled deck to four players. The shuffle (hash function) ensures each player gets roughly the same number of cards, regardless of the original order.
The Trade-Off
Hash-based sharding prevents hotspots by spreading load evenly. However, it makes range queries impossible—you can't ask "give me all users with IDs 1000-2000" because those users are scattered across different shards. You'd have to query all shards and combine results.
Also, adding or removing shards later requires rehashing most of your data (we'll address this challenge in future lessons).
Key Takeaway:
Hash-based sharding uses a hash function on the shard key to deterministically and evenly distribute records across shards, preventing hotspots but sacrificing range query efficiency.