Docs HomeMongoDB Manual

Causal Consistency and Read and Write Concerns因果一致性与读写关注

With MongoDB's causally consistent client sessions, different combinations of read and write concerns provide different causal consistency guarantees.通过MongoDB因果一致的客户端会话,不同的读写问题组合提供了不同的因果一致性保证

The following table lists the specific guarantees that various combinations provide:下表列出了各种组合提供的具体保证:

Read Concern读取关注Write Concern撰写关注事项Read own writes读取自己的写入Monotonic reads单调性阅读Monotonic writes单调写作Writes follow reads写入后读取
"majority""majority"
"majority"{ w: 1 }
"local"{ w: 1 }
"local""majority"

If you want causal consistency with data durability, then, as seen from the table, only read operations with "majority" read concern and write operations with "majority" write concern can guarantee all four causal consistency guarantees. 如果您想要具有数据持久性的因果一致性,那么,从表中可以看出,只有具有"majority"读取关注点的读取操作和具有"majority"写入关注点的写入操作才能保证所有四个因果一致性保证。That is, causally consistent client sessions can only guarantee causal consistency for:也就是说,因果一致的客户端会话只能保证以下方面的因果一致性:

  • Read operations with "majority" read concern; in other words, the read operations that return data that has been acknowledged by a majority of the replica set members and is durable.具有"majority"读取问题的读取操作;换句话说,读取操作返回的数据已被大多数副本集成员确认,并且是持久的。
  • Write operations with "majority" write concern; in other words, the write operations that request acknowledgement that the operation has been applied to a majority of the replica set's voting members.具有"majority"写入问题的写入操作;换句话说,请求确认操作已应用于副本集的大多数投票成员的写入操作。

If you want causal consistency without data durability (meaning that writes may be rolled back), then write operations with { w: 1 } write concern can also provide causal consistency.如果您希望在没有数据持久性的情况下实现因果一致性(这意味着写入可能会回滚),那么具有{ w: 1 }写入关注的写入操作也可以提供因果一致性。

Note

The other combinations of read and write concerns may also satisfy all four causal consistency guarantees in some situations, but not necessarily in all situations.读写关注点的其他组合在某些情况下也可能满足所有四种因果一致性保证,但不一定在所有情况下都满足。

The read concern "majority" and write concern "majority" ensure that the four causal consistency guarantees hold even in circumstances (such as with a network partition) where two members in a replica set transiently believe that they are the primary. 读关注点"majority"和写关注点"majority"确保即使在副本集中的两个成员暂时认为他们是主要成员的情况下(例如网络分区),四个因果一致性保证仍然有效。And while both primaries can complete writes with { w: 1 } write concern, only one primary will be able to complete writes with "majority" write concern.虽然两个主程序都可以完成具有{ w: 1 }写入问题的写入,但只有一个主程序能够完成具有"majority"写入问题的写操作。

For example, consider a situation where a network partition divides a five member replica set:例如,考虑一个网络分区划分五个成员副本集的情况:

Network partition: new primary elected on one side but old primary has not stepped down yet.
Example

With the above partition带有上述分区

  • Writes with "majority" write concern can complete on P new but cannot complete on P old.具有"majority"写入问题的写入可以在Pnew上完成,但不能在Pold上完成。
  • Writes with { w: 1 } write concern can complete on either P old or P new. 具有{ w: 1 }写入关注的写入可以在PoldPnew上完成。However, the writes to P old (as well as the writes replicated to S 1) roll back once these members regain communication with the rest of the replica set.但是,一旦这些成员与副本集的其余部分重新获得通信,对Pold的写入(以及复制到S1的写入)就会回滚。
  • After a successful write with "majority" write concern on P new, causally consistent reads with "majority" read concern can observe the write on P new, S 2,and S 3. Pnew上成功写入"majority"写入关注点后,具有"majority"读取关注点的因果一致读取可以观察到PnewS2S3上的写入。The reads can also observe the write on P old and S 1 once they can communicate with the rest of the replica set and sync from the other members of the replica set. 读取还可以观察到PoldS1上的写入,一旦它们可以与副本集的其余部分通信并与副本集中的其他成员同步。Any writes made to P old and/or replicated to S 1 during the partition are rolled back.在分区期间对Pold和/或复制到S1的任何写入都将回滚。

Scenarios情形

To illustrate the read and write concern requirements, the following scenarios have a client issue a sequence of operations with various combination of read and write concerns to the replica set:为了说明读写问题的要求,以下场景让客户端向副本集发出一系列操作,其中包含各种读写问题组合:

Read Concern "majority" and Write concern "majority"读取关注点"majority"并写下关注点"majority"

The use of read concern "majority" and write concern "majority" in a causally consistent session provides the following causal consistency guarantees:

✅ Read own writes ✅ Monotonic reads ✅ Monotonic writes ✅ Writes follow reads

Note

Scenario 1 (Read Concern "majority" and Write Concern "majority")场景1(读取关注点“多数”并写入关注点“大多数”)

During the transient period with two primaries, because only P new can fulfill writes with { w: "majority" } write concern, a client session can issue the following sequence of operations successfully:在有两个主进程的过渡期内,由于只有Pnew可以完成具有{ w: "majority" }写入问题的写入,因此客户端会话可以成功发出以下操作序列:

SequenceExample
1. Write 1 with write concern "majority" to P new
2. Read 1 with read concern "majority" to S 2
3. Write 2 with write concern "majority" to P new


4. Read 2 with read concern "majority" to S 3
For item A, update qty to 50.
Read item A.
For items with qty less than or equal to 50,
update restock to true.
Read item A.
State of data with two primaries using read concern majority and write concern majority
Read own writesRead 1 reads data from S 2 that reflects a state after Write 1.
Read 2 reads data from S 3 that reflects a state after Write 1 followed by Write 2.
Monotonic readsRead 2 reads data from S 3 that reflects a state after Read 1.
Monotonic writesWrite 2 updates data on P new that reflects a state after Write 1.
Writes follow readsWrite 2 updates data on P new that reflects a state of the data after Read 1 (meaning that an earlier state reflects the data read by Read 1).
Note

Scenario 2 (Read Concern "majority" and Write Concern "majority")

Consider an alternative sequence where Read 1 with read concern "majority" routes to S 1:

SequenceExample
1. Write 1 with write concern "majority" to P new
2. Read 1 with read concern "majority" to S 1
3. Write 2 with write concern "majority" to P new


4. Read 2 with with read concern "majority" to S 3
For item A, update qty to 50.
Read item A.
For items with qty less than or equal to 50,
update restock to true.
Read item A.

In this sequence, Read 1 cannot return until the majority commit point has advanced on P old. This cannot occur until P old and S 1 can communicate with the rest of the replica set; at which time, P old has stepped down (if not already), and the two members sync (including Write 1) from the other members of the replica set.

Read own writesRead 1 reflects a state of data after Write1 1, albeit after the network partition has healed and the member has sync'ed from the other members of the replica set.
Read 2 reads data from S 3 that reflects a state after Write1 1 followed by Write 2.
Monotonic readsRead 2 reads data from S 3 that reflects a state after Read 1 (meaning that an earlier state is reflected in the data read by Read 1).
Monotonic writesWrite 2 updates data on P new that reflects a state after Write 1.
Writes follow readsWrite 2 updates data on P new that reflects a state of the data after Read 1 (meaning that an earlier state reflects the data read by Read 1).

Read Concern "majority" and Write concern {w: 1}

The use of read concern "majority" and write concern { w: 1 } in a causally consistent session provides the following causal consistency guarantees if you want causal consistency with data durability:

❌ Read own writes ✅ Monotonic reads ❌ Monotonic writes ✅ Writes follow reads

If you want causal consistency without data durability:

✅ Read own writes ✅ Monotonic reads ✅ Monotonic writes ✅ Writes follow reads

Note

Scenario 3 (Read Concern "majority" and Write Concern {w: 1})

During the transient period with two primaries, because both P old and P new can fulfill writes with { w: 1 } write concern, a client session could issue the following sequence of operations successfully but not be causally consistent if you want causal consistency with data durability:

SequenceExample
1. Write 1 with write concern { w: 1 } to P old
2. Read 1 with read concern "majority" to S 2
3. Write 2 with write concern { w: 1 } to P new


4. Read 2 with with read concern "majority" to S 3
For item A, update qty to 50.
Read item A.
For items with qty less than or equal to 50,
update restock to true.
Read item A.
State of data with two primaries using read concern majority and write concern 1

In this sequence,

  • Read 1 cannot return until the majority commit point has advanced on P new past the time of Write 1.
  • Read 2 cannot return until the majority commit point has advanced on P new past the time of Write 2.
  • Write 1 will roll back when the network partition is healed.

If you want causal consistency with data durability

Read own writesRead 1 reads data from S 2 that doesn't reflect a state after Write 1.
Monotonic readsRead 2 reads data from S 3 that reflects a state after Read 1 (meaning that an earlier state is reflected in the data read by Read 1).
Monotonic writesWrite 2 updates data on P new that does not reflect a state after Write 1.
Writes follow readsWrite 2 updates data on P new that reflects a state after Read 1 (meaning that an earlier state reflects the data read by Read 1).

If you want causal consistency without data durability

Read own writesRead 1 reads data from S 2 returns data that reflects a state equivalent to Write 1 followed by rollback of Write 1.
Monotonic readsRead 2 reads data from S 3 that reflects a state after Read 1 (meaning that an earlier state is reflected in the data read by Read 1).
Monotonic writesWrite 2 updates data on P new that is equivalent to after Write 1 followed by rollback of Write 1.
Writes follow readsWrite 2 updates data on P new that reflects a state after Read 1 (meaning whose earlier state reflects the data read by Read 1).
Note

Scenario 4 (Read Concern "majority" and Write Concern {w: 1})

Consider an alternative sequence where Read 1 with read concern "majority" routes to S 1:

SequenceExample
1. Write 1 with write concern { w: 1 } to P old
2. Read 1 with read concern "majority" to S 1
3. Write 2 with write concern { w: 1 } to P new


4. Read 2 with with read concern "majority" to S 3
For item A, update qty to 50.
Read item A.
For items with qty less than or equal to 50,
update restock to true.
Read item A.

In this sequence:

  • Read 1 cannot return until the majority commit point has advanced on S 1. This cannot occur until P old and S 1 can communicate with the rest of the replica set. At which time, P old has stepped down (if not already), Write 1 is rolled back from P old and S 1, and the two members sync from the other members of the replica set.

If you want causal consistency with data durability

Read own writesThe data read by Read 1 doesn't reflect the results of Write 1, which has rolled back.
Monotonic readsRead 2 reads data from S 3 that reflects a state after Read 1 (meaning whose earlier state reflects the data read by Read 1).
Monotonic writesWrite 2 updates data on P new that does not reflect a state after Write 1, which had preceded Write 2 but has rolled back.
Writes follow readsWrite 2 updates data on P new that reflects a state after Read 1 (meaning whose earlier state reflects the data read by Read 1).

If you want causal consistency without data durability

Read own writesRead 1 returns data that reflects the final result of Write 1 since Write 1 ultimately rolls back.
Monotonic readsRead 2 reads data from S 3 that reflects a state after Read 1 (meaning that an earlier state reflects the data read by Read 1).
Monotonic writesWrite 2 updates data on P new that is equivalent to after Write 1 followed by rollback of Write 1.
Writes follow readsWrite 2 updates data on P new that reflects a state after Read 1 (meaning that an earlier state reflects the data read by Read 1).

Read Concern "local" and Write concern {w: 1}

The use of read concern "local" and write concern { w: 1 } in a causally consistent session cannot guarantee causal consistency.

❌ Read own writes ❌ Monotonic reads ❌ Monotonic writes ❌ Writes follow reads

This combination may satisfy all four causal consistency guarantees in some situations, but not necessarily in all situations.

Note

Scenario 5 (Read Concern "local" and Write Concern {w: 1})

During this transient period, because both P old and P new can fulfill writes with { w: 1 } write concern, a client session could issue the following sequence of operations successfully but not be causally consistent:

SequenceExample
1. Write 1 with write concern { w: 1 } to P old
2. Read 1 with read concern "local" to S 1
3. Write 2 with write concern { w: 1 } to P new


4. Read 2 with read concern "local" to S 3
For item A, update qty to 50.
Read item A.
For items with qty less than or equal to 50,
update restock to true.
Read item A.
State of data with two primaries using read concern local and write concern 1
❌ Read own writesRead 2 reads data from S 3 that only reflects a state after Write 2 and not Write 1 followed by Write 2.
❌ Monotonic readsRead 2 reads data from S 3 that doesn't reflect a state after Read 1 (meaning that an earlier state doesn't reflect the data read by Read 1).
❌ Monotonic writesWrite 2 updates data on P new that does not reflect a state after Write 1.
❌ Write follow readWrite 2 updates data on P new that does not reflect a state after Read 1 (meaning that an earlier state doesn't reflect the data read by Read 1).

Read Concern "local" and Write concern "majority"

The use of read concern "local" and write concern "majority" in a causally consistent session provides the following causal consistency guarantees:

❌ Read own writes ❌ Monotonic reads ✅ Monotonic writes ❌ Writes follow reads

This combination may satisfy all four causal consistency guarantees in some situations, but not necessarily in all situations.

Note

Scenario 6 (Read Concern "local" and Write Concern "majority")

During this transient period, because only P new can fulfill writes with { w: "majority" } write concern, a client session could issue the following sequence of operations successfully but not be causally consistent:

SequenceExample
1. Write 1 with write concern "majority" to P new
2. Read 1 with read concern "local" to S 1
3. Write 2 with write concern "majority" to P new


4. Read 2 with read concern "local" to S 3
For item A, update qty to 50.
Read item A.
For items with qty less than or equal to 50,
update restock to true.
Read item A.
State of data with two primaries using read concern local and write concern majority
❌ Read own writes.Read 1 reads data from S 1 that doesn't reflect a state after Write1 1.
❌ Monotonic reads.Read 2 reads data from S 3 that doesn't reflect a state after Read 1 (meaning that an earlier state doesn't reflect the data read by Read 1).
✅ Monotonic writesWrite 2 updates data on P new that reflects a state after Write 1.
❌ Write follow read.Write 2 updates data on P new that does not reflect a state after Read 1 (meaning that an earlier state doesn't reflect the data read by Read 1).