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的因果一致性客户端会话,读写入关注的不同组合提供了不同的因果一致性保证When causal consistency is defined to imply durability, then the following table lists the specific guarantees provided by the various combinations:当因果一致性被定义为意味着耐久性时,下表列出了各种组合提供的具体保证:

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 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:也就是说,因果一致的客户端会话只能保证以下方面的因果一致性:

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 }写入关注的写操作也可以提供因果一致性。

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.
  • Writes with { 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.
  • 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. The reads can also observe the write on P old and S1 once they can communicate with the rest of the replica set and sync from the other members of the replica set. Any writes made to P old and/or replicated to S 1 during the partition are rolled back.在分区期间对P old和/或复制到S 1的任何写入都将回滚。

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"

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(读取关注"majority",写入关注"majority"

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

Sequence序列Example实例

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.读取A项。

State of data with two primaries using read concern majority and write concern majority
Read own writes

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 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 (i.e. an earlier state reflects the data read by Read 1).
Note注意
Scenario 2 (Read Concern "majority" and Write Concern "majority")情景2(读取关注"majority"”和写入关注"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 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 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 (i.e. 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 (i.e. an earlier state reflects the data read by Read 1).

Read Concern "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

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

During the transient period with two primaries, because both Pold 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:

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 causal consistency implies durability

Read own writesRead 1 reads data from S 2 that does not reflect a state after Write 1.
Monotonic readsRead 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 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 (i.e. an earlier state reflects the data read by Read 1).

If causal consistency does not imply 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 (i.e. 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 (i.e. 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 Pold 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 Pold and S 1, and the two members sync from the other members of the replica set.

If causal consistency implies durability

Read own writesThe data read by Read 1 does not 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 (i.e. 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 (i.e. whose earlier state reflects the data read by Read 1).

If causal consistency does not imply 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 (i.e. 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 (i.e. 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})情景5(读关注"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:

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,对于qty小于或等于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 does not reflect a state after Read 1 (i.e. an earlier state does not 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 (i.e. an earlier state does not 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")情景6(读取关注“本地”并写下关注“多数”)

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 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 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 (i.e. an earlier state does not reflect the data read by Read 1).
←  Read Isolation, Consistency, and RecencyQuery Optimization →