Chat app: the main query is load the latest 50 messages of a conversation. Messages are sharded by hash(message_id) and that query fans out to every shard. Which shard key fixes it?
Show answer
Answer
Sharding by the key the hot query filters on puts a conversation on one shard, so the read hits one node.
More shards of message_id makes the fan-out wider. created_at puts all new messages on the newest shard. sender_id still spreads one conversation across every participant's shard.
Source: Hello Interview: Core Concepts for System Design, Designing Data-Intensive Applications, Martin Kleppmann, O'Reilly, 2017