12 / 15

What is refining a shard key and how to do it?

Difficulty: 7/10
shard key selection, refine shard key, data migration

Refining a collection's shard key allows for a more fine-grained data distribution and can address situations where the existing key has led to jumbo chunks due to insufficient cardinality.

  1. 1

    To refine a collection's shard key, use the refineCollectionShardKey command. The refineCollectionShardKey adds a suffix field or fields to the existing key to create the new shard key.

For example, you may have an existing orders collection in a test database with the shard key { customer_id: 1 }. You can use the refineCollectionShardKey command to change the shard key to the new shard key { customer_id: 1, order_id: 1 }:

Scenario Questions

0-2 years experience

  1. 1We have a collection sharded on {country:1}. After a few weeks most queries also filter on city. How would you refine the shard key to include city, and what MongoDB steps would you take?
  2. 2If you try to add a new field to an existing shard key without recreating the collection, what error do you expect and why?
  3. 3You need to switch from a hashed shard key to a compound key to support range queries. Which commands would you run and what immediate considerations are there?

2-5 years experience

  1. 1Our logs are sharded by userId, but a handful of users generate most writes, causing hot shards. Explain how you would refine the shard key to spread load and what impact this has on existing data and indexes.
  2. 2During a rollout we changed the shard key from {region:1} to {region:1, deviceType:1}. Some queries got slower afterwards. What could be causing the slowdown and how would you diagnose and fix it?
  3. 3We have a 500 M‑document collection and need to refine its shard key without downtime. Walk me through the steps, including any use of refineCollectionShardKey and its limitations.

5-8 years experience

  1. 1Design a migration plan to refine the shard key of a production collection that cannot be taken offline, covering data size, index rebuild, and read/write latency impact.
  2. 2Compare using the refineCollectionShardKey aggregation pipeline versus dumping and re‑importing data for a collection that has secondary and TTL indexes. What trade‑offs would you consider?
  3. 3After refining a shard key in a multi‑region deployment, how would you monitor and mitigate performance degradation caused by chunk migrations?

8+ years experience

  1. 1Our platform has dozens of sharded collections, many originally sharded on suboptimal keys. Propose a long‑term strategy for evaluating and refining shard keys across services, including governance, tooling, and backward compatibility.
  2. 2When refining a shard key, how do you handle cross‑service contracts and client libraries that embed the original shard key fields? Discuss versioning and migration patterns.
  3. 3If a refined shard key introduces a new field that is also used for data‑residency compliance, how would you align the refinement with regulatory requirements and operational processes?

Follow-up Questions

  • What are the risks if the new shard key isn’t a superset of the old one?
  • How would you verify that chunk distribution improved after the refinement?
  • Can you roll back a shard key refinement if something goes wrong, and how?
Share

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