读关注 "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. The documents read are durable and
guaranteed to not roll back.
For operations in multi-document transactions, read concern "majority"
provides its
guarantees only if the transaction commits with write concern
"majority". Otherwise, the
"majority"
read concern provides no guarantees about the
data read in transactions.
无论读关注级别如何,节点上的最新数据可能无法反映系统中数据的最新版本。
For more information about what happens if a primary fails, see Automatic Failover.
性能
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 to other read
concerns.
可用性
读关注 (read concern) "majority"
is available for use with or
without causally consistent sessions and transactions.
警告
如使用主节点-从节点-仲裁节点 (PSA) 三成员架构,请考虑以下因素:
如果从节点不可用或滞后,写关注
"majority"
可能会导致性能问题。有关如何缓解这些问题的建议,请参阅缓解自管理 PSA 副本集的性能问题。If you are using a global default
"majority"
and the write concern is less than the size of the majority, your queries may return stale (not fully replicated) data.
例子
考虑以下对三节点副本集执行 Write 0 写入操作的时间轴:
注意
为简单起见,该示例预设以下条件:
Write 0 之前的所有写入均已成功复制到所有成员。
Write prev 是 Write 0 前一次的写入操作。
Write 0 之后没有发生其他写入操作。
时间 | 事件 | 最新写入 | 最新 w: "majority" 写入 |
---|---|---|---|
t 0 | 主节点应用 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 1 | 从节点 1 应用 write 0 | Primary: Write 0 Secondary 1: Write 0 Secondary 2: Write prev | Primary: Write prev Secondary 1: Write prev Secondary 2: Write prev |
t 2 | 从节点 2 应用 write 0 | Primary: Write 0 Secondary 1: Write 0 Secondary 2: Write 0 | Primary: Write prev Secondary 1: Write prev Secondary 2: Write prev |
t 3 | 主节点知道已成功复制到从节点 1,并向客户端发送确认 | Primary: Write 0 Secondary 1: Write 0 Secondary 2: Write 0 | Primary: Write 0 Secondary 1: Write prev Secondary 2: Write prev |
t 4 | 主节点感知到数据已成功复制到从节点 2 | Primary: Write 0 Secondary 1: Write 0 Secondary 2: Write 0 | Primary: Write 0 Secondary 1: Write prev Secondary 2: Write prev |
t 5 | 从节点 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 6 | 从节点 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 |
然后,下表汇总了具有 "majority"
读关注的读取操作在时间 T
时看到的数据状态。
读取目标 | 时间 T | 数据状态 |
---|---|---|
主节点 | t 之前 3 | 数据反映为 Write prev 应用后的状态 |
主节点 | t 之后 3 | 数据反映为 Write 应用后的状态 0 |
从节点 1 | t 之前 5 | 数据反映为 Write prev 应用后的状态 |
从节点 1 | t 之后 5 | 数据反映为 Write 应用后的状态 0 |
从节点 2 | Before or at t 6 | 数据反映为 Write prev 应用后的状态 |
从节点 2 | t 之后 6 | 数据反映为 Write 应用后的状态 0 |
Storage Engine Support
读关注 (read concern) "majority"
is available for the
WiredTiger storage engine.
提示
The serverStatus
command returns the
storageEngine.supportsCommittedReads
field, which
indicates whether the storage engine supports "majority"
read
concern.
读关注 (read concern) "majority"
and Transactions
注意
You set the read concern at the transaction level, not at the individual operation level. To set the read concern for transactions, see 事务和读关注.
For operations in multi-document transactions, read concern "majority"
provides its
guarantees only if the transaction commits with write concern
"majority". Otherwise, the
"majority"
read concern provides no guarantees about the
data read in transactions.
读关注 (read concern) "majority"
and Aggregation
You can specify 读关注 (read concern) level
"majority"
for an aggregation that includes an $out
stage.
读取自己的写入
如果写入请求确认,您可以使用因果一致的会话来读取您自己的写入。
主从仲裁副本集
从 MongoDB 5.0 开始,由于改进了存储引擎,enableMajorityReadConcern
和 --enableMajorityReadConcern
不可更改,并始终设置为 true
。
在 MongoDB 的早期版本中,enableMajorityReadConcern
和 --enableMajorityReadConcern
均可配置,可以设置为 false
,以防止由三个节点组成的主-从-仲裁 (PSA) 架构的部署因存储缓存压力而停止运行。
如使用主节点-从节点-仲裁节点 (PSA) 三成员架构,请考虑以下因素:
如果从节点不可用或滞后,写关注
"majority"
可能会导致性能问题。有关如何缓解这些问题的建议,请参阅缓解自管理 PSA 副本集的性能问题。If you are using a global default
"majority"
and the write concern is less than the size of the majority, your queries may return stale (not fully replicated) data.