Notes from https://www.hellointerview.com/learn/system-design/core-concepts/consistent-hashing
- Outgrown single database
- need to split data across multiple independent servers
- Happens when hit storage limits
- A single Postgres instance maxes out in the TB
- Write throughput limits
- Tens of thousands of writes per second
- Read throughput that even replicas can’t handle
- Most important decision is shard key
- Determines how data gets distributed
- For user-centric apps
- Instagram
- Sharding by
user_id means all of a user’s posts, likes, and comments live on one shard
- User-scoped queries are fast because they only hit one shard
- Global queries like “trending posts across all users” become expensive
- Have to hit every shard and aggregate results
- Most systems use hash-basd sharding
- Hash the shard key and use modulo to pick a shard
- Distributes data evenly and avoids hot spots
- Range-based sharing
- Can work if access patterns naturally partition
- Like multi-tenant SaaS where each company only queries their own data
- Easy to create hot spots if one range gets more traffic
- Directory-based sharding
- Uses a lookup table to decide where data lives
- Flexible
- Adds dependency and latency to every request
- Rarely worth it in interview
- Problems
- Cross-shard transactions become nearly impossible
- Design shard boundaries to avoid them
- If user transfer in banking app requires updating accounts on different shards, need to distribute transactions or sagas
- Hot spots
- Happen when one shard gets disproportionate traffic
- Resharding is painful
- Can’t just add a new shard without moving massive amounts of data around
- Mistakes
- Sharding too early
- Well-tuned single database with read replicas can handle way more than one might think
- If proposing sharding, do capacity math
- 10k write per second and 100GB data, don’t need sharding yet
- Suggest if numbers justify
- Interviews
- Only bring up with justification of why single database won’t work
- Clearly state shard key choice
- Explain tradeoff
- Fast for X queries, slow for Y queries
Notes from https://www.hellointerview.com/learn/system-design/core-concepts/sharding