Tool to create mongodb sharded cluster
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
MongoDB, a popular NoSQL database, is known for its high performance, high availability, and easy scalability. In larger applications, where data grows to a substantial size, setting up a MongoDB sharded cluster becomes essential. This type of cluster helps in distributing the data across multiple machines and managing the database's load effectively.
Understanding MongoDB Sharding
Sharding is a method for distributing data across multiple servers, where each shard holds a portion of the data and acts as an independent database. The overall cluster's operations are managed by query routers and configuration servers.
Components of a MongoDB Sharded Cluster
- Shards: Each shard contains a subset of the sharded data. Each shard can be deployed as a replica set for fault tolerance.
- Config Servers: These store metadata and configuration information about the cluster. Typically, a production cluster has three config servers.
- Query Routers (mongos instances): These act as the interface for client applications. They direct operations to the appropriate shards and return results to the clients.
Steps to Create a MongoDB Sharded Cluster
1. Set Up Config Servers
First, initiate the configuration servers. You typically need to start 3 config servers for redundancy.
Next, initiate the replica set for these configuration servers.
2. Start the Shard Servers
Each shard in the cluster is usually a replica set. Start mongod instances for each member of the replica set.
You will also need to initiate each shard as a replica set.
3. Start the mongos Instances
The mongos instances route queries to the correct shards and aggregate the results.
4. Add Shards to the Cluster
Once the config servers and shards are operational, connect to one of the mongos instances and add the shards.
Key Considerations
- Shard Key Selection: It is crucial to choose an appropriate shard key to ensure the even distribution of data and minimize hotspots.
- Redundancy and Fault Tolerance: Each shard can be a replica set to ensure data redundancy and high availability.
- Monitoring and Maintenance: Regular monitoring is essential for checking the health and performance of a sharded cluster.
Summary Table of Key Commands
| Action | Command |
| Start Config Server | mongod --configsvr --dbpath /data/configdb --port 27019 |
| Initiate Config Server RS | rs.initiate({...}) |
| Start Shard Server | mongod --shardsvr --dbpath /data/shard1 --port 27018 |
| Initiate Shard RS | rs.initiate({...}) |
| Start mongos Instance | mongos --configdb configReplSet/... --port 27017 |
| Add Shard | sh.addShard("shard1/...") |
Deploying a MongoDB sharded cluster involves multiple components and steps, but it substantially increases the database’s ability to handle large-scale operations efficiently. After setup, ongoing maintenance and optimization of the cluster will further enhance its performance.
Related reading
- Tradeoff between building own distributed system and using kubernetes to deploy my application
- Traefik Forward Authentication in k8s ingress controller
- Traefik v2.2 Ingress Route example not working
- Trouble connecting to postgres from outside Kubernetes cluster
- Tool to load balance tasks in distributed system
- Topic Exchange vs Direct Exchange in RabbitMQ
- transaction rollback for multiple databases
- Transaction synchronization in Spring Boot

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.