Read Concern 读取关注"majority"

On this page本页内容

"majority"

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.无论读取关注级别如何,节点上的最新数据可能不会反映系统中数据的最新版本。

Performance性能

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",节点从该视图返回数据,并且在性能成本上与其他读关注点相当。

Availability可利用性

Read concern "majority" is available for use with or without causally consistent sessions and transactions.读关注"majority"可用于有或无因果一致的会话和事务。

Warning警告

If you are using a three-member primary-secondary-arbiter (PSA) architecture, the write concern "majority" can cause performance issues if a secondary is unavailable or lagging. 如果您使用的是三成员主辅仲裁器(PSA)体系结构,那么如果辅仲裁器不可用或滞后,写问题"majority"可能会导致性能问题。See Mitigate Performance Issues with PSA Replica Set for advice on how to mitigate these issues.有关如何缓解这些问题的建议,请参阅缓解PSA副本集的性能问题

Example示例

Consider the following timeline of a write operation Write 0 to a three member replica set:考虑以下写入操作的时间线:将0写入三成员复制集:

Note注意

For simplification, the example assumes:为了简化,该示例假设:

  • All writes prior to Write 0 have been successfully replicated to all members.写入0之前的所有写入都已成功复制到所有成员。
  • Write prev is the previous write before Write 0.写入prev是写入0之前的上一次写入。
  • No other writes have occured after Write 0.写入0后未发生其他写入。
Timeline of a write operation to a three member replica set.
>
TimeEventMost Recent Write最近写Most Recent w: "majority" write最近w: "majority"写
t 0Primary applies Write 主要应用于写入0
Primary: Write 0
Secondary 1: Write prev
Secondary 2: Write prev
Primary: Write prev
Secondary 1: Write prev
Secondary 2: Write prev
t 1Secondary 1 applies write 0辅助1应用于写入0
Primary: Write 0
Secondary 1: Write 0
Secondary 2: Write prev
Primary: Write prev
Secondary 1: Write prev
Secondary 2: Write prev
t 2Secondary 2 applies write 0次要2应用于写入0
Primary: Write 0
Secondary 1: Write 0
Secondary 2: Write 0
Primary: Write prev
Secondary 1: Write prev
Secondary 2: Write prev
t 3Primary is aware of successful replication to Secondary 1 and sends acknowledgement to client主服务器知道到辅助服务器1的复制成功,并向客户端发送确认
Primary: Write 0
Secondary 1: Write 0
Secondary 2: Write 0
Primary: Write 0
Secondary 1: Write prev
Secondary 2: Write prev
t 4Primary is aware of successful replication to Secondary 2主服务器知道已成功复制到辅助服务器2
Primary: Write 0
Secondary 1: Write 0
Secondary 2: Write 0
Primary: Write 0
Secondary 1: Write prev
Secondary 2: Write prev
t 5Secondary 1 receives notice (through regular replication mechanism) to update its snapshot of its most recent w: "majority" write次要1收到通知(通过常规复制机制)更新其最新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 6Secondary 2 receives notice (through regular replication mechanism) to update its snapshot of its most recent w: "majority" write次要2收到通知(通过常规复制机制)更新其最新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将看到的数据状态。

Timeline of a write operation to a three member replica set.
Read TargetTime TState of Data数据状态
PrimaryBefore t 3Data reflects Write 数据反映了写入prev
PrimaryAfter t 3Data reflects Write 数据反映了写入0
Secondary 1Before t 5Data reflects Write 数据反映了写入prev
Secondary 1After t 5Data reflects Write 数据反映了写入0
Secondary 2Before or at t 6Data reflects Write 数据反映了写入prev
Secondary 2After t 6Data reflects Write 数据反映了写入0

Storage Engine Support存储引擎支持

Read concern "majority" is available for the WiredTiger storage engine.WiredTiger存储引擎具有读取问题"majority"

Tip提示

The serverStatus command returns the storageEngine.supportsCommittedReads field which indicates whether the storage engine supports "majority" read concern.serverStatus命令返回storageEngine.supportsCommittedReads字段,该字段指示存储引擎是否支持"majority"读关注。

Read Concern "majority" and Transactions读取关注"majority"和事务

Note注意

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"读取关注点不能保证事务中读取的数据。

Read Concern "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 $out stage to use "majority" read concern for the aggregation.在MongoDB 4.0及更早版本中,不能包含$out阶段来使用聚合的"majority"读取关注点。

Read Your Own Writes读取自己的写入

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 { w: "majority" } write concern, and then issue your read operation with primary read preference, and either "majority" or "linearizable" read concern.在MongoDB 3.6之前,为了读取您自己的写入操作,您必须使用{ w: "majority" }写入关注点发出写入操作,然后使用primary读取首选项发出读取操作,以及"majority""linearizable"读取关注点。

Primary-Secondary-Arbiter Replica Sets主仲裁器和次仲裁器副本集

Starting in MongoDB 5.0, enableMajorityReadConcern and --enableMajorityReadConcern cannot be changed and are always set to true due to storage engine improvements.从MongoDB 5.0开始,由于存储引擎的改进,enableMajorityReadConcern--enableMajorityReadConcern无法更改,并且始终设置为true

In earlier versions of 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.在早期版本的MongoDB中,enableMajorityReadConcern--enableMajorityReadConcern是可配置的,可以设置为false,以防止存储缓存压力阻止使用三成员主从仲裁器(PSA)体系结构的部署。

If you are using a three-member primary-secondary-arbiter (PSA) architecture, the write concern "majority" can cause performance issues if a secondary is unavailable or lagging. 如果您使用的是三成员主辅仲裁器(PSA)体系结构,那么如果辅仲裁器不可用或滞后,写问题"majority"可能会导致性能问题。See Mitigate Performance Issues with PSA Replica Set for advice on how to mitigate these issues.有关如何缓解这些问题的建议,请参阅缓解PSA副本集的性能问题

←  Read Concern "available"Read Concern "linearizable" →