13 / 15

How to reshard a collection?

Difficulty: 7/10
shard key selection, resharding process, balancer behavior

Resharding a collection in MongoDB involves changing the shard key. While MongoDB does not directly allow modifying the shard key for an existing collection, starting from version 5.0, MongoDB provides a process to reshard a collection.

  1. 1

    Ensure you're running MongoDB 5.0 or newer.

  2. 2

    Make sure the cluster has sufficient resources and minimal workload to support the resharding process.

  3. 3

    You must initiate the process by running a command.

  4. 4

    Carefully choose the new shard key, considering factors like query patterns, data distribution, and write loads.

javascript

Scenario Questions

0-2 years experience

  1. 1We have a sharded MongoDB collection using the field 'userId' as the shard key, but we need to change it to 'email'. How would you go about resharding this collection with minimal downtime?
  2. 2If you run the reshardCollection command while the balancer is disabled, what effect does that have on data distribution?

2-5 years experience

  1. 1During a recent rollout we attempted to reshard a large orders collection and observed the balancer getting stuck. Walk me through how you would diagnose and resolve the issue.
  2. 2Explain the trade‑offs between using the reshardCollection command versus creating a new collection and migrating data manually.

5-8 years experience

  1. 1Our platform stores petabytes of time‑series data across many shards. We need to change the shard key to improve query patterns. How would you design a resharding strategy that minimizes impact on read/write latency and avoids hot spots?
  2. 2What are the performance and operational considerations when resharding a collection that has TTL indexes and a high write throughput?

8+ years experience

  1. 1We have multiple microservices that each own different subsets of a sharded MongoDB cluster. A decision is made to change the global shard key for several core collections. How would you coordinate this migration across teams, ensure data consistency, and plan for rollback?
  2. 2Discuss the long‑term maintenance implications of frequently resharding collections in a large organization. What policies or tooling would you put in place to reduce operational risk?

Follow-up Questions

  • What would you monitor to know the resharding is progressing safely?
  • How would you handle a situation where a chunk migration fails repeatedly?
  • Can you describe how you would test the resharding process before running it in production?
Share

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