Storing data records across multiple machines is known as Sharding. A single machine can’t store all the data as the size of the data increases, It's become tough to provide an acceptable read and write throughput. To overcome this issue, database systems have two basic approaches Vertical scaling and Sharding or horizontal scaling.
Vertical Scaling
In vertical scaling the database requires some extra storage device to hold the data, like hard disk, memory and so on. This technique is known as vertical scaling but this approach increases the load on a single machine and increases the chance of system failover. It is also known as a scaling up approach.
Horizontal Scaling
This is also known as scaling out. In this approach we add a new node (server) to the system such that the entire load is distributed over all the servers. MongoDB uses a simple approach to do scaling out approach. It starts with a single or multiple nodes. If 10,000 new users connect with the application it adds another server.
Sharding or horizontal scaling divides the data set and distributes the data over multiple servers. These multiple server are known as shards. Each shard is an independent database and collectively, the shards make up a single logical database. Sharding distributes data over multiple shards so it reduces the number of operations for each shard. Each shard processes fewer operations since the size of the cluster is increased and as a result the cluster can increase capacity and throughput.
In the case of selection of a specific record the application doesn’t access the entire database system, it only accesses the desired shard responsible for that record. Sharding reduces the amount of data that each shard must store. For example, if a database contains 1 TB of data and we have 4 shards then each shard will contain approximate 250 GB of data. If we have 100 shards then each shard contains approximately 10 GB data.
Sharding In MongoDB
MongoDB does Sharding using a sharded cluster. A sharded cluster contain 3 components. The following describes these components:
-
Shards - It is use to store data. Each shard contains a replica set. It provide high data consistency and data availability.
-
Config Servers - It is use to store the metadata of clusters. A sharded cluster has exactly 3 config servers. These 3 config servers contain the mapping of the cluster’s data stored in shards. Config servers help the query router to select the desired shards to do the operations.


