English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Sharding in MongoDB

Sharding is the process of storing data records across multiple machines, which is MongoDB's method to meet the needs of data growth. As the size of the data increases, a single machine may not be enough to store the data and may also not provide an acceptable read and write throughput. Sharding solves the problem of horizontal scaling. By sharding, you can add more computers to support the needs of data growth and read and write operations.

Why shard?

  • In replication, all writes are routed to the primary node

  • Queries that are sensitive to latency are still routed to the primary node

  • The limit of a single replica set is12a node

  • When the activity dataset is very large, memory may not be large enough

  • Local disk is not large enough

  • Vertical scaling is too expensive

Sharding in MongoDB

The following diagram shows the shards in MongoDB using a sharded cluster.

In the following diagram, there are three main components-

  • ShardsThe 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 ServersThe configuration servers store the metadata of the cluster. This data includes the mapping of the cluster dataset to the shards. The query router uses this metadata to direct operations to specific shards. In a production environment, a sharded cluster exactly has3configuration servers.

  • Query RoutersThe query router is basically a mongo instance, which can interface with client applications and directly route operations to the appropriate shard. The query router handles operations, locates them to the shard, and then returns the results to the client. A sharded cluster can contain multiple query routers to distribute the client request load. Clients send requests to a query router. Typically, a sharded cluster has many query routers.