All Products
Search
Document Center

ApsaraDB for MongoDB:Read/write splitting and high availability for a replica set instance

Last Updated:Jun 20, 2026

ApsaraDB for MongoDB replica set instances support high availability and read/write splitting. To use these features, you must connect to your instance correctly and configure the necessary settings.

Background

The primary node of a replica set instance is not fixed. If the primary node fails, the system triggers a primary/secondary switchover, demoting the original primary to a secondary node.

If you connect to the database using the direct endpoint of the primary node, all read and write operations are sent to that node. This can cause a high load on the primary node. If a primary/secondary switchover occurs, the node you are connected to becomes a secondary node, and your client can no longer perform write operations. This can severely impact your business.

High availability

ApsaraDB for MongoDB provides a connection string URI to ensure high availability. This URI contains the addresses of all nodes in the instance. After a primary/secondary switchover, write requests are automatically routed to the new primary node, preventing interruptions to write operations.

For more information about how to connect to a replica set instance, see Connect to a replica set instance.

Note
  • To ensure high availability in a production environment, connect your applications to the instance using the connection string URI.

  • All official MongoDB drivers support connection string URIs.

Obtain the high-availability address

  1. Go to the Replica Set Instances page, select the resource group and region, and then click the ID of the target instance.

  2. In the navigation pane on the left, click Database Connections.

  3. Obtain the high-availability connection string URI. In the Private Connection - Virtual Private Cloud section, find the Connection String URI row. This row is marked as Recommended. Copy the address. The address is formatted as follows: mongodb://root:****@dds-InstanceID.mongodb.rds.aliyuncs.com:3717,dds-InstanceID.mongodb.rds.aliyuncs.com:3717/admin?replicaSet=mgset-ReplicaSetID.

    When you connect using the connection string URI, the client automatically detects the primary and secondary node roles. If a primary/secondary switchover occurs, the client automatically redirects write operations to the new primary node, ensuring high availability.

Read/write splitting

The connection string URI provides the readPreference and readPreferenceTags parameters. The readPreference parameter controls read/write splitting and load balancing. The readPreferenceTags parameter prioritizes routing read requests to nodes that have specific tags.

You can combine the readPreference and readPreferenceTags parameters to support various use cases.

Read failure fallback

Target node

Parameter

Route read requests to the primary node on failure

Prefer the primary node

&readPreference=primaryPreferred

Prefer secondary and read-only nodes

&readPreference=secondaryPreferred

Prefer secondary nodes (cloud disk-based instances only)

&readPreference=secondaryPreferred&readPreferenceTags=role:electable

Prefer read-only nodes

&readPreference=secondaryPreferred&readPreferenceTags=role:readonly

Do not route read requests to the primary node on failure

Read only from secondary and read-only nodes

&readPreference=secondary

Read only from secondary nodes (cloud disk-based instances only)

&readPreference=secondary&readPreferenceTags=role:electable

Read only from read-only nodes

&readPreference=secondary&readPreferenceTags=role:readonly

Read/write splitting with a connection string URI

To implement read/write splitting, append the readPreference and readPreferenceTags parameters to the connection string URI.

For example, to route all write requests to the primary node and read requests exclusively to read-only nodes without fallback, append &readPreference=secondary&readPreferenceTags=role:readonly to the connection string URI. The following example shows a complete URI:

mongodb://root:****@dds-bp19f409d7512****.mongodb.rds.aliyuncs.com:3717,dds-bp19f409d7512****.mongodb.rds.aliyuncs.com:3717/admin?replicaSet=mgset-6108****&readPreference=secondary&readPreferenceTags=role:readonly
Note

The test environment used the following driver versions:

  • pymongo 3.11.4

  • mongodb-driver-sync 4.6.1 

  • mongosh 2.2.15

References