01 / 15

What is sharding in MongoDB?

Difficulty: 6/10
shard key selection, balancing, cluster migration

Sharding is a method for distributing data across multiple machines by partitioning data into smaller chunks (shards). MongoDB uses sharding to support deployments with very large data sets and high throughput operations.

Scenario Questions

0-2 years experience

  1. 1We have a small e‑commerce app that stores the product catalog in a single MongoDB instance. If we want to start using sharding to handle growth, which steps would you take to enable sharding for the 'products' collection?
  2. 2Suppose you set the shard key to 'category' and insert a new document with a category value that doesn't exist yet. What happens during the insert on a sharded cluster?
  3. 3If a query filters only by 'price' and the collection is sharded on 'category', how will MongoDB route that query?

2-5 years experience

  1. 1Your team added a new field 'region' and wants to change the shard key from 'category' to 'region' without downtime. How would you approach this migration, and what pitfalls would you watch for?
  2. 2After deploying a sharded cluster, you notice one shard is 80% full while others are barely used. What could cause this imbalance and how would you resolve it?
  3. 3A write operation to a sharded collection is failing with a 'StaleConfig' error. Walk me through how you'd debug and fix the issue.

5-8 years experience

  1. 1Design a sharding strategy for a high‑traffic IoT telemetry service that writes millions of documents per minute and queries by deviceId and timestamp. Explain your shard key choice, chunk size, and how you’d handle hot spots.
  2. 2Explain the trade‑offs between using a hashed shard key versus a ranged shard key for a social media feed service that needs chronological ordering.
  3. 3Your cluster experiences increased latency during a global read‑heavy campaign. How would you evaluate whether to add more shards, adjust balancer settings, or introduce read‑only secondary replicas?

8+ years experience

  1. 1Our company is moving from a monolithic MongoDB deployment to a globally distributed sharded architecture across three data centers. What architectural considerations, data migration plan, and operational safeguards would you put in place?
  2. 2Discuss how you would coordinate sharding changes (e.g., shard key redesign) across multiple product teams that each own different microservices, ensuring backward compatibility and minimal service disruption.
  3. 3Given a legacy system that cannot be sharded because of embedded transactions, propose a long‑term strategy to modernize the data layer while preserving existing business logic.

Follow-up Questions

  • Why did you pick that particular shard key?
  • What metrics would you monitor to detect shard imbalance?
  • How would you validate a shard‑key migration before rolling it out?
Share

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