Retryable reads allow MongoDB drivers to automatically retry certain read operations a single time if they encounter certain network or server errors.可重试读取允许MongoDB驱动程序在遇到某些网络或服务器错误时自动重试某些读取操作。
Prerequisites先决条件
Minimum Driver Version最低驱动程序版本Official MongoDB drivers compatible with MongoDB Server 6.0 and later support retryable reads.与MongoDB Server 6.0及更高版本兼容的官方MongoDB驱动程序支持可重试读取。For more information on official MongoDB drivers, see MongoDB Drivers.有关官方MongoDB驱动程序的更多信息,请参阅MongoDB驱动程序。Minimum Server Version最低服务器版本Drivers can only retry read operations if connected to MongoDB Server 6.0 or later.驱动程序只能在连接到MongoDB Server 6.0或更高版本时重试读取操作。
Enabling Retryable Reads启用可重试读取
Official MongoDB drivers compatible with MongoDB Server 6.0 and later enable retryable reads by default. To explicitly disable retryable reads, specify 与MongoDB Server 6.0及更高版本兼容的官方MongoDB驱动程序默认启用可重试读取。要显式禁用可重试读取,请在部署的连接字符串中指定retryReads=false in the connection string for the deployment.retryReads=false。
mongosh does not support retryable reads.不支持可重试的读取。
Retryable Read Operations可重试的读取操作
MongoDB drivers support retrying the following read operations. The list references a generic description of each method. For specific syntax and usage, defer to the driver documentation for that method.MongoDB驱动程序支持重试以下读取操作。该列表引用了每种方法的通用描述。有关具体的语法和用法,请参阅该方法的驱动程序文档。
Collection.aggregateCollection.countCollection.countDocumentsCollection.distinctCollection.estimatedDocumentCountCollection.findDatabase.aggregate
| |
Collection.watchDatabase.watchMongoClient.watch | |
MongoClient.listDatabasesDatabase.listCollectionsCollection.listIndexes | |
Collection.find Collection.find支持的GridFS操作(e.g. GridFSBucket.openDownloadStream) |
MongoDB drivers may include retryable support for other operations, such as helper methods or methods that wrap a retryable read operation. Defer to the driver documentation to determine whether a method explicitly supports retryable reads.MongoDB驱动程序可能包括对其他操作的可重试支持,例如辅助方法或包装可重试读取操作的方法。请参阅驱动程序文档,以确定方法是否明确支持可重试的读取。
Tip
Retryable Read Specification: 可重试读取规范:Supported Read Operations支持的读取操作
Unsupported Read Operations不支持的读取操作
The following operations do not support retryable reads:以下操作不支持可重试的读取:
db.collection.mapReduce()getMoreAny read command passed to a generic传递给通用Database.runCommandhelper, which is agnostic about read or write commands.Database.runCommand帮助程序的任何读取命令,该帮助程序与读取或写入命令无关。
Behavior行为
Persistent Network Errors持续的网络错误
MongoDB retryable reads make only one retry attempt. This helps address transient network errors or replica set elections, but not persistent network errors.MongoDB可重试读取只进行一次重试尝试。这有助于解决暂时的网络错误或副本集选举,但不能解决持久的网络错误。
Failover Period故障转移期
The driver performs server selection using the read command's original read preference before retrying the read operation. If the driver cannot select a server for the retry attempt using the original read preference, the driver returns the original error.在重试读取操作之前,驱动程序使用读取命令的原始读取首选项执行服务器选择。如果驱动程序无法使用原始读取首选项为重试尝试选择服务器,则驱动程序将返回原始错误。
The drivers wait 驱动程序在执行服务器选择之前等待serverSelectionTimeoutMS milliseconds before performing server selection. Retryable reads do not address instances where no eligible servers exist after waiting serverSelectionTimeoutMS.serverSelectionTimeoutMS毫秒。可重试的读取不会解决在等待serverSelectionTimeoutMS后不存在符合条件的服务器的情况。