Help Center> Document Database Service> Best Practices> How Do I Prevent the Mongos Cache Problem?
Updated on 2023-09-26 GMT+08:00

How Do I Prevent the Mongos Cache Problem?

Background

DDS is a document-oriented database service based on distributed file storage, famed for its scalability, high performance, open source, and free mode.

Figure 1 DDS cluster architecture

A cluster instance consists of the following three parts:

  • Mongos is deployed on a single node. It provides APIs to allow access from external users and shields the internal complexity of the distributed database. A DDS cluster can contain 2 to 12 mongos. You can add them as required.
  • Config server is deployed as a replica set. It stores metadata for a sharded cluster. The metadata include information about routes and shards. A cluster contains only one config server.
  • Shard server is deployed as a replica set. It stores user data on shards. You can add shard servers in a cluster as required.

Sharding

Sharding is a method for distributing data evenly across multiple shard servers based on a specified shard key. The collection that has a shard key is called sharded collection. If the collection is not sharded, data is stored on only one shard server. DDS cluster mode allows the coexistence of sharded collection and non-sharded collection.

You can run the sh.shardCollection command to convert a non-sharded collection into a sharded collection. Before sharding, ensure that the sharding function is enabled on the database where the collections to be sharded are located. You can run the sh.enableSharding command to enable the sharding function.

Caching Metadata with mongos

User data is stored in the shard server and metadata is stored in the config server. The route information belongs to metadata and is also stored in the config server. When a user needs to access data through mongos, mongos sends the user's requests to the corresponding shard server according to the route information stored on the config server.

This means that every time the user accesses the data, mongos needs to connect to the config server for the route information, which may affect the system performance. Therefore, a cache mechanism is developed for the mongos to cache the route information of the config server. In this scenario, not only the config server stores the route information, but also the mongos caches the route information.

If no operation is performed on mongos, mongos does not cache any route information. In addition, the route information cached on mongos may not be the latest because the information is only updated in the following scenarios:

  • If the mongos is started, it will obtain the latest route information from the config server and caches them locally.
  • If the mongos processes the data request for the first time, it will obtain the route information from the config server. After that, the information is cached and can be used directly at the time when it is required.
  • Updating route information by running commands on mongos.

Only the metadata related to the requested data is updated.

The data to be updated is in the unit of DB.

Scenarios

In the scenario where data is not sharded and multiple mongos exist in a sharded cluster, if data is accessed through different mongos, the cached route information on each mongos may become different. The following shows an example scenario:

  1. Create database A with sharding disabled through mongos1. After data1 is written, data1 is allocated to shard server1 for storage. Then, mongos2 is used to query data. Both mongos1 and mongos2 have cached the route information of database A.
  2. If database A is deleted through mongos2, the information about database A in the config server and shard server1 is deleted. As a result, mongos1 cannot identify data1 because database A has been deleted.
  3. When data2 is written to database A through mongos1, data2 will be stored on shard server1 based on the cached route information but actually database A has been deleted. Then, when data3 is written into database A through mongos2, new information about database A will be generated again on the config server and shard server2 because mongos2 has identified that database A has been deleted.
  4. In this case, the route information cached in the mongos1 and mongos2 is inconsistent. mongos1 and mongos2 are associated with different shard servers, and data is not shared between them. As a result, data inconsistency occurs.
Figure 2 mongos cache defect scenario

The client queries data through different mongos:

  • mongos1: Data2 can be queried, but data3 cannot be queried.
  • mongos2: Data3 can be queried, but data2 cannot be queried.

Workaround Suggestion

MongoDB official suggestions: After deleting databases or collections, run db.adminCommand("flushRouterConfig") on all mongos nodes to update the route information.

Reference link:

Workaround Suggestion

  • For the cluster mode, you are advised to enable the sharding function and then shard the collections in the cluster.
  • If the database with sharding disabled is deleted, do not create a database or collection with the same name as the deleted database or collection.

    If you need to create a database or collection with the same name as the deleted database or collection, log in to all the mongos nodes to update the route information before creating the database and collection.