08 / 15

How to shard a collection?

Difficulty: 7/10
shard key selection, chunk migration, balancing

We use the sh.shardCollection() method, specifying the full namespace of the collection and the target index or compound index to use as the shard key. If you shard a populated collection, only one chunk is created initially. The balancer then migrates ranges from that chunk if necessary according to the configured range size.

javascript

Scenario Questions

0-2 years experience

  1. 1We have a small orders collection for a new e‑commerce app. Walk me through the exact steps you would take to shard that collection.
  2. 2If you shard on the field 'userId' and then insert a document that lacks 'userId', what will MongoDB do?
  3. 3After turning on sharding, you notice all reads still go to a single mongod. What could be causing that?

2-5 years experience

  1. 1Your team is adding a time‑series sensor data collection that will grow quickly. How would you pick a shard key, and what trade‑offs would you evaluate?
  2. 2During a rollout you see a chunk migration taking a long time and causing latency spikes. How would you troubleshoot and mitigate the issue?
  3. 3After sharding, one shard is handling a disproportionate amount of traffic. What steps would you take to rebalance the load?

5-8 years experience

  1. 1Design a sharding strategy for a multi‑tenant SaaS platform that must isolate tenant data while supporting high write throughput. Explain your shard‑key, zone sharding, and balancing choices.
  2. 2Explain how you would monitor and automate chunk migrations at scale to avoid performance degradation.
  3. 3Compare hashed versus ranged shard keys for a workload with frequent range queries and bursts of writes. When might you switch from one to the other?

8+ years experience

  1. 1Our legacy monolithic MongoDB deployment needs to be migrated to a sharded cluster across three data centers. Outline the migration plan, data consistency concerns, and how you’d handle rolling upgrades.
  2. 2How would you evaluate the long‑term cost and operational complexity of staying with a sharded MongoDB versus moving to a different database technology as the product scales?
  3. 3Propose a cross‑team governance model for shard‑key selection and schema evolution to prevent future performance regressions.

Follow-up Questions

  • What metrics would you watch after you enable sharding?
  • Can you give an example of a shard key that caused a hotspot and how you fixed it?
  • If a chunk migration fails mid‑process, how would you recover?
Share

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