Read Preference Use Cases阅读首选项用例

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 Modes读取首选项模式

Read Preference Mode读取首选项模式Description描述
primary

Default mode. 默认模式。All operations read from the current replica set primary.从当前副本集primary读取的所有操作。

Multi-document transactions that contain read operations must use read preference primary. 包含读取操作的多文档事务必须使用读取首选项primaryAll operations in a given transaction must route to the same member.给定事务中的所有操作都必须路由到同一成员。

primaryPreferred

In most situations, operations read from the primary but if it is unavailable, operations read from secondary members.在大多数情况下,操作从primary读取,但如果主成员不可用,则操作从secondary成员读取。

Starting in version 4.4, primaryPreferred supports hedged reads on sharded clusters.从4.4版开始,primaryPreferred支持分片集群上的对冲读取

secondary

All operations read from the secondary members of the replica set.从副本集的secondary成员读取的所有操作。

Starting in version 4.4, secondary supports hedged reads on sharded clusters.从4.4版开始,secondary支持分片集群上的对冲读取

secondaryPreferred

In most situations, operations read from secondary members but if no secondary members are available, operations read from the primary on sharded clusters.在大多数情况下,操作从secondary成员读取,但如果没有secondary成员可用,则操作从分片集群上的primary成员读取。

Starting in version 4.4, secondaryPreferred supports hedged reads on sharded clusters.从4.4版开始,secondaryPreferred支持分片集群上的对冲读取

nearest

Operations read from a random eligible replica set member, irrespective of whether that member is a primary or secondary, based on a specified latency threshold. 基于指定的延迟阈值,从随机合格副本集成员读取的操作,无论该成员是primary还是secondaryThe operation considers the following when calculating latency:该操作在计算延迟时考虑以下因素:

Starting in version 4.4, nearest supports hedged reads on sharded clusters and enables the hedged read option by default.从4.4版开始,nearest支持分片集群上的对冲读取,并默认启用对冲读取选项。

Indications to Use Non-Primary Read Preference使用非主要读取首选项的指示

The following are common use cases for using non-primary read preference modes:以下是使用非primary读取首选项模式的常见用例:

  • Running systems operations that do not affect the front-end application.运行不影响前端应用程序的系统操作。

    Note注意

    Read preferences aren't relevant to direct connections to a single mongod instance. 读取首选项与到单个mongod实例的直接连接无关。However, in order to perform read operations on a direct connection to a secondary member of a replica set, you must set a read preference, such as secondary.但是,为了在与副本集的次要成员的直接连接上执行读取操作,必须设置读取首选项,例如secondary

  • Providing local reads for geographically distributed applications.为地理分布的应用程序提供本地读取。

    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. 如果您在多个数据中心中有应用程序服务器,您可以考虑使用地理分布的副本集,并使用非主要或nearest读取首选项。This allows the client to read from the lowest-latency members, rather than always reading from the primary.这允许客户端从延迟最低的成员读取数据,而不是总是从主成员读取数据。

  • Maintaining availability during a failover.在故障切换期间保持可用性。

    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. 如果您希望应用程序在正常情况下从主应用程序读取数据,但在主应用程序不可用时允许从辅助应用程序读取过时数据,请使用primaryPreferredThis provides a "read-only mode" for your application during a failover.这在故障切换期间为应用程序提供了“只读模式”。

Counter-Indications for Non-Primary Read Preference非主要读取首选项的计数器指示

In general, do not use secondary and secondaryPreferred to provide extra capacity for reads, because:通常,不要使用secondarysecondaryPreferred为读取提供额外容量,因为:

  • All members of a replica have roughly equivalent write traffic; as a result, secondaries will service reads at roughly the same rate as the primary.复制副本的所有成员具有大致相等的写入流量;因此,辅助服务器将以与主服务器大致相同的速率服务读取。
  • Replication is asynchronous and there is some amount of delay between a successful write operation and its replication to secondaries. 复制是异步的,成功的写入操作与其复制到辅助设备之间存在一定的延迟。Reading from a secondary can return stale data; reading from different secondaries may result in non-monotonic reads.从辅助设备读取可能会返回过时的数据;从不同的二级读取可能导致非单调读取。

    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.从MongoDB 3.6开始,客户端可以使用客户端会话和因果一致性保证来确保单调读取。

  • Distributing read operations to secondaries can compromise availability if any members of the set become unavailable because the remaining members of the set will need to be able to handle all application requests.如果集合中的任何成员变得不可用,则将读取操作分发给辅助服务器可能会影响可用性,因为集合中的其余成员需要能够处理所有应用程序请求。

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.有关读取首选项的内部应用的更多信息,请参阅服务器选择算法。

Maximize Consistency最大化一致性

To avoid stale reads, use primary read preference and "majority" readConcern. 要避免过时的读取,请使用primary读取首选项和"majority" readConcernIf 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.如果主副本不可用,例如,在选举期间或当大多数副本集不可访问时,使用primary读取首选项的读取操作会产生错误或引发异常。

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.在某些情况下,副本集可能暂时有两个主副本;然而,只有一个主服务器能够确认"majority"写入问题的写入。

  • A partial network partition may segregate a primary (Pold) into a partition with a minority of the nodes, while the other side of the partition contains a majority of nodes. 部分网络分区可以将主(P旧)分区为具有少数节点的分区,而分区的另一侧包含大多数节点。The partition with the majority will elect a new primary (Pnew), but for a brief period, the old primary (Pold) 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. 多数分区将选择一个新的主分区(Pnew),但在很短的一段时间内,旧的Primary(Pold)仍可能继续执行读写操作,因为它尚未检测到只能在副本集中看到少数节点。During this period, if the old primary (Pold) is still visible to clients as a primary, reads from this primary may reflect stale data.在此期间,如果旧的Primary(Pold)仍然作为主对客户端可见,则从该主读取的数据可能反映过时的数据。
  • A primary (P old) may become unresponsive, which will trigger an election and a new primary (P new) can be elected, serving reads and writes. 初选(P old)可能变得无反应,这将触发选举,新的初选(P new)可以被选出,提供读写服务。If the unresponsive primary (P old) starts responding again, two primaries will be visible for a brief period. 如果无响应的主(P old)再次开始响应,则两个主将在短时间内可见。The brief period will end when Pold steps down. Pold下台时,短暂的时期将结束。However, during the brief period, clients might read from the old primary P old, which can provide stale data.然而,在短暂的时间内,客户端可能会从旧的PrimaryP old文件中读取数据,这可能会提供过时的数据。

To increase consistency, you can disable automatic failover; however, disabling automatic failover sacrifices availability.为了提高一致性,您可以禁用自动故障切换;然而,禁用自动故障切换会牺牲可用性。

Maximize Availability最大化可用性

To permit read operations when possible, use primaryPreferred. 若要在可能的情况下允许读取操作,请使用primaryPreferredWhen there's a primary you will get consistent reads [1], but if there is no primary you can still query secondaries. 当有主服务器时,您将获得一致的读取[1],但如果没有primary,您仍然可以查询secondariesHowever, when using this read mode, consider the situation described in secondary vs secondaryPreferred.但是,在使用此读取模式时,请考虑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. 在某些情况下,副本集中的两个节点可能会暂时认为自己是主节点,但最多只有一个节点能够使用{ w: "majority" }完成写操作。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. 可以完成{ w: "majority" }写入的节点是当前主节点,而另一个节点是尚未识别其降级的前主节点,通常是由于网络分区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.发生这种情况时,连接到前一个主服务器的客户端可能会观察到过时的数据,尽管请求了读取首选项primary,但对前一个主机的新写入最终会回滚。

Minimize Latency最小化延迟

To always read from a low-latency node, use nearest. 要始终从低延迟节点读取,请使用nearestThe driver or mongos will read from the nearest member and those no more than 15 milliseconds [2] further away than the nearest member.驱动程序或mongos将读取最近的成员以及距离最近成员不超过15毫秒[2]的成员。

nearest does not guarantee consistency. nearest不保证一致性。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.nearest仅反映网络距离,而不反映I/O或CPU负载。

[2] This threshold is configurable. 此阈值是可配置的。See localPingThresholdMs for mongos or your driver documentation for the appropriate setting.有关适当的设置,请参阅mongoslocalPingThresholdMs或驱动程序文档。

Query From Geographically Distributed Members从地理分布的成员查询

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:例如,如果“east”和“west”数据中心中的成员标记{'dc': 'east'}{'dc': 'west'},则您在east数据中心的应用程序服务器可以使用以下读取首选项从附近的成员读取:

db.collection.find().readPref('nearest', [ { 'dc': 'east' } ])

Although nearest already favors members with low network latency, including the tag makes the choice more predictable.尽管nearest已经倾向于网络延迟较低的成员,但包括标签使选择更具可预测性。

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. 对于特定的专用查询(例如ETL、报告),可以使用secondary读取首选项模式将读取负载从主查询转移。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. 对于这种使用情况,secondary模式比secondaryPreferred模式更可取,因为secondaryPreferred模式存在以下风险:如果所有次要模式都不可用,并且您的副本集有足够的仲裁器[3]来防止主要模式降级,则主要模式将接收来自客户端的所有流量。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.因此,使用读取首选项secondary来分发这些特定的专用查询,而不是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.如果必须部署仲裁器,请避免为每个副本集部署多个仲裁器。
←  Hedged Read OptionServer Selection Algorithm →