Partitioning
(Sharding)
When your dataset is too big for one disk, you split it up. But how do you split it so you can still find things quickly?
Vertical vs Horizontal Scaling
Vertical (Scale Up)
- ✓ Simple (No code changes).
- ✗ Expensive (Specialized hardware).
- ✗ Hard Limit (Max RAM/CPU).
Horizontal (Scale Out)
- ✓ Cheap (Commodity hardware).
- ✓ Infinite scale.
- ✗ Complex (Distributed transactions, joins).
Partitioning Strategies
Key Range Partitioning
Sort keys and split by range. Like an encyclopedia (A-C, D-F, etc.).
- ✓ Range scans are efficient.
- ✗ Uneven load if keys are sequential (e.g., timestamps).
Hash Partitioning
Hash the key to pick a shard. hash(id) % N.
- ✓ Even distribution (load balancing).
- ✗ Range scans are impossible (keys are scattered).
The Hotspot Problem
Celebrity Problem
What if one key is extremely popular? (e.g., Justin Bieber's Twitter handle).
Even with perfect hashing, ALL his tweets and replies hit ONE shard.
The Fix: Salting
Append a random number to the key to split it across shards.
(Trade-off: Reads must now query all split keys and merge results).
Rebalancing (Consistent Hashing)
When you add a new node, you don't want to move all data. You only want to move some data.
Consistent Hashing Ring
Imagine a circle (0-360°). Nodes are placed on the circle. Keys are placed on the circle. A key belongs to the first node it encounters moving clockwise.
To balance load, each physical node appears at multiple random positions on the ring ("Virtual Nodes"). This prevents one node from getting a huge slice of the pie by luck.
Secondary Indexes
You sharded by User ID, but now you want to search by Email. The data is scattered everywhere.
Local Index (Scatter-Gather)
Each shard maintains its own index for its own data.
Merge results.
- ✓ Fast writes (only touch one shard).
- ✗ Expensive reads (query amplification).
Global Index
A separate database/shard just for the index (Email → UserID).
Go to specific User shard.
- ✓ Fast reads (direct lookup).
- ✗ Slow writes (distributed transaction to update data + index).