"majority"
On this page本页内容
For read operations not associated with multi-document transactions, read concern 对于与多文档事务无关的读取操作,读取关注点"majority"
guarantees that the data read has been acknowledged by a majority of the replica set members (i.e. the documents read are durable and guaranteed not to roll back)."majority"
保证读取的数据已被大多数副本集成员确认(即,读取的文档是持久的,并保证不会回滚)。
For operations in multi-document transactions, read concern 对于多文档事务中的操作,读关注点“多数”仅在事务以写关注点"majority"
provides its guarantees only if the transaction commits with write concern "majority". "majority"
提交时提供其保证。Otherwise, the 否则,"majority"
read concern provides no guarantees about the data read in transactions."majority"
读取关注点不能保证事务中读取的数据。
Regardless of the read concern level, the most recent data on a node may not reflect the most recent version of the data in the system.无论读取关注级别如何,节点上的最新数据可能不会反映系统中数据的最新版本。
Each replica set member maintains, in memory, a view of the data at the majority-commit point; the majority-commit point is calculated by the primary. 每个副本集成员在内存中维护多数提交点的数据视图;多数提交点由主提交点计算。To fulfill read concern 为了满足读关注点"majority"
, the node returns data from this view and is comparable in performance cost to other read concerns."majority"
,节点从该视图返回数据,并且在性能成本上与其他读关注点相当。
Read concern 读关注"majority"
is available for use with or without causally consistent sessions and transactions."majority"
可用于有或无因果一致的会话和事务。
If you are using a three-member primary-secondary-arbiter (PSA) architecture, the write concern 如果您使用的是三成员主辅仲裁器(PSA)体系结构,那么如果辅仲裁器不可用或滞后,写问题"majority"
can cause performance issues if a secondary is unavailable or lagging. "majority"
可能会导致性能问题。See Mitigate Performance Issues with PSA Replica Set for advice on how to mitigate these issues.有关如何缓解这些问题的建议,请参阅缓解PSA副本集的性能问题。
Consider the following timeline of a write operation Write 0 to a three member replica set:考虑以下写入操作的时间线:将0写入三成员复制集:
For simplification, the example assumes:为了简化,该示例假设:
Time | Event | ||
---|---|---|---|
t 0 | Primary: Write 0
Secondary 1: Write prev
Secondary 2: Write prev
| Primary: Write prev
Secondary 1: Write prev
Secondary 2: Write prev
| |
t 1 | Primary: Write 0
Secondary 1: Write 0
Secondary 2: Write prev
| Primary: Write prev
Secondary 1: Write prev
Secondary 2: Write prev
| |
t 2 | Primary: Write 0
Secondary 1: Write 0
Secondary 2: Write 0
| Primary: Write prev
Secondary 1: Write prev
Secondary 2: Write prev
| |
t 3 | Primary: Write 0
Secondary 1: Write 0
Secondary 2: Write 0
| Primary: Write 0
Secondary 1: Write prev
Secondary 2: Write prev
| |
t 4 | Primary: Write 0
Secondary 1: Write 0
Secondary 2: Write 0
| Primary: Write 0
Secondary 1: Write prev
Secondary 2: Write prev
| |
t 5 | w: "majority" 写入的快照 | Primary: Write 0
Secondary 1: Write 0
Secondary 2: Write 0
| Primary: Write 0
Secondary 1: Write 0
Secondary 2: Write prev
|
t 6 | w: "majority" 写入的快照 | Primary: Write 0
Secondary 1: Write 0
Secondary 2: Write 0
| Primary: Write 0
Secondary 1: Write 0
Secondary 2: Write 0
|
Then, the following tables summarizes the state of the data that a read operation with 然后,下表总结了具有"majority"
read concern would see at time T
."majority"
读取关注点的读取操作在时间T
将看到的数据状态。
Read Target | Time T | |
---|---|---|
Primary | Before t 3 | |
Primary | After t 3 | |
Secondary 1 | Before t 5 | |
Secondary 1 | After t 5 | |
Secondary 2 | Before or at t 6 | |
Secondary 2 | After t 6 |
Read concern WiredTiger存储引擎具有读取问题"majority"
is available for the WiredTiger storage engine."majority"
。
The serverStatus
command returns the storageEngine.supportsCommittedReads
field which indicates whether the storage engine supports "majority"
read concern.serverStatus
命令返回storageEngine.supportsCommittedReads
字段,该字段指示存储引擎是否支持"majority"
读关注。
"majority"
and Transactions"majority"
和事务You set the read concern at the transaction level, not at the individual operation level. 您可以在事务级别而不是单个操作级别设置读取关注点。To set the read concern for transactions, see Transactions and Read Concern.要设置事务的读取关注点,请参阅事务和读取关注点。
For operations in multi-document transactions, read concern 对于多文档事务中的操作,读关注点"majority"
provides its guarantees only if the transaction commits with write concern "majority". "majority"
仅在事务以写关注点"majority"
提交时提供其保证。Otherwise, the 否则,"majority"
read concern provides no guarantees about the data read in transactions."majority"
读取关注点不能保证事务中读取的数据。
"majority"
and Aggregation"majority"
和聚合Starting in MongoDB 4.2, you can specify read concern level "majority"
for an aggregation that includes an $out
stage.
In MongoDB 4.0 and earlier, you cannot include the 在MongoDB 4.0及更早版本中,不能包含$out
stage to use "majority"
read concern for the aggregation.$out
阶段来使用聚合的"majority"
读取关注点。
Changed in version 3.6.在版本3.6中更改。
Starting in MongoDB 3.6, you can use causally consistent sessions to read your own writes, if the writes request acknowledgement.从MongoDB 3.6开始,如果写入请求确认,您可以使用因果一致的会话来读取自己的写入。
Prior to MongoDB 3.6, in order to read your own writes you must issue your write operation with 在MongoDB 3.6之前,为了读取您自己的写入操作,您必须使用{ w: "majority" }
write concern, and then issue your read operation with primary
read preference, and either "majority"
or "linearizable"
read concern.{ w: "majority" }
写入关注点发出写入操作,然后使用primary
读取首选项发出读取操作,以及"majority"
或"linearizable"
读取关注点。
Starting in MongoDB 5.0, 从MongoDB 5.0开始,由于存储引擎的改进,enableMajorityReadConcern
and --enableMajorityReadConcern
cannot be changed and are always set to true
due to storage engine improvements.enableMajorityReadConcern
和--enableMajorityReadConcern
无法更改,并且始终设置为true
。
In earlier versions of MongoDB, 在早期版本的MongoDB中,enableMajorityReadConcern
and --enableMajorityReadConcern
are configurable and can be set to false
to prevent storage cache pressure from immobilizing a deployment with a three-member primary-secondary-arbiter (PSA) architecture.enableMajorityReadConcern
和--enableMajorityReadConcern
是可配置的,可以设置为false
,以防止存储缓存压力阻止使用三成员主从仲裁器(PSA)体系结构的部署。
If you are using a three-member primary-secondary-arbiter (PSA) architecture, the write concern 如果您使用的是三成员主辅仲裁器(PSA)体系结构,那么如果辅仲裁器不可用或滞后,写问题"majority"
can cause performance issues if a secondary is unavailable or lagging. "majority"
可能会导致性能问题。See Mitigate Performance Issues with PSA Replica Set for advice on how to mitigate these issues.有关如何缓解这些问题的建议,请参阅缓解PSA副本集的性能问题。