Introduction to "read Preferences" for MongoDB read-write separation

Source: Internet
Author: User
Tags string format

In some cases, it is reasonable to send a read request to the backup node of the replica set, for example, a single server cannot handle the read pressure on the application, and the query request can be routed to multiple servers in a replicable set. Most MongoDB drivers now support read preferences (read preference; or translate to read preferences) to tell the driver to read data from a particular node.

1 Read Preference Options

primary-This is the default setting, which indicates that data is read only from the primary node of the replicable set, and therefore has strong consistency. If there is a problem with the replicable set and there is no selectable slave node, the error is indicated.

Premarypreferred-the driver that sets this parameter reads data from the primary node, unless for some reason the primary node is unavailable or the master node is not available, and it reads the data from the node at that time. In this setting, read requests cannot guarantee consistency.

secondary-This setting tells the driver that data should be read from the node all the time. This setting is useful when we want to ensure that read requests do not affect write requests from the primary node. If no slave nodes are available, the read request throws an exception.

Secondarypreferred-read requests are emitted to the slave node and are not read from the primary node until they are not available from the node.

The nearest– driver attempts to read the read data from the most recent replicable set member node, judging by network latency. Can be either the primary node or the slave node. Therefore, the read request is sent only to the node that drives the fastest communication.

Primary is the only pattern that ensures consistent reading. Because the write request is first completed on the primary node, the update from the server is somewhat delayed, so the document data that has just been written to the master node may not be found in the slave node.

Summarizing the above knowledge, the node to which the data request is read under each preference is as follows:

2 Maximum Expiration Time

The Maxstalenessseconds settings have been added to MongoDB 3.4 and later.

The slave nodes of a replica set may lag behind the master node because of network congestion, low disk throughput, long execution, and so on. The Read settings maxstalenessseconds option allows you to define a maximum lag or "expire" time for read from a node. When the from node estimates that the expiration time exceeds maxstalenessseconds, the client stops using it for read operations.

The maximum expiration and primary modes do not match, only the select from node member read operation can be applied.

When a server that uses Maxstalenessseconds for read operations is selected, the client estimates the expiration of the slave node by comparing the last write time from the node and the primary node. The client points the connection to a slave node that is estimated to be less than or equal to maxstalenessseconds. If there is no master node, clients are compared using the most recent write from one node to another.

The default is no maximum expiration time and the client does not consider the lag from the node when pointing to the read operation.

The value of maxstalenessseconds must be defined to be greater than or equal to 90 seconds: defining a smaller value throws an exception. The client estimates the extent to which the replica set expires by periodically checking the last write time of each replica set member. Because the check is not frequent, so the estimate is rough. Therefore, the client cannot force maxstalenesssecconds less than 90 seconds.

3 Connection string Format

Replica set connection string format

mongodb://username:password@host1:port1,host2:port2[,..., hostn:portn]/database?options

Options are optional in the connection configuration, Replicaset, Readpreference, and Maxstalenessseconds are one of the subkeys.

Let's take an example to illustrate how the string is configured, and the test environment's replica set information is as follows:

Replica set name

Node role

Node IP

Port

Reptest

Primary server

172.171.x.xx1

27017

Replica node

172.171.x.xx2

27017

Quorum node

172.171.x.xx3

27017

The account information is as follows:

Username

Password

DBName

Mongousertest

Testuserpwd

Mongotestdb

If you want the program read request to be routed to the node secondary,100 seconds from the node data failure time, the CONNECTIONSTR string in the C # program can be set as follows:

Stringconnectionstr = "mongodb://mongousertest:testuserpwd@172.171.x.xx1:27017,172. 171.x. XX2:27017/mongotestdb?replicaset=reptest&readpreference=secondary&maxstalenessseconds=100 ";

This article copyright belongs to the author, without the consent of the author, thank you!!!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.