04 / 15

What is a shard key?

Difficulty: 5/10
shard key selection, data distribution, scalability

MongoDB uses the shard key to distribute the collection's documents across shards. The shard key consists of a field or multiple fields in the documents.

  1. 1

    Documents in sharded collections can be missing the shard key fields. Missing shard key fields are treated as having null values when distributing the documents across shards but not when routing queries.

  2. 2

    Starting in MongoDB 5.0, you can reshard a collection by changing a collection's shard key.

  3. 3

    You can refine a shard key by adding a suffix field or fields to the existing shard key.

  4. 4

    A document's shard key value determines its distribution across the shards. You can update a document's shard key value unless your shard key field is the immutable _id field.

  5. 5

    To shard a populated collection, the collection must have an index that starts with the shard key. When sharding an empty collection, MongoDB creates the supporting index if the collection does not already have an appropriate index for the specified shard key.

  6. 6

    The choice of shard key affects the performance, efficiency, and scalability of a sharded cluster. A cluster with the best possible hardware and infrastructure can be bottlenecked by the choice of shard key.

Scenario Questions

0-2 years experience

  1. 1We need to store user profiles in a sharded MongoDB cluster. Which field would you choose as the shard key and why?
  2. 2If you pick a shard key that has many duplicate values, what impact will you see on query performance and balancing?

2-5 years experience

  1. 1Our recent rollout caused a chunk migration to stall after we added a new index. Walk me through how the shard key choice could have contributed to this issue.
  2. 2You have a collection with time‑series data and need to support range queries on timestamps. How would you design the shard key, and what trade‑offs are you considering?

5-8 years experience

  1. 1We are scaling a multi‑tenant SaaS platform to billions of documents. Explain how you would evaluate different shard key candidates to minimize hotspotting and support efficient tenant isolation.
  2. 2During a heavy write load we observed uneven chunk distribution and increased latency. How would you redesign the shard key or use zone sharding to address this?

8+ years experience

  1. 1Our organization is migrating from a monolithic MongoDB deployment to a globally distributed sharded cluster across three data centers. Describe the long‑term strategy for choosing and possibly evolving shard keys to accommodate future data models and compliance requirements.
  2. 2Several legacy services rely on a compound shard key that includes a field we now need to deprecate. How would you plan a migration that preserves data locality and minimizes downtime?

Follow-up Questions

  • What metrics would you monitor to detect a poorly chosen shard key?
  • Can you give an example of a good versus a bad shard key for a given workload?
  • How does the shard key choice influence index selection and query performance?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.