Splitting data across servers
Day 121 of 149
π Full deep-dive with code examples
The Library Card Catalog Analogy
One huge card catalog is hard to use:
Long lines at one catalog
If it breaks, no one can find books
Split by letters:
A-F catalog in room 1
G-M catalog in room 2
N-Z catalog in room 3
Search is faster, and if one catalog goes down, you can still use the others (but you lose access to that section until it comes back).
Sharding splits your database into pieces across servers!
Why Shard?
One server has limits:
Storage β Runs out of disk space
Speed β Too many queries to handle
Memory β Can't fit all data in RAM
Sharding spreads the load across multiple servers.
How It Works
Split data by some key:
Users A-H β Server 1 Users I-P β Server 2 Users Q-Z β Server 3
Enter fullscreen mode
Exit fullscreen mode
Each piece is a "shard." Each shard handles its portion.
Sharding Strategies
By key range:
Users 1-1000 β Shard 1
Users 1001-2000 β Shard 2
By hash:
hash(user_id) % 3 ββ¦