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.
-
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
-
Go to the Replica Set Instances page, select the resource group and region, and then click the ID of the target instance.
-
In the navigation pane on the left, click Database Connections.
-
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 |
|
|
Prefer secondary and read-only nodes |
|
|
|
Prefer secondary nodes (cloud disk-based instances only) |
|
|
|
Prefer read-only nodes |
|
|
|
Do not route read requests to the primary node on failure |
Read only from secondary and read-only nodes |
|
|
Read only from secondary nodes (cloud disk-based instances only) |
|
|
|
Read only from read-only nodes |
|
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
The test environment used the following driver versions:
-
pymongo 3.11.4
-
mongodb-driver-sync 4.6.1
-
mongosh 2.2.15