articles / mongodb-sharding-shards-chunks-partitions
MongoDB sharding: what it's for, and shards, chunks, replicas, partitions
Mounir Guizani · 30 November 2026 · 5 min read
When a MongoDB collection grows to the point where a single server is no longer enough, two reflexes come up: "let's add replicas" or "let's partition". They are not the same thing, and MongoDB adds its own vocabulary: shards, chunks, replica sets. Here is what sharding is really for, and how not to confuse the different "segments" of data.
Sharding in one sentence
Sharding spreads a collection across several servers, each storing only part of the documents. It is horizontal scaling: more data or more writes, you add shards rather than a bigger machine.
The parts of a sharded cluster
| Component | Role |
|---|---|
| Shard | a server (in practice a replica set) storing part of the data |
| mongos | the router: the application talks to it as to a single database, it sends each query to the right shard |
| Config servers | they know which range of data lives on which shard |
| Shard key | the field or fields that decide where each document goes |
| Chunk (range) | a contiguous segment of shard key values, the unit MongoDB moves between shards |
| Balancer | the process that redistributes chunks to keep shards balanced |
The "segments": don't mix them up
This is where confusion starts. Four notions split data, but not for the same purpose:
| Notion | What it splits | Across how many machines | Goal |
|---|---|---|---|
| Replica set | nothing: each member holds all the data | several | availability, fault tolerance, reads |
| Shard | the collection, into disjoint pieces | several | volume and write throughput |
| Chunk / range | the shard key values, into contiguous segments | spread across shards | unit of distribution and balancing |
| SQL partition (PostgreSQL, Oracle) | a table, by range, list or hash | usually one | maintenance, purging, query pruning |
Key takeaways:
- Replicating is copying; sharding is splitting. A production cluster does both: each shard is itself a replica set.
- A chunk is not a shard: a shard holds many chunks, and the balancer moves them from one shard to another.
- SQL partitioning splits a table, often on the same server: it speeds up data management (archiving a month, skipping partitions in a query); it doesn't multiply write capacity.
Everything hinges on the shard key
A good shard key has three qualities:
- High cardinality: many possible values, so data can be split finely.
- Good distribution: no ultra-frequent value concentrating the documents.
- A fit with the queries: frequent queries include it, so they are routed to a single shard.
Two main strategies:
// Ranged: efficient for interval queries on the key
sh.shardCollection("bank.operations", { customerId: 1, operationDate: 1 })
// Hashed: spreads writes evenly
sh.shardCollection("bank.events", { _id: "hashed" })
Classic traps:
- An increasing key (date,
ObjectId) with ranged sharding: every insert lands on the last chunk, so on a single shard. Hashing solves this. - A low-cardinality key (country, status): chunks that can't be split ("jumbo chunks") and unbalanced shards.
- Queries without the shard key: mongos sends them to every shard (scatter-gather), cancelling part of the benefit.
Good news: recent MongoDB versions let you refine a shard key, or even reshard a collection online. But the initial choice remains the most structuring decision.
When to shard… and when not to
Sharding is worth it when:
- the data volume exceeds what one server can reasonably carry;
- write throughput saturates a single primary node (replicas don't help: every write goes through the primary);
- the working set no longer fits in memory;
- data residency rules require keeping some data in a given region (with zones).
Better to wait when:
- suitable indexes and a revised data model solve the problem;
- a bigger machine (vertical scaling) is enough for several years;
- the team can't operate a cluster: a managed service like MongoDB Atlas greatly reduces that cost.
Sharding adds components, choices that are hard to undo and operational complexity. It is justified by numbers (volume, throughput, latency), not by anticipation.
What about DynamoDB?
DynamoDB does automatic, invisible partitioning: the partition key plays the role of the shard key, and AWS splits and spreads partitions on its own. The design rules are the same (high cardinality, no "hot" key), without a cluster to administer. MongoDB gives more query freedom, DynamoDB more operational peace of mind.
In short
- The replica set protects (availability), sharding grows (volume, writes).
- Chunks are the segments MongoDB moves between shards; SQL partitions organise a table, without necessarily changing machine.
- The shard key decides everything: cardinality, distribution, fit with the queries.
- Shard when measurements demand it, after exhausting indexes, data modelling and vertical scaling.