100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
MongoDB

Sharding in MongoDB

How MongoDB distributes data across multiple shards using shard keys, chunks, and a query router to scale horizontally beyond a single server's capacity.

Scaling & PracticeAdvanced10 min readJul 10, 2026
Analogies

Why Shard a MongoDB Cluster?

Sharding is MongoDB's method of horizontal scaling: it partitions a collection's data across multiple shards (each typically a replica set) so that no single server needs to hold the entire dataset or handle all the write load. You shard a cluster when a working set exceeds available RAM on one machine, when write throughput saturates a single primary, or when you need to distribute data geographically for latency reasons; sharding a small collection that fits comfortably on one server usually adds operational complexity without benefit.

🏏

Cricket analogy: Like the IPL splitting matches across multiple stadiums in different cities instead of forcing every game through one ground, sharding splits a collection's data across multiple servers instead of forcing every operation through one machine.

Shard Keys and Chunk Distribution

The shard key is the field (or compound fields) MongoDB uses to partition documents into chunks, and it is the single most consequential decision in a sharded design because it cannot be changed after data is written at scale (though MongoDB 5.0+ allows refining it in limited ways). A good shard key has high cardinality and low frequency of repeated values to avoid oversized chunks, and its access pattern should avoid monotonically increasing values like a plain _id or timestamp unless hashed, since those create 'hot' chunks that all writes funnel into on a single shard.

🏏

Cricket analogy: Like choosing which stat (batting average vs. strike rate) to sort a huge player database by, picking a shard key with poor cardinality is like sorting by 'country' when most players share one, creating lopsided groups.

Once data is partitioned by shard key value, MongoDB groups documents into chunks (by default up to 128MB in recent versions using the auto-splitter), and the balancer process migrates chunks between shards in the background to keep the distribution roughly even as data grows. Chunk migrations move a range of documents from a source to a destination shard while updating the config servers' metadata, and heavy migration activity can add temporary load, which is why the balancer window is often scheduled during off-peak hours in write-heavy clusters.

🏏

Cricket analogy: Like a franchise league periodically redistributing players in an auction to keep squads balanced, MongoDB's balancer periodically migrates chunks between shards to keep data distribution even.

Cluster Architecture: mongos, Config Servers, Shards

A sharded cluster has three components: shards (each storing a portion of the data, usually as a replica set for HA), config servers (a replica set storing cluster metadata like chunk ranges and shard mappings), and mongos routers (stateless query routers that applications connect to, which consult the config servers to route operations to the correct shard or shards). Applications should always connect through mongos rather than directly to a shard's primary, since connecting directly bypasses routing and can return an incomplete view of the data.

🏏

Cricket analogy: Like a cricket board having venues (shards) hosting matches, a scheduling committee (config servers) tracking which venue hosts what, and a broadcaster (mongos) directing viewers to the right feed, every request should go through the broadcaster, not directly to a venue.

javascript
// Enable sharding for a database
sh.enableSharding("ecommerce");

// Create a hashed shard key to avoid monotonic hot chunks
db.orders.createIndex({ customerId: "hashed" });
sh.shardCollection("ecommerce.orders", { customerId: "hashed" });

// Compound, ranged shard key for a write-and-range-query workload
sh.shardCollection("ecommerce.events", { tenantId: 1, createdAt: 1 });

// Check chunk distribution across shards
db.orders.getShardDistribution();

Because a shard key is difficult to change once a collection has significant data, model your access patterns before sharding: identify the highest-volume query and write patterns first, then choose a key that distributes writes evenly and, ideally, lets common queries target a single shard rather than scattering across all of them.

Query Routing and Scatter-Gather

Queries that don't include the shard key (or a prefix of a compound shard key) must be broadcast to every shard as a 'scatter-gather' operation, since mongos has no way to know which shard holds the matching documents. This is significantly slower than a targeted single-shard query, so design your most frequent queries around the shard key whenever possible.

  • Sharding scales MongoDB horizontally by partitioning a collection's data across multiple shards.
  • The shard key determines how data is distributed and is extremely difficult to change once data is written.
  • High-cardinality, well-distributed shard keys avoid hot chunks; hashed keys prevent monotonic hotspots.
  • The balancer migrates chunks between shards in the background to keep storage roughly even.
  • A sharded cluster consists of shards, config servers (metadata), and mongos routers (query routing).
  • Applications should always connect through mongos, never directly to a shard's primary.
  • Queries without the shard key trigger a slower scatter-gather across all shards.

Practice what you learned

Was this page helpful?

Topics covered

#MongoDB#MongoDBStudyNotes#Database#ShardingInMongoDB#Sharding#Shard#Cluster#Keys#StudyNotes#SkillVeris