As data volume grows beyond the limits of a single server, MongoDB’s sharding feature becomes essential for maintaining performance and availability. Sharding distributes data across multiple servers (shards), allowing your application to scale horizontally with ease.

In this blog, we’ll walk through the entire process—from setting up config servers and shard replica sets to enabling sharding on a specific collection and analyzing how your data is distributed. Whether you’re a DBA or a developer, this guide will provide a hands-on understanding of sharding in action.

What is Sharding?

Sharding is the process of splitting a large database into smaller, faster parts called “shards”.Each shard holds a subset of the data, and all shards together make up the full dataset.

Why Sharding Matters

  • Scalability : As your data grows, a single server can’t handle it all.Sharding lets you distribute data across multiple servers (horizontal scaling).
  • Performance Boost : Queries hit only the relevant shard instead of scanning the whole dataset.This means faster reads and writes.
  • High Availability : When combined with replication each shard can be replicated — making the system more reliable.
  • Cost Efficiency : You can use many smaller, cheaper machines instead of one big, expensive server.

Advantages

  • Horizontal Scalability : Easily handle large datasets by distributing them across multiple servers.
  • Improved Performance : Queries target specific shards, reducing load and improving response time.
  • High Write Throughput : Since writes are distributed, MongoDB can handle more concurrent writes.
  • Cost Efficiency : Use multiple smaller, cheaper servers instead of scaling one big server.
  • Storage Optimization : No single machine needs to store all the data — each holds only part.

Sharded Cluster Architecture

  1. App Server Layer (Top Layer)
    • At the top, we have the App Servers, which connect to Routers (mongos).
    • These routers act as query routers. They receive client requests and forward them to the appropriate shard.
    • There can be one or more mongos routers for load balancing and high availability.
  2. Config Servers (Right Side – Red Box)
    • The Config Servers form a replica set and store metadata about the cluster.
    • They contain the mapping of the data chunks to shards.
    • Mongos queries the config servers to determine where data lives, but they don’t handle actual data.
    • You must have exactly 3 config servers in a production cluster for fault tolerance.
  3. Shards (Bottom Layer – Green Boxes)
    • The actual data resides in the shards.
    • Each shard is a replica set, ensuring high availability and fault tolerance.
    • MongoDB splits large datasets into chunks and distributes them across shards based on the shard key.

How It All Works Together

  • When an application sends a query to mongos:
  • Mongos checks with the config servers to know which shard has the needed data.
  • The query is then forwarded to the appropriate shard.
  • The response is sent back to the app via mongos.

Cluster Components

  1. Shard
    • Purpose
      • A shard stores a subset of the data in the cluster.
      • It is a replica set (for high availability and redundancy)
    • Why Needed?
      • To horizontally scale your data.
      • If one server can’t handle all data or workload, you split it across shards.
  2. Mongos (Query Router)
    • Purpose
      • Acts as a middleware router between the application and the cluster.
      • It routes read/write queries to the appropriate shard(s).
    • Why Needed?
      • Application doesn’t need to know where data lives.
      • Mongos handles query routing, aggregation, and results merging.
  3. Config Servers
    • Purpose
      • Store metadata about the cluster: chunk ranges, shard locations, etc.
      • Maintain the cluster state and mapping of data.
    • Why Needed?
      • Without config servers, mongos won’t know where data lives.
      • They ensure consistency and coordination in sharded clusters.
  4. Shard Key
    • Purpose
      • A field or fields used to determine how data is partitioned (sharded).
      • All documents are partitioned based on this key.
    • Why Needed?
      • Determines the distribution logic of documents across shards.
      • Helps balance read/write loads.
  5. Chunks
    • Purpose
      • A chunk is a range of shard key values.
      • MongoDB splits data into these chunks, usually ~64MB each.
    • Why Needed?
      • Makes it easier to balance and migrate parts of the data.
      • Helps the balancer move small units of data between shards to maintain balance.

How to Setup Sharding

These steps demonstrate how to set up a MongoDB sharded cluster across three Ubuntu 22.04 LTS servers running MongoDB 7.0.2. I’ll show you how to configure sharding for a new collection.

  1. Set Up the MongoDB Config Server Replica Set : This stores metadata and configuration settings for the cluster.
  2. Configure Shard Server Replica Sets : Each shard is a replica set to ensure redundancy and fault tolerance.
  3. Run mongos and Add Shards to the Cluster : mongos acts as the query router between the client and the sharded cluster.
  4. Enable Sharding for the Database : This step prepares the database for distributing data.
  5. Create the Sharded Dataset : Insert sample data or prepare collections to be sharded.
  6. Enable Sharding on the Collection : Define the shard key and begin partitioning the data.
  7. Analyze Shard Usage : Use tools and commands to monitor how data is distributed across shards.

1) Set Up the MongoDB Config Server Replica Set

Edit the /etc/mongod.conf file to enable replication and specify the replica set name and also enable sharding and add the clusterRole

sudo vi /etc/mongod.conf
replication:
  replSetName: "config"
sharding:
  clusterRole: "configsvr"  

After that restart the mongoDB service and initiate the replication using rs.initiate() method.

2) Configure Shard Server Replica Sets

Edit the /etc/mongod.conf file to enable replication and specify the replica set name and also enable sharding and add the clusterRole

    sudo vi /etc/mongod.conf
replication:
  replSetName: "shard1"
sharding:
  clusterRole: "shardsvr"  

After that restart the mongoDB service and initiate the replication using rs.initiate() method

3) Run mongos and Add Shards to the Cluster

Step 1 : Install the mongodb server and change the bindIp adress.
Step 2 : Stop the mongodb service
Step 3 : Disable the the mongod service from starting whenever the server boots up.
Step 4 : Run the mongos and connect it to the config replica server using –configdb option.

sudo mongos --configdb config/192.168.56.10:27017

Step 5: To add the shards into the sharded cluster using sh.addShard() method.

sh.addShard("shard1/192.168.56.20:27017")

After that you need to run the sh.status() method to verify the status of the shard.

4) Enable Sharding for the Database

Here enable the “demo” database.

sh.enableSharding("demo") 

After that you need to run the sh.status() method to verify the status of the shard.

5) Create the Sharded Dataset

Step1 : Create a new collection and add documents into the collection.
Step2 : Create a new index using db.createIndex()
Step3 : To make sure the index is hashed using db.ensureIndex().

If you have existing a collection you don’t need to follow the step1.

db.cities.createIndex({name:1})
db.cities.ensureIndex({name: "hashed"})

In future the ensureIndex() method won’t support.So you need to create the hashed index like below,

db.cities.createIndex({name: "hashed"})

6) Enable Sharding on the Collection

Here enable the cities collection

sh.shardCollection("demo.cities", {name: "hashed"})

To verify the status of the sharding operation using getShardDistribution() method

db.cities.getShardDistribution()

7) Analyze Shard Usage

To analyze shard usage, you need to run the explain() method along with your query. Below is an example of how to run a query to retrieve all documents from a collection and examine how the query is distributed across shards.

db.cities.find().explain()

Conclusion

Setting up a MongoDB sharded cluster may seem complex at first, but once the core components—config servers, shard replica sets, and the mongos query router—are properly configured, it provides a powerful and scalable foundation for managing large datasets.

By enabling sharding on specific collections and analyzing shard usage, you ensure optimal distribution and performance across your infrastructure. Whether you’re preparing for growth or already managing high-throughput applications, sharding equips your MongoDB deployment to handle it efficiently.

If you’re planning to implement sharding in a production environment, make sure to monitor your cluster regularly and choose appropriate shard keys to maintain balanced data distribution.

Thanks for reading! If you found this guide helpful, feel free to share, comment, or reach out with any questions or feedback.

Discover more from Genexdbs

Subscribe now to keep reading and get access to the full archive.

Continue reading