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:下表列出了各种组合提供的具体保证:
"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 acknowledgment 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:例如,考虑一个网络分区划分五个成员副本集的情况:
Example示例
With the above partition有了上面的隔断
Writes with具有"majority"write concern can complete onPnew but cannot complete onPold."majority"写入关注的写入可以在Pnew上完成,但不能在Pold上完成。Writes with具有{ w: 1 }write concern can complete on eitherPold orPnew.{ w: 1 }写入关注的写入可以在Pold或Pnew上完成。However, the writes to然而,一旦这些成员重新与副本集的其余部分恢复通信,对Pold(as well as the writes replicated toS1) roll back once these members regain communication with the rest of the replica set.Pold的写入(以及复制到S1的写入)就会回滚。After a successful write with在"majority"write concern onPnew, causally consistent reads with"majority"read concern can observe the write onPnew,S2,andS3.Pnew上成功写入"majority"写入关注后,具有"majority"读取关注的因果一致读取可以观察到Pnew、S2和S3上的写入。The reads can also observe the write on一旦读取可以与副本集的其余部分通信并从副本集的其他成员同步,读取还可以观察到Pold andS1 once they can communicate with the rest of the replica set and sync from the other members of the replica set.Pold和S1上的写入。Any writes made to在分区期间对Pold and/or replicated toS1 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"Read Concern读取关注"majority"and Write concern{w: 1}"majority"和写入关注{w: 1}Read Concern读取关注"local"and Write concern"majority""local"和写入关注"majority"Read Concern读取关注"local"and Write concern{w: 1}"local"和写入关注{w: 1}
Read Concern "majority" and Write concern "majority"读取关注"majority"和写关注"majority"
"majority" and Write concern "majority"The use of read concern 在因果一致性会话中使用读关注"majority" and write concern "majority" in a causally consistent session provides the following causal consistency guarantees:"majority"和写关注"majority"提供了以下因果一致性保证:
✅ Read own writes读取自己的写入 ✅ Monotonic reads单调读取 ✅ Monotonic writes单调写入 ✅ Writes follow reads按读写
Note
Scenario 1 (Read Concern "majority" and Write Concern "majority")场景1(读取关注"majority"和写入关注"majority")
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" }写关注的写操作,因此客户端会话可以成功发出以下操作序列:
1. Write 1 with write concern "majority" to P new2. Read 1 with read concern "majority" to S 23. Write 2 with write concern "majority" to P new4. 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. |
| ✅ | Read 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 reads | Read 2 reads data from S 3 that reflects a state after Read 1. |
| ✅ Monotonic writes | Write 2 updates data on P new that reflects a state after Write 1. |
| ✅ Writes follow reads | Write 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:
| Sequence | |
|---|---|
1. Write 1 with write concern "majority" to P new2. Read 1 with read concern "majority" to S 13. Write 2 with write concern "majority" to P new4. 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 writes | Read 1 reflects a state of data after Write 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 Write 1 followed by Write 2. |
| ✅ Monotonic reads | Read 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 writes | Write 2 updates data on P new that reflects a state after Write 1. |
| ✅ Writes follow reads | Write 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:
| Sequence | |
|---|---|
1. Write 1 with write concern { w: 1 } to P old2. Read 1 with read concern "majority" to S 23. Write 2 with write concern { w: 1 } to P new4. 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
Pnew past the time of Write 1. - Read 2 cannot return until the majority commit point has advanced on
Pnew 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 writes | Read 1 reads data from S 2 that doesn't reflect a state after Write 1. |
| ✅ Monotonic reads | Read 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 writes | Write 2 updates data on P new that does not reflect a state after Write 1. |
| ✅ Writes follow reads | Write 2 updates data on |
➤ If you want causal consistency without data durability如果你想在没有数据持久性的情况下保持因果一致性
| ✅ Read own writes | Read 1 reads data from |
| ✅ Monotonic reads | Read 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 writes | Write 2 updates data on |
| ✅ Writes follow reads | Write 2 updates data on |
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:
| Sequence | |
|---|---|
1. Write 1 with write concern { w: 1 } to P old2. Read 1 with read concern "majority" to S 13. Write 2 with write concern { w: 1 } to P new4. 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
S1. This cannot occur untilPold andS1 can communicate with the rest of the replica set. At which time,Pold has stepped down (if not already), Write 1 is rolled back fromPold andS1, and the two members sync from the other members of the replica set.
➤ If you want causal consistency with data durability
| ❌ Read own writes | The data read by Read 1 doesn't reflect the results of Write 1, which has rolled back. |
| ✅ Monotonic reads | Read 2 reads data from |
| ❌ Monotonic writes | Write 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 reads | Write 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 writes | Read 1 returns data that reflects the final result of Write 1 since Write 1 ultimately rolls back. |
| ✅ Monotonic reads | Read 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 writes | Write 2 updates data on |
| ✅ Writes follow reads | Write 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:
| Sequence | |
|---|---|
1. Write 1 with write concern { w: 1 } to P old2. Read 1 with read concern "local" to S 13. Write 2 with write concern { w: 1 } to P new4. 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. |
| ❌ Read own writes | Read 2 reads data from |
| ❌ 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 writes | Write 2 updates data on P new that does not reflect a state after Write 1. |
| ❌ Write follow read | Write 2 updates data on |
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:
| Sequence | |
|---|---|
1. Write 1 with write concern "majority" to P new2. Read 1 with read concern "local" to S 13. Write 2 with write concern "majority" to P new4. 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. |
| ❌ Read own writes. | Read 1 reads data from S 1 that doesn't reflect a state after Write 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 writes | Write 2 updates data on P new that reflects a state after Write 1. |
| ❌ Write follow read. | Write 2 updates data on |