§2023-07-26
¶What is MongoDB Sharding?
Sharding is a method for distributing or partitioning data across multiple machines.
It is useful when no single machine can handle large modern-day workloads, by allowing you to scale horizontally.
Horizontal scaling, also known as scale-out, refers to adding machines to share the data set and load. Horizontal scaling allows for near-limitless scaling to handle big data and intense workloads.
¶Sharding Architecture
In MongoDB, a sharded cluster consists of:
- Shards
- Mongos
- Config servers
A shard is a replica set that contains a subset of the cluster’s data.
The mongos acts as a query router for client applications, handling both read and write operations. It dispatches client requests to the relevant shards and aggregates the result from shards into a consistent client response. Clients connect to a mongos, not to individual shards.
Config servers are the authoritative source of sharding metadata. The sharding metadata reflects the state and organization of the sharded data. The metadata contains the list of sharded collections, routing information, etc.
In its simplest configuration (a single shard), a sharded cluster will look like this:
In a production cluster, ensure that data is redundant and that your systems are highly available. Consider the following for a production sharded cluster deployment:
- Deploy Config Servers as a 3 member replica set
- Deploy each Shard as a 3 member replica set
- Deploy one or more mongos routers