Read Preference 读取首选项maxStalenessSeconds

Replica set members can lag behind the primary due to network congestion, low disk throughput, long-running operations, etc. 由于网络拥塞、磁盘吞吐量低、长时间运行等原因,副本集成员可能落后于primaryThe read preference maxStalenessSeconds option lets you specify a maximum replication lag, or "staleness", for reads from secondaries. When a secondary's estimated staleness exceeds maxStalenessSeconds, the client stops using it for read operations.读取首选项maxStalenessSeconds选项允许您为从辅助设备读取指定最大复制延迟或“过时”。当辅助服务器的估计过时时间超过maxStalenessSeconds时,客户端将停止使用它进行读取操作。

Important重要

The maxStalenessSeconds read preference option is intended for applications that read from secondaries and want to avoid reading from a secondary that has fallen overly far behind in replicating the primary's writes. maxStalenessSeconds读取首选项选项适用于从辅助设备读取数据并希望避免从复制主设备写入数据时落后太多的辅助设备读取的应用程序。For example, a secondary might stop replicating due to a network outage between itself and the primary. 例如,辅助服务器可能会由于其与主服务器之间的网络中断而停止复制。In that case, the client should stop reading from the secondary until an administrator resolves the outage and the secondary catches up.在这种情况下,客户端应该停止从辅助服务器读取数据,直到管理员解决了中断问题,辅助服务器恢复正常。

To use maxStalenessSeconds, all of the MongoDB instances in your deployment must be using MongoDB 3.4 or later. 要使用maxStalenessSeconds,部署中的所有MongoDB实例必须使用MongoDB 3.4或更高版本。If any instances are on an earlier version of MongoDB, the driver or mongos will raise an error.如果任何实例在MongoDB的早期版本上,驱动程序或mongos将引发错误。

Note注意

Starting in version 4.2, MongoDB introduces a flow control mechanism to control the rate at which the primary applies its writes with the goal of keeping majority committed lag under a specified maximum value.从4.2版开始,MongoDB引入了一种流控制机制,以控制主应用其写入的速率,目标是将majority committed延迟保持在指定的最大值之下。

You can specify maxStalenessSeconds with the following read preference modes:可以使用以下读取首选项模式指定maxStalenessSeconds

Max staleness is not compatible with mode primary and only applies when selecting a secondary member of a set for a read operation.最大过时性与模式primary不兼容,仅在为读取操作选择集合的secondary成员时适用。

When selecting a server for a read operation with maxStalenessSeconds, clients estimate how stale each secondary is by comparing the secondary's last write to that of the primary. 当为maxStalenessSeconds的读取操作选择服务器时,客户端通过比较辅助服务器的上一次写入与主服务器的写入来估计每个辅助服务器的过时程度。The client will then direct 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.如果没有主文件,则客户端使用具有最新写入的次文件进行比较。

By default, there is no maximum staleness and clients will not consider a secondary's lag when choosing where to direct a read operation.默认情况下,没有最大的过时性,并且客户端在选择将读取操作定向到何处时不会考虑辅助设备的延迟。

You must specify a maxStalenessSeconds value of 90 seconds or longer: specifying a smaller maxStalenessSeconds value will raise an error. 必须将maxStalenessSeconds值指定为90秒或更长:指定较小的maxStalenessSeconds值将引发错误。Clients estimate secondaries' staleness by periodically checking the latest write date of each replica set member. 客户端通过定期检查每个副本集成员的最新写入日期来评估辅助副本的过时性。Since these checks are infrequent, the staleness estimate is coarse. 由于这些检查并不频繁,所以对老化的估计是粗略的。Thus, clients cannot enforce a maxStalenessSeconds value of less than 90 seconds.因此,客户端无法强制执行小于90秒的maxStalenessSeconds值。

←  Read Preference Tag SetsHedged Read Option →