All Products
Search
Document Center

ApsaraDB for MongoDB:Connect to a replica set instance and implement read/write splitting and high availability

Last Updated:May 17, 2024

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.

Precautions

  • 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://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]
The parameters used in the connection string URI:
  • 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 name of the database to which you want to connect. Default value: admin.
  • ?options: the additional options that are used to connect to the instance.

    A common option is authSource=<authenticationDatabase>. This parameter is used to specify the database to which the specified database account belongs. <authenticationDatabase>: the name of the database used for authentication. For the authentication to succeed, the specified database account must belong to the specified authentication database. If you do not specify the authentication database, the database specified by <database> is used as the authentication database.

Note For more information about connection string URIs, see connection String URI Format.

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.

  1. Obtain the connection string URI of a replica set instance. For more information, see Overview of replica set instance connections.
  2. Use the obtained connection string URI to connect your applications to the instance. For more information, see Connection sample code for MongoDB drivers.
    Note For read/write splitting, you must add readPreference=secondaryPreferred in the options parameter to set read preference to secondary nodes. For more information about read preference options, see Read Preference.

    Example:

    The database account is test and the database is admin.
    mongodb://test:****@dds-bp19f409d7512****.mongodb.rds.aliyuncs.com:3717,dds-bp19f409d7512****.mongodb.rds.aliyuncs.com:3717/admin?replicaSet=mgset-6108****&readPreference=secondaryPreferred

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.