Q41: What is a Shard Key, and how do you choose a good shard key?
A Shard Key determines how documents are partitioned across cluster shards. Characteristics of a good shard key:
- High Cardinality: Contains many unique values.
- Even Frequency Distribution: Avoids data clustering.
- Non-Monotonically Increasing: Monotonic keys (e.g., auto-increment IDs, timestamps) cause all writes to hit a single shard (jumbo chunk hotspotting).
Key Takeaways
- Immutable field determining document placement.
- Avoid monotonically increasing keys; consider Hashed Shard Keys.
Knowledge Check
Why is a monotonically increasing timestamp a poor choice for a range-based Shard Key?