Docs HomeMongoDB Manual

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. 读取首选项maxStalenessSeconds选项允许您为从secondary中读取指定最大复制滞后或“过时”。When a secondary's estimated staleness exceeds maxStalenessSeconds, the client stops using it for read operations.当辅助服务器的估计过期时间超过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引入了一种流控制机制来控制主应用写入的速率,目的是将大多数提交延迟保持在指定的最大值以下。

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. 您必须指定90秒或更长的maxStalenessSeconds值:指定较小的maxStarenessSeconds将引发错误。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值。