What is sharding in MongoDB?
Sharding in MongoDB is a horizontal-scaling mechanism where data from a single collection is spread across different servers (shards). Each shard holds only part of the data, and together they look like a single database. The goal is handling large data volumes and heavy read/write load.
How this works
A sharded cluster consists of:
| Component | Role |
|---|---|
| Shards | hold the actual data (each holds only its part) |
| Config servers (CSRS) | hold metadata about which documents live where |
| Mongos | a router: accepts the client's request and directs it to the right shard |
What data gets split on
Sharding requires picking a shard key, the field MongoDB uses to decide where to place a document. For example:
javascript
{ shardKey: 1 }Documents with different shard key values end up on different shards.
What this gives you
- Data-volume scaling, a collection can grow almost indefinitely
- Write scaling, load is spread across servers
- Read scaling, queries run in parallel across different shards
- Performance, there's no bottleneck at a single server
When this is needed
- there's more data than one server can handle
- there's a high volume of writes/queries
- collections grow faster than hardware can scale vertically
Summary
Sharding means spreading data across several servers, so MongoDB can handle large volumes and heavy load while staying fast and scalable.
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.