MongoDB & NoSQL (50 Qs)•Part 3: Advanced Internals, Scale & Production Patterns (Senior / Lead)
What makes a Good vs Bad Shard Key in MongoDB?
senior LevelDatabases & SQL
In Simple Words: Selecting the right shard key is the most critical architectural decision:
- Bad Shard Key (Monotonically Increasing
_idor timestamp):- All new writes route to the single shard owning the highest range.
- Creates a Hot Shard bottleneck while other shards sit idle.
- Bad Shard Key (Low Cardinality, e.g.
country):- Creates massive indivisible chunks that cannot be balanced across shards.
- Good Shard Key (High Cardinality + High Query Isolation):
- Compound Shard Key:
{ tenantId: 1, createdAt: 1 }or Hashed Shard Key ({ _id: "hashed" }). - Distributes writes evenly across all shards while allowing targeted queries.
- Compound Shard Key: