Read Preference读取首选项

On this page本页内容

Read preference describes how MongoDB clients route read operations to the members of a replica set.读取首选项描述MongoDB客户端如何将读取操作路由到副本集的成员。

Read operations to a replica set. Default read preference routes the read to the primary. Read preference of ``nearest`` routes the read to the nearest member.

By default, an application directs its read operations to the primary member in a replica set (i.e. read preference mode "primary"). 默认情况下,应用程序将其读取操作定向到副本集中的primary成员(即读取首选项模式“primary”)。But, clients can specify a read preference to send read operations to secondaries.但是,客户端可以指定一个读取首选项,以便将读取操作发送到辅助设备。

Read preference consists of the read preference mode and optionally, a tag set, the maxStalenessSeconds option, and the hedged read option. 读取首选项包括读取首选项模式和可选的标记集maxStalenessSeconds选项和对冲读取选项。Hedged read option is available for MongoDB 4.4+ sharded clusters for reads that use non-primary read preference.对冲读取选项可用于MongoDB 4.4+分片化集群,用于使用非primary读取首选项的读取。

The following table lists a brief summary of the read preference modes:下表列出了读取首选项模式的简要摘要:

Note注意

Starting in version 4.4, non-primary read preference modes support hedged read on sharded clusters.从4.4版开始,非primary读取偏好模式支持分片集群上的对冲读取

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读取,但如果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成员还是secondary成员。The 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支持分片集群上的对冲读取,并默认启用对冲读取选项。

For detailed description of the read preference modes, see Read Preference Modes.有关读取首选项模式的详细说明,请参阅读取首选项

Behavior行为

  • All read preference modes except primary may return stale data because secondaries replicate operations from the primary in an asynchronous process. primary外的所有读取首选项模式都可能返回过时数据,因为secondaries在异步进程中从primary复制操作。[1] Ensure that your application can tolerate stale data if you choose to use a non-primary mode.如果选择使用非primary模式,请确保应用程序可以容忍过时数据。
  • Read preference does not affect the visibility of data; i.e. clients can see the results of writes before they are acknowledged or have propagated to a majority of replica set members. 读取偏好不影响数据的可见性;即,客户端可以在写入结果被确认或传播到大多数副本集成员之前看到写入结果。For details, see Read Isolation, Consistency, and Recency.有关详细信息,请参阅读取隔离、一致性和最近性
  • Read preference does not affect causal consistency. 阅读偏好不会影响因果一致性The causal consistency guarantees provided by causally consistent sessions for read operations with "majority" read concern and write operations with "majority" write concern hold across all members of the MongoDB deployment.对于具有"majority"读取关注的读操作和具有"majority"写入关注的写操作,MongoDB部署的所有成员都具有因果一致性会话提供的因果一致性保证

Read Preference Modes读取首选项模式

primary

All read operations use only the current replica set primary. 所有读取操作仅使用当前副本集主primary[1] This is the default read mode. If the primary is unavailable, read operations produce an error or throw an exception.这是默认的读取模式。如果主节点不可用,则读取操作会产生错误或引发异常。

The primary read preference mode is not compatible with read preference modes that use tag sets or maxStalenessSeconds. primary读取首选项模式与使用标记集maxStalenessSeconds的读取首选项不兼容。If you specify tag sets or a maxStalenessSeconds value with primary, the driver will produce an error.如果使用primary指定标记集或maxStalenessSeconds值,驱动程序将产生错误。

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 member of the set. 在大多数情况下,操作从集合的primary成员读取。However, if the primary is unavailable, as is the case during failover situations, operations read from secondary members that satisfy the read preference's maxStalenessSeconds and tag sets.但是,如果主成员不可用(如故障切换情况下的情况),则从满足读取首选项maxStalenessSeconds和标记集的secondary成员读取操作。

When the primaryPreferred read preference includes a maxStalenessSeconds value and there is no primary from which to read, the client estimates how stale each secondary is by comparing the secondary's last write to that of the secondary with the most recent write. primaryPreferred读取首选项包含maxStalenessSeconds,并且没有要从中读取的主项时,客户端通过将次项的上次写入与次项的最新写入进行比较来估计每个次项的过时程度。The client then directs the read operation to a secondary whose estimated lag is less than or equal to maxStalenessSeconds.然后,客户端将读取操作定向到估计滞后小于或等于maxStalenessSeconds的辅助设备。

When the read preference includes a tag set (i.e. a list of tag specifications) and there is no primary from which to read, the client attempts to find secondary members with matching tags (trying the tag specifications in order until a match is found). 当读取首选项包括标签集(即标签规范列表)并且没有要读取的主要成员时,客户端尝试查找具有匹配标签的次要成员(按顺序尝试标签规范,直到找到匹配)。If matching secondaries are found, the client selects a random secondary from the nearest group of matching secondaries. 如果找到匹配的辅助服务器,则客户端从最近的匹配辅助服务器组中随机选择一个辅助服务器。If no secondaries have matching tags, the read operation produces an error.如果没有辅助设备具有匹配的标记,则读取操作将产生错误。

When the read preference includes a maxStalenessSeconds value and a tag set, the client filters by staleness first and then by the specified tags.当读取首选项包含maxStalenessSeconds值和标记集时,客户端会先按过时性进行筛选,然后按指定的标记进行筛选。

Read operations using the primaryPreferred mode may return stale data. Use the maxStalenessSeconds option to avoid reading from secondaries that the client estimates are overly stale.使用primaryPreferred模式的读取操作可能会返回过时的数据。使用maxStalenessSeconds选项可以避免从辅助设备读取客户机估计的数据过于陈旧。

Note注意

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

secondary

Operations read only from the secondary members of the set. 仅从集合的secondary成员读取的操作。If no secondaries are available, then this read operation produces an error or exception.如果没有可用的辅助设备,则此读取操作会产生错误或异常。

Most replica sets have at least one secondary, but there are situations where there may be no available secondary. 大多数副本集至少有一个辅助副本,但在某些情况下可能没有可用的辅助副本。For example, a replica set with a primary, a secondary, and an arbiter may not have any secondaries if a member is in recovering state or unavailable.例如,如果成员处于恢复状态或不可用,则具有primary、secondary和仲裁器的副本集可能没有任何辅助副本集。

When the secondary read preference includes a maxStalenessSeconds value, the client estimates how stale each secondary is by comparing the secondary's last write to that of the primary. secondary读取首选项包含maxStalenessSeconds时,客户端通过将辅助读取的上次写入与主读取的写入进行比较来估计每个辅助读取的过时程度。The client then directs the read operation to a secondary whose estimated lag is less than or equal to maxStalenessSeconds. 然后,客户端将读取操作定向到估计滞后小于或等于maxStalenessSeconds的辅助设备。If there is no primary, the client uses the secondary with the most recent write for the comparison.如果没有主文件,则客户端使用具有最新写入的次文件进行比较。

When the read preference includes a tag set (i.e. a list of tag specifications), the client attempts to find secondary members with matching tags (trying the tag specifications in order until a match is found). 当读取偏好包括标签集(即标签规范列表)时,客户端尝试查找具有匹配标签的次要成员(按顺序尝试标签规范,直到找到匹配)。If matching secondaries are found, the client selects a random secondary from the nearest group of matching secondaries. 如果找到匹配的辅助服务器,则客户端从最近的匹配辅助服务器组中随机选择一个辅助服务器。If no secondaries have matching tags, the read operation produces an error.如果没有辅助设备具有匹配的标记,则读取操作将产生错误。

When the read preference includes a maxStalenessSeconds value and a tag set, the client filters by staleness first and then by the specified tags.当读取首选项包含maxStalenessSeconds值和标记集时,客户端会先按过时性进行筛选,然后按指定的标记进行筛选。

Read operations using the secondary mode may return stale data. 使用secondary模式的读取操作可能会返回过时的数据。Use the maxStalenessSeconds option to avoid reading from secondaries that the client estimates are overly stale.使用maxStalenessSeconds选项可以避免从辅助设备读取客户机估计的数据过于陈旧。

Note注意

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 in situations where the set consists of a single primary (and no other members), the read operation will use the replica set's primary.在大多数情况下,操作从secondary成员读取,但在集由单个primary(没有其他成员)组成的情况下,读取操作将使用副本集的主成员。

When the secondaryPreferred read preference includes a maxStalenessSeconds value, the client estimates how stale each secondary is by comparing the secondary's last write to that of the primary. secondaryPreferred读取首选项包含maxStalenessSeconds时,客户端通过将次文件的上次写入与主文件的写入进行比较来估计每个次文件的过时程度。The client then directs the read operation to a secondary whose estimated lag is less than or equal to maxStalenessSeconds. 然后,客户端将读取操作定向到估计滞后小于或等于maxStalenessSeconds的辅助设备。If there is no primary, the client uses the secondary with the most recent write for the comparison. 如果没有主文件,则客户端使用具有最新写入的次文件进行比较。If there are no secondaries with estimated lag less than or equal to maxStalenessSeconds, the client directs the read operation to the replica set's primary.如果没有估计延迟小于或等于maxStalenessSeconds的辅助副本,则客户端将读取操作定向到副本集的主副本集。

When the read preference includes a tag set (i.e. a list of tag specifications), the client attempts to find secondary members with matching tags (trying the tag specifications in order until a match is found). 当读取偏好包括标签集(即标签规范列表)时,客户端尝试查找具有匹配标签的次要成员(按顺序尝试标签规范,直到找到匹配)。If matching secondaries are found, the client selects a random secondary from the nearest group of matching secondaries. 如果找到匹配的辅助服务器,则客户端从最近的匹配辅助服务器组中随机选择一个辅助服务器。If no secondaries have matching tags, the client ignores tags and reads from the primary.如果没有辅助服务器具有匹配的标记,则客户端将忽略标记并从主服务器读取。

When the read preference includes a maxStalenessSeconds value and a tag set, the client filters by staleness first and then by the specified tags.当读取首选项包含maxStalenessSeconds值和标记集时,客户端会先按过时性进行筛选,然后按指定的标记进行筛选。

Read operations using the secondaryPreferred mode may return stale data. 使用secondaryPreferred模式的读取操作可能会返回过时的数据。Use the maxStalenessSeconds option to avoid reading from secondaries that the client estimates are overly stale.使用maxStalenessSeconds选项可以避免从辅助设备读取客户机估计的数据过于陈旧。

Note注意

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

nearest

The driver reads from a member whose network latency falls within the acceptable latency window. 驱动程序读取网络延迟在可接受延迟窗口内的成员。Reads in the nearest mode do not consider whether a member is a primary or secondary when routing read operations: primaries and secondaries are treated equivalently.在路由读取操作时,nearest模式下的读取不考虑成员是primary还是secondary: primary和secondary被同等对待。

Set this mode to minimize the effect of network latency on read operations without preference for current or stale data.设置此模式以最小化网络延迟对读取操作的影响,而不首选当前数据或过时数据。

When the read preference includes a maxStalenessSeconds value, the client estimates how stale each secondary is by comparing the secondary's last write to that of the primary, if available, or to the secondary with the most recent write if there is no primary. 当读取首选项包含maxStalenessSeconds时,客户端通过将次文件的最后一次写入与主文件的写入(如果可用)进行比较,或者将次文件与最近一次写入(如果没有主文件)进行比较来估计每个次文件的过时程度。The client will then filter out any secondary whose estimated lag is greater than maxStalenessSeconds and randomly direct the read to a remaining member (primary or secondary) whose network latency falls within the acceptable latency window.然后,客户端将筛选掉任何估计延迟大于maxStalenessSeconds的次要成员,并将读取随机引导到网络延迟在可接受的延迟窗口内的剩余成员(主要或次要)。

If you specify a tag set, the client attempts to find a replica set member that matches the specified tag sets and directs reads to an arbitrary member from among the nearest group.如果指定标记集,则客户端将尝试查找与指定标记集匹配的副本集成员,并将读取指向最近组中的任意成员。

When the read preference includes a maxStalenessSeconds value and a tag set, the client filters by staleness first and then by the specified tags. 当读取首选项包含maxStalenessSeconds值和标记集时,客户端会先按过时性进行筛选,然后按指定的标记进行筛选。From the remaining mongod instances, the client then randomly directs the read to an instance that falls within the acceptable latency window. 然后,客户端从剩余的mongod实例中随机地将读取引导到一个在可接受的延迟窗口内的实例。The read preference member selection documentation describes the process in detail.阅读首选项成员选择文档详细描述了该过程。

Read operations using the nearest mode may return stale data. 使用nearest模式的读取操作可能会返回过时的数据。Use the maxStalenessSeconds option to avoid reading from secondaries that the client estimates are overly stale.使用maxStalenessSeconds选项可以避免从辅助设备读取客户机估计的数据过于陈旧。

Note注意

Starting in version 4.4, read preference nearest, by default, specifies the use of hedged reads for reads on a sharded cluster.从4.4版开始,默认情况下,nearest读取首选项指定在分片化集群上使用对冲读取

Tip提示
See also: 参阅:

To learn about use cases for specific read preference settings, see Read Preference Use Cases.要了解特定读取首选项设置的用例,请参阅读取首选项用例

Configure Read Preference配置读取首选项

When using a MongoDB driver, you can specify the read preference using the driver's read preference API. 使用MongoDB驱动程序时,可以使用驱动程序的读取首选项API指定读取首选项。See the driver API documentation. You can also set the read preference (except for the hedged read option) when connecting to the replica set or sharded cluster. 请参阅驱动程序API文档。您还可以在连接到副本集或分片集群时设置读取首选项(除了对冲读取选项)。For an example, see connection string.有关示例,请参阅连接字符串

For a given read preference, the MongoDB drivers use the same member selection logic.对于给定的读取偏好,MongoDB驱动程序使用相同的成员选择逻辑

When using mongosh, see cursor.readPref() and Mongo.setReadPref().使用mongosh时,请参阅cursor.readPref()Mongo.setReadPref()

Read Preference and Transactions读取首选项和事务

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

Additional Considerations其他注意事项

For aggregation pipeline operations that include the $out or $merge stages, the pipeline runs on the primary member regardless of read preference setting.对于包含$out$merge阶段的聚合管道操作,无论读取首选项设置如何,管道都在primary成员上运行。

For mapReduce operations, only "inline" mapReduce operations that do not write data support read preference. 对于mapReduce操作,只有不写入数据的“内联”mapReduce运算支持读取首选项。Otherwise, mapReduce operations run on the primary member.否则,mapReduce操作将在primary成员上运行。

[1](1, 2) 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,但对前一个主机的新写入最终会回滚。
←  Write Concern for Replica SetsRead Preference Tag Sets →