An ApsaraDB for MongoDB replica set instance provides multiple copies of data to ensure the high reliability of data. It also provides an automatic failover mechanism to guarantee the high availability of ApsaraDB for MongoDB. You must use a correct method to connect to a replica set instance to implement high availability. You can also configure the connection for read /write splitting.
- The primary node of a replica set instance may change. A failover between the primary and secondary nodes may be triggered when nodes of the replica set instance are upgraded in turn, the primary node is faulty, or the network is partitioned. In these scenarios, the replica set can elect a new primary node and downgrade the original primary node to a secondary node.
- If the primary node of a replica set instance is directly connected through the connection string of the primary node, the primary node will bear heavy load to process all read and write operations. If a failover is triggered in the replica set instance and the connected primary node is downgraded to a secondary node, you can no longer perform write operations and your business is seriously affected.
Connection string URIs
To correctly connect to a replica set instance, you must understand the format of connection string URIs of MongoDB. All official MongoDB drivers allow you to use a connection string URI to connect to MongoDB.
mongodb://: the prefix, indicating a connection string URI.
username:password@: the username and password used to log on to the database. If authentication is enabled, a password is required.
hostX:portX: the list of connection strings used to connect to nodes in the replica set instance. Each connection string consists of an IP address and a port number. Separate multiple connection strings with commas (,).
/database: the database corresponding to the username and password if authentication is enabled.
?options: additional connection options.
Use a connection string URI to connect to a replica set instance
You can use a connection string URI to connect to a replica set instance.
- Obtain the connection string URI of a replica set instance. For more information, see Overview of replica set instance connections.
- Use the obtained connection string URI to connect your applications to the instance.
For more information, see Connection sample code for MongoDB drivers.
For read /write splitting, you must add
readPreference=secondaryPreferredin the options parameter to set read preference to secondary nodes.
For more information about read preference options, see Read Preference.Example:
After you use the preceding method to connect to a replica set instance, a client can preferentially send read requests to secondary nodes to implement read/write splitting. The client also automatically detects the relationship between the primary and secondary nodes. If the primary node changes, the client automatically switches over write operations to the new primary node to ensure the high availability of ApsaraDB for MongoDB.