With MongoDB's causally consistent client sessions, different combinations of read and write concerns provide different causal consistency guarantees. 对于MongoDB的因果一致性客户端会话,读写入关注的不同组合提供了不同的因果一致性保证。When causal consistency is defined to imply durability, then the following table lists the specific guarantees provided by the various combinations:当因果一致性被定义为意味着耐久性时,下表列出了各种组合提供的具体保证:
"majority" | "majority" | ✅ | ✅ | ✅ | ✅ |
"majority" | { w: 1 } | ✅ | ✅ | ||
"local" | { w: 1 } | ||||
"local" | "majority" | ✅ |
If causal consistency implies 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:也就是说,因果一致的客户端会话只能保证以下方面的因果一致性:
"majority"
read concern; i.e. the read operations that return data that has been acknowledged by a majority of the replica set members and is durable."majority"
读取关注的读取操作;即,返回大多数副本集成员已确认且持久的数据的读取操作。"majority"
write concern; i.e. the write operations that request acknowledgement that the operation has been applied to a majority of the replica set's voting members."majority"
写入关注;即,请求确认操作已应用于副本集的大多数投票成员的写操作。If causal consistency does not imply durability (i.e. writes may be rolled back), then write operations with 如果因果一致性并不意味着持久性(即写操作可能会回滚),那么具有{ w: 1 }
write concern can also provide causal consistency.{ w: 1 }
写入关注的写操作也可以提供因果一致性。
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:例如,考虑网络分区划分五个成员副本集的情况:
"majority"
write concern can complete on P
new but cannot complete on P
old.{ w: 1 }
write concern can complete on either P
old or P
new. 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."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. 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. P
old and/or replicated to S
1 during the partition are rolled back.P
old和/或复制到S
1的任何写入都将回滚。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:为了说明读写入关注的要求,以下场景要求客户机向复制集发出一系列操作,其中包含各种读写入关注组合:
"majority"
"majority"
"majority"
{w: 1}
"local"
"majority"
"local"
{w: 1}
"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
"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:P
new可以使用{ w: "majority" }
写入关注完成写操作,因此客户端会话可以成功地发出以下操作序列:
1. Write 1 with write concern 2. Read 1 with read concern 3. Write 2 with write concern 4. Read 2 with read concern | For item Read item For items with update
|
✅ Read own writes | Read 1 reads data from Read 2 reads data from |
✅ 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 (i.e. an earlier state reflects the data read by Read 1).
|
"majority"
”和写入关注"majority"
)Consider an alternative sequence where Read 1 with read concern "majority"
routes to S
1:
Sequence | Example |
---|---|
1. Write 1 with write concern 2. Read 1 with read concern 3. Write 2 with write concern 4. Read 2 with with read concern | For item Read item For items with update Read item |
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 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 |
✅ Monotonic reads | Read 2 reads data from S 3 that reflects a state after Read 1 (i.e. 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 (i.e. an earlier state reflects the data read by Read 1).
|
"majority"
and Write concern {w: 1}
"majority"
和写入关注{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 causal consistency implies durability:
❌ Read own writes ✅ Monotonic reads ❌ Monotonic writes ✅ Writes follow reads
If causal consistency does not imply durability:
✅ Read own writes ✅ Monotonic reads ✅ Monotonic writes ✅ Writes follow reads
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 causal consistency implies durability:
Sequence | Example |
---|---|
1. Write 1 with write concern 2. Read 1 with read concern 3. Write 2 with write concern 4. Read 2 with with read concern | For item Read item For items with update Read item |
In this sequence,
P
new past the time of Write 1.P
new past the time of Write 2.➤ If causal consistency implies durability
❌ Read own writes | Read 1 reads data from S 2 that does not reflect a state after Write 1.
|
✅ Monotonic reads | Read 2 reads data from S 3 that reflects a state after Read 1 (i.e. 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 P new that reflects a state after Read 1 (i.e. an earlier state reflects the data read by Read 1).
|
➤ If causal consistency does not imply durability
✅ Read own writes | Read 1 reads data from S 2 returns data that reflects a state equivalent to Write 1 followed by rollback of Write 1.
|
✅ Monotonic reads | Read 2 reads data from S 3 that reflects a state after Read 1 (i.e. an earlier state is reflected in the data read by Read 1).
|
✅ Monotonic writes | Write 2 updates data on P new that is equivalent to after Write 1 followed by rollback of Write 1.
|
✅ Writes follow reads | Write 2 updates data on P new that reflects a state after Read 1 (i.e. whose earlier state reflects the data read by Read 1).
|
Consider an alternative sequence where Read 1 with read concern "majority"
routes to S
1:
Sequence | Example |
---|---|
1. Write 1 with write concern 2. Read 1 with read concern 3. Write 2 with write concern 4. Read 2 with with read concern | For item Read item For items with update Read item |
In this sequence:
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 causal consistency implies durability
❌ Read own writes | The data read by Read 1 does not reflect the results of Write 1, which has rolled back. |
✅ Monotonic reads | Read 2 reads data from S 3 that reflects a state after Read 1 (i.e. whose earlier state reflects the data read by Read 1).
|
❌ 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 (i.e. whose earlier state reflects the data read by Read 1).
|
➤ If causal consistency does not imply 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 (i.e. an earlier state reflects the data read by Read 1).
|
✅ Monotonic writes | Write 2 updates data on P new that is equivalent to after Write 1 followed by rollback of Write 1.
|
✅ Writes follow reads | Write 2 updates data on P new that reflects a state after Read 1 (i.e. an earlier state reflects the data read by Read 1).
|
"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.在某些情况下,这种组合可能满足所有四种因果一致性保证,但不一定在所有情况下都满足。
"local"
,写入关注{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 | Example |
---|---|
1. Write 1 with write concern 2. Read 1 with read concern 3. Write 2 with write concern 4. Read 2 with read concern | For item Read item
update Read item |
❌ Read own writes | Read 2 reads data from S 3 that only reflects a state after Write 2 and not Write 1
followed by Write 2.
|
❌ Monotonic reads | Read 2 reads data from S 3 that does not reflect a state after Read 1 (i.e. an earlier state does not 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 P new that does not reflect a state after Read 1 (i.e. an earlier state does not reflect the data read by Read 1).
|
"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.在某些情况下,这种组合可能满足所有四种因果一致性保证,但不一定在所有情况下都满足。
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 | Example |
---|---|
1. Write 1 with write concern 2. Read 1 with read concern 3. Write 2 with write concern 4. Read 2 with read concern | For item Read item For items with update Read item |
❌ Read own writes. | Read 1 reads data from S 1 that does not reflect a state after Write1 1.
|
❌ Monotonic reads. | Read 2 reads data from S 3 that does not reflect a state after Read 1 (i.e. an earlier state does not 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 P new that does not reflect a state after Read 1 (i.e. an earlier state does not reflect the data read by Read 1).
|