Skip to main content

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:

ComponentRole
Shardshold the actual data (each holds only its part)
Config servers (CSRS)hold metadata about which documents live where
Mongosa 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

  1. Data-volume scaling, a collection can grow almost indefinitely
  2. Write scaling, load is spread across servers
  3. Read scaling, queries run in parallel across different shards
  4. 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 ready
Premium

A concise answer to help you respond confidently on this topic during an interview.