A shard is a single MongoDB instance that holds a subset of the sharded data. Shards can be deployed as replica sets to increase availability and provide redundancy. The combination of multiple shards creates a complete data set.
Sharding is the process of distributing data across multiple hosts. In MongoDB, sharding is achieved by splitting large data sets into small data sets across multiple MongoDB instances.
The underlying hardware becomes the main limitation when dealing with high throughput applications or very large databases. High query rates can stress disk drives' CPU, RAM, and I/O capacity, resulting in a poor end-user experience. There are two types of scaling methods.
Vertical scaling is the traditional way of increasing the hardware capabilities of a single server. The process involves upgrading the CPU, RAM, and storage capacity. However, technological limitations and cost constraints often challenge upgrading a single server.
Horizontal scaling
This method divides the dataset into multiple servers and distributes the database load among each server instance. Distributing the load reduces the strain on the required hardware resources and provides redundancy in case of failure.
However, horizontal scaling increases the complexity of underlying architecture. MongoDB supports horizontal scaling through sharding.
MongoDB sharding works by creating a cluster of MongoDB instances consisting of at least three servers. That means sharded clusters are comprised of three main components:
A shard is a single MongoDB instance that holds a subset of the sharded data. Shards can be deployed as replica sets to increase availability and provide redundancy. The combination of multiple shards creates a complete data set. For example, a 2 TB data set can be broken down into four shards containing 500 GB of data from the original data set.
Mongos act as the query router providing a stable interface between the application and the sharded cluster. This MongoDB instance is responsible for routing the client requests to the correct shard.
Configuration servers store the metadata and the configuration settings for the whole cluster.
It’s important to remember that the config servers also work as replica sets.
When sharding a MongoDB collection, a shard key gets created as one of the initial steps. The “shard key” is used to distribute the MongoDB collection’s documents across all the shards. The key consists of a single field or multiple fields in every document. The sharded key is immutable and cannot be changed after sharding, and a sharded collection only contains a single shard key.
The shard key can directly impact the performance of the cluster. Hence can lead to bottlenecks in applications associated with the cluster. To mitigate this, before sharding the collection, the shard key must be created based on:
Chunks are subsets of shared data. MongoDB separates sharded data into chunks distributed across the shards in the shared cluster. Based on the shard key, each chunk has an inclusive lower and exclusive upper range. A balancer specific for each cluster handles the chunk distribution.
The balancer runs as a background job and distributes the chunks needed to achieve an even balance of chunks across all shards. This process is called even chuck distribution.
|