Hash-Based Partitioning
What you'll learn: How to use hash functions to spread data uniformly across partitions, avoiding hotspots and ensuring balanced load distribution.
The Core Idea
Hash-based partitioning applies a deterministic hash function to your partition key, producing a hash value that determines which partition (or node) stores that data. Instead of manually assigning ranges or lists of keys to partitions, the hash function does the distribution automatically.
How it works:
- Choose a partition key (e.g.,
user_id,order_id) - Apply a hash function:
hash(partition_key) → integer - Map the hash to a partition:
partition_number = hash_value % number_of_partitions
This spreads records uniformly across all partitions, regardless of the natural distribution of key values.
Why Use Hash-Based Partitioning?
Uniform load distribution is the primary benefit. Even if your partition keys aren't naturally balanced (imagine user_id values clustered in certain ranges), hashing scrambles them evenly across partitions.
Analogy: Think of hash-based partitioning like shuffling a deck of cards before dealing. Even if the deck started sorted (clustered keys), shuffling ensures each player gets a random, roughly equal hand.
When to Use It
- You need predictable, even distribution without manual tuning
- Your workload doesn't require range queries (hash destroys ordering)
- You want to avoid hot partitions from skewed key distributions
- Write-heavy systems where balanced writes matter more than sorted reads
Tradeoffs
✅ Pros: Even distribution, simple logic, avoids hotspots
❌ Cons: Range queries require scanning all partitions, adding/removing partitions requires rehashing (unless using consistent hashing)
Key Takeaway: Hash-based partitioning uses hash functions to distribute data uniformly across nodes, sacrificing range query efficiency for guaranteed load balance and simplicity.