What is Sharding in MongoDB? What are the types of Sharding?
MongoDB Sharding is a method for distributing data across different machines. Sharding is a technique used by MongoDB to facilitate installations with extremely big data sets and high bandwidth operations.
Basically, while working with larger datasets, the concept of sharding is utilized. This vast volume of data may present some problems when a query is done for them. This function helps distribute the problematic data among multiple MongoDB instances.
The larger collections in MongoDB are split out among different collections. These units are known as “shards.”
There are two methods for system growth: vertical and horizontal scaling.
Vertical scaling is boosting a single server’s capacity by using a more potent CPU, adding more RAM, or expanding the quantity of storage available. A single machine might not be powerful enough for a given task due to restrictions in the currently available technology. Furthermore, based on the hardware configurations that are available, cloud-based service providers have strict ceilings. Therefore, there is a reasonable upper limit to vertical scaling.
In horizontal scaling, the system dataset and load are distributed among several servers, and if needed, more servers are added to boost capacity. Even if a single machine may not have a high overall speed or capacity, each one manages a portion of the total workload, which may result in greater efficiency than a single high-speed, high-capacity server. It simply takes a few extra servers to increase the deployment’s capacity, which can be less expensive overall than buying high-end hardware for a single computer. Increased infrastructure complexity and deployment maintenance complexity is the price to pay.
Sharded Cluster
The sharded cluster consists of the following components:
- Shard
- Mongos
- Config servers
At the collection level, MongoDB shards data, dispersing it throughout the cluster’s shards.
Shard Keys
To spread the collection’s documents among shards, MongoDB employs the shard key. A field or a number of fields from the documents make up the shard key.
Chunks
MongoDB partitions sharded data into chunks. Based on the shard key, each chunk has an inclusive lower and an exclusive higher range.
Advantages of Sharding
- Reads / Writes
- Storage Capacity
- High Availability
Sharded and Non-Sharded Collections
There can be a combination of sharded and unsharded collections in a database. Sharded collections are divided up and dispersed throughout the cluster’s shards. Collections that are not sharded are kept on the primary shard. Each primary shard in a database is unique.