Three types of MongoDB servers are involved in sharding data. Each of these servers plays a specific role in presenting a single unified view to the applications. The following are the server types, and Figure 17.4 illustrates the interaction between them:
Shard: A shard stores the documents that make up the collection. A shard can be an individual server, but to provide high availability and data consistency in production, you should consider using a replica set that provides primary and secondary copies of the shard.
Query router: A query router runs an instance of mongos
. The query routers provide the interface for client applications to interact with the collection and obfuscate the fact that the data is in fact sharded. The query router processes a request, sends targeted operations to the shards, and then combines the shard responses into a single response to the client. A sharded cluster can contain more than one query router, which is a great way to load balance large numbers of client requests.
Config server: Config servers store the metadata about the sharded cluster that contains a mapping of the cluster’s data set to the shards. The query router uses this metadata when targeting operations to specific shards. A production sharded cluster should have exactly three config servers.