System Design Intermediate
03 Scaling Reads & Writes
One database eventually stops being one database. Replicas multiply your read capacity, shards split your write load -- and each introduces brand-new ways to be wrong that a single box never had.
A chat app's messages table has outgrown one database and must be sharded. Candidate shard keys: message_id, sender user_id, conversation_id. Pick one, justify it with the dominant query, explain why each rejected key fails, and say what you do about one enormously hot conversation.
Hints
- What is the dominant query in chat? Loading the recent messages of ONE conversation.
- A good shard key keeps the dominant query on one shard. Which candidate does that?
- One giant conversation still lands on a single shard. Think secondary splitting -- by time bucket -- or special-casing the whales.