On this page本页内容
The following document explains common use cases for various read preference modes, as well as counter-indications outlining when you should not change the read preference from the default primary
.
Read Preference Mode | |
---|---|
primary |
Default mode. All operations read from the current replica set primary. Multi-document transactions that contain read operations must use read preference |
primaryPreferred |
In most situations, operations read from the primary but if it is unavailable, operations read from secondary members. Starting in version 4.4, |
secondary |
All operations read from the secondary members of the replica set. Starting in version 4.4, |
secondaryPreferred |
In most situations, operations read from secondary members but if no secondary members are available, operations read from the primary on sharded clusters. Starting in version 4.4, |
nearest |
Operations read from the member of the replica set with the least network latency, irrespective of whether that member is a primary or secondary. Starting in version 4.4, |
The following are common use cases for using non-primary
read preference modes:
If you have application servers in multiple data centers, you may consider having a geographically distributed replica set and using a non primary or nearest
read preference. This allows the client to read from the lowest-latency members, rather than always reading from the primary.
Use primaryPreferred
if you want an application to read from the primary under normal circumstances, but to allow stale reads from secondaries when the primary is unavailable. This provides a “read-only mode” for your application during a failover.
In general, do not use secondary
and secondaryPreferred
to provide extra capacity for reads, because:
Changed in version 3.6.在版本3.6中更改。Starting in MongoDB 3.6, clients can use Client Sessions and Causal Consistency Guarantees to ensure monotonic reads.
Sharding increases read and write capacity by distributing read and write operations across a group of machines, and is often a better strategy for adding capacity.
See Server Selection Algorithm for more information about the internal application of read preferences.
To avoid stale reads, use primary
read preference and "majority"
readConcern
. If the primary is unavailable, e.g. during elections or when a majority of the replica set is not accessible, read operations using primary
read preference produce an error or throw an exception.
In some circumstances, it may be possible for a replica set to temporarily have two primaries; however, only one primary will be capable of confirming writes with the "majority"
write concern.
P
old) into a partition with a minority of the nodes, while the other side of the partition contains a majority of nodes. The partition with the majority will elect a new primary (P
new), but for a brief period, the old primary (P
old) may still continue to serve reads and writes, as it has not yet detected that it can only see a minority of nodes in the replica set. During this period, if the old primary (P
old) is still visible to clients as a primary, reads from this primary may reflect stale data.P
old) may become unresponsive, which will trigger an election and a new primary (P
new) can be elected, serving reads and writes. If the unresponsive primary (P
old) starts responding again, two primaries will be visible for a brief period. The brief period will end when P
old steps down. However, during the brief period, clients might read from the old primary P
old, which can provide stale data.To increase consistency, you can disable automatic failover; however, disabling automatic failover sacrifices availability.
To permit read operations when possible, use primaryPreferred
. When there’s a primary you will get consistent reads [1], but if there is no primary you can still query secondaries. However, when using this read mode, consider the situation described in secondary vs secondaryPreferred.
[1] | In some circumstances, two nodes in a replica set may transiently believe that they are the primary, but at most, one of them will be able to complete writes with { w:
"majority" } write concern. The node that can complete { w: "majority" } writes is the current primary, and the other node is a former primary that has not yet recognized its demotion, typically due to a network partition. When this occurs, clients that connect to the former primary may observe stale data despite having requested read preference primary , and new writes to the former primary will eventually roll back. |
To always read from a low-latency node, use nearest
. The driver or mongos
will read from the nearest member and those no more than 15 milliseconds [2] further away than the nearest member.
nearest
does not guarantee consistency. If the nearest member to your application server is a secondary with some replication lag, queries could return stale data. nearest
only reflects network distance and does not reflect I/O or CPU load.
[2] | This threshold is configurable. See localPingThresholdMs for mongos or your driver documentation for the appropriate setting. |
If the members of a replica set are geographically distributed, you can create replica tags based that reflect the location of the instance and then configure your application to query the members nearby.
For example, if members in “east” and “west” data centers are tagged {'dc': 'east'}
and {'dc': 'west'}
, your application servers in the east data center can read from nearby members with the following read preference:
Although nearest
already favors members with low network latency, including the tag makes the choice more predictable.
secondary
vs secondaryPreferred
¶For specific dedicated queries (e.g. ETL, reporting), you may shift the read load from the primary by using the secondary
read preference mode. For this use case, the secondary
mode is preferable to the secondaryPreferred
mode because secondaryPreferred
risks the following situation: if all secondaries are unavailable and your replica set has enough arbiters [3] to prevent the primary from stepping down, then the primary will receive all traffic from the clients. If the primary is unable to handle this load, the queries will compete with the writes. For this reason, use read preference secondary
to distribute these specific dedicated queries instead of secondaryPreferred
.
[3] | In general, avoid deploying arbiters in replica sets and use an odd number of data-bearing nodes instead. If you must deploy arbiters, avoid deploying more than one arbiter per replica set. |