MongoDB - Sharding
Sharding is the process of storing records of data across multiple machines, and is MongoDB's approach to meeting data growth requirements. As the size of the data grows, one machine may not be enough to store the data or provide reasonable read and write speeds. Sharding solves the horizontal scaling problem. With sharding, you add more machines to support data growth and read/write requirements.
- With replication, all writes go to the master node
- Latency sensitive requests are still sent to the master
- One replica set has a limit of 12 nodes
- Memory cannot be large enough when the active dataset is large
- Local disk is not big enough
- Vertical scaling is too expensive
Sharding in MongoDB
The following diagram shows sharding in MongoDB using a sharded cluster.
There are three main components in the following diagram:
Shards − Shards are used to store data. They provide high availability and data consistency. In a production environment, each shard is a separate replica set.
Config Servers - Config servers store cluster metadata. This data contains the mapping of the cluster dataset to shards. The request router uses this metadata to target operations to specific segments. In a production environment, sharded clusters have exactly 3 configuration servers.
Query Routers - Query Routers are basically mongo instances that interact with client applications and route operations to the appropriate segment. The request router processes and routes operations to shards and then returns the results to clients. A clustered cluster can contain more than one request router to share the load of client requests. The client sends requests to one request router. Typically, a sharded cluster has many request routers.