Your MongoDB collection is outgrowing one replica set, so you're sharding it. The one decision that matters most is the shard key. A good one spreads data and load evenly and routes queries to a single shard. A bad one gives you one overloaded shard, "jumbo" chunks that can't move, and queries that hit every shard.
MongoDB can change a shard key now (since 5.0), but resharding a large collection is still a big operation. It pays to get it right first.
What the shard key does
MongoDB splits a sharded collection into ranges of shard key values (chunks) and spreads them across shards. For every operation:
- queries that include the shard key (or its prefix) go to the one shard that owns that range: a targeted query;
- queries without it are sent to all shards and merged: scatter-gather, which gets slower and more expensive as you add shards.
Four properties of a good shard key
1. High cardinality. Many distinct values, so data can be split into many chunks. country (about 200 values) or status (5 values) can never spread across 50 shards, and all documents with one value stay together in one chunk that can't be split: a jumbo chunk.
2. Low frequency. Values should be spread evenly. If 40% of documents have tenantId: "acme", that tenant's range sits on one shard, however high the overall cardinality is.
3. Non-monotonic for writes. _id (ObjectId) and createdAt always increase, so every insert goes to the chunk holding the maximum value, on one shard. Write throughput stops at one shard's capacity, and the balancer is constantly moving chunks away from it.
4. Matches your queries. The most frequent queries should include the shard key, so they're targeted.
Common choices
Hashed shard key. It spreads writes evenly even for monotonic fields:
sh.shardCollection("app.events", { deviceId: "hashed" })
It gives even distribution and no insert hotspot, but range queries on the key are scatter-gather (hashing destroys order).
Compound key: a high-cardinality or grouping field, then something more specific.
sh.shardCollection("app.orders", { customerId: 1, orderId: 1 })
Queries by customerId are targeted, and one customer's orders are stored together. Adding orderId gives enough cardinality to split big customers into multiple chunks.
Hashed prefix for multi-tenant, time-series-like data (supported since 4.4):
sh.shardCollection("app.metrics", { tenantId: "hashed", ts: 1 })
Tenants are spread evenly across shards, and each tenant's data is ordered by time.
Test before you commit
-
analyzeShardKey(MongoDB 7.0+) samples real queries and data to report a candidate key's cardinality, frequency, monotonicity and targeting:db.adminCommand({ analyzeShardKey: "app.orders", key: { customerId: 1, orderId: 1 } })Enable query sampling first (
configureQueryAnalyzer) so it can judge read and write targeting. -
Check your top queries in the profiler or Atlas Query Insights: do they include the candidate key?
If you got it wrong
refineCollectionShardKey(4.4+) adds suffix fields to an existing key, for example{ customerId: 1 }→{ customerId: 1, orderId: 1 }, which fixes jumbo chunks without moving data.reshardCollection(5.0+) changes the key entirely. It copies the collection in the background while writes continue, with a short write-blocking cutover at the end. It needs spare disk and I/O capacity, and time proportional to the collection's size.sh.status()anddb.collection.getShardDistribution()show how data and chunks are spread.
Before sharding at all
Sharding adds operational complexity. Check first whether you really need it: better indexes, archiving old data, a larger replica set tier, or splitting unrelated collections across clusters are often simpler for moderate growth.
Checklist
- Choose a key with high cardinality, low frequency and non-monotonic writes, that matches your frequent queries.
- Use a hashed key (or hashed prefix) for monotonic or time-based data, and compound keys for grouping plus uniqueness.
- Validate candidates with
analyzeShardKeyand real query patterns. - Fix mistakes with
refineCollectionShardKeyorreshardCollection, and consider whether you need sharding at all.
Get the weekly commit
New database deep dives every week.
