edit-icon download-icon

How to split write and read with MongoDB?

Last Updated: Jan 17, 2018

Replica sets in MongoDB store data in multiple copies for improved data reliability and support automatic primary/secondary failover for high service availability. However, the services are not highly available if a replica set is not properly connected.

Instructions before use

The primary node in a replica set is not always the same. In cases such as replica set rotation and update, primary node breakdown, and network partitioning, the replica set may elect a new primary node and the original primary node is downgraded to a secondary node, resulting in primary/secondary failover. This leads to constant change of the primary node.

If the replica set is connected using the primary address, data can be read and written correctly until the primary node is downgraded to a secondary node in the event of primary/secondary failover, which interrupts the write operation and seriously affects online services. Therefore, do not directly connect to the replica set using the primary address in the production environment.

Correct way to connect to a replica set

To correctly connect to a replica set, you must first understand the connection string URI of MongoDB. All official drivers support connection to MongoDB using a connection string.

A connection string includes the following content:



  • mongodb:// is a prefix indicating a connection string.

  • username:password@ indicates the user name and password that must be specified if authentication is enabled.

  • hostX:portX indicates the IP address and port number of each member in the replica set. Multiple members are separated by commas (,).

  • /database indicates the database to which the user account belongs during authentication.

  • ?options indicates additional connection options.

  • To implement read/write splitting, add readPreference=secondaryPreferred to options so that read requests are prioritized to secondary nodes. For more information about read options, see Read preferences.

  • Connections limit: Add maxPoolSize=xx to options to limit the client connection pool to a specified number (xx) of connections.

  • To confirm writes with the client only after data is successfully written to a majority of nodes, add w= majority to options. For more information about write options, see Write Concern.

Connection to ApsaraDB for MongoDB is used as an example here. When you buy an ApsaraDB for MongoDB replica set, you can obtain the name of the replica set and the addresses of replica set members. See the following figure.

Connection information

For ease of use, the MongoDB console generates replica set connection strings and Mongo shell connection commands. See the following figure.

Useful information

The following shows how to connect to ApsaraDB for MongoDB using Java.

  1. MongoClientURI connectionString = new MongoClientURI("mongodb://root:****@dds-*****.mongodb.rds.aliyuncs.com:3717,****.aliyuncs.com:3717/admin?replicaSet=mgset-677201"); // ****Replace it with the root password.
  2. MongoClient client = new MongoClient(connectionString);
  3. MongoDatabase database = client.getDatabase("mydb");
  4. MongoCollection<Document> collection = database.getCollection("mycoll");

When a replica set is connected using a correct connection string, the client automatically checks the primary/secondary relationship. If the relationship changes, writes are automatically switched to the new primary node to guarantee high service availability.

Thank you! We've received your feedback.