We earn commission when you buy through affiliate links.
This does not influence our reviews or recommendations.Learn more.
What is Sharding?
In practical terms, it is not feasible to store exponentially growing data on a single machine.
Horizontal Scalingworks with adding more servers and distribute the load on multiple servers.
But it requires additional maintenance of complex infrastructure with a large number of servers.
Mongo DB sharding works on the horizontal scaling technique.
Shards are required to be deployed in the replica set.
Mongosis a Mongo instance and acts as an interface between a client program and a sharded cluster.
It works as a query router to shards.
Config Serveris a Mongo instance which stores metadata information and configuration details of cluster.
MongoDB requires the config server to be deployed as a replica set.
Sharding Architecture
MongoDB cluster consists of a number of replica sets.
Each replica set consists of a minimum of 3 or more mongo instances.
The software interacts withMongos,which in turn communicates withshards.
Therefore in Sharding, applications never interact directly with shard nodes.
The query router distributes the subsets of data among shards nodes based upon the shard key.
Step 9
Insert data into the collection.
Step 10
The last step is to check the status of the sharding.
Status can be checked by running below command on the Mongos route node.
Sharding status
Check sharding status by running below command on the mongo route node.
Choosing a good shard key is extremely important.
MongoDB takes care of safely draining data to other shards nodes before the removal of the required shard node.
Run below command to remove the required shard.
First, we need to determine the hostname of the shard to be removed.
Below command will list all the shards present in the cluster along with the state of the shard.
Once removed sharded node will not be reflected in the output.
But if draining will be ongoing, the sharded node will come with draining status as true.
Finally, we need to check the remaining shards in the cluster.
I hope the above helps you to setup MongoDB sharding.
Next, you may want to get familiar with some of the commonly usedMongoDB commands.