Database Manual / CRUD Operations / CRUD Concepts / Read Isolation, Consistency, & Recency

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

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 Pold (as well as the writes replicated to S1) 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:在因果一致性会话中使用读关注"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" }写关注的写操作,因此客户端会话可以成功发出以下操作序列:

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.
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 (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 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 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 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).

If you want causal consistency without data durability如果你想在没有数据持久性的情况下保持因果一致性

Read own writes

Read 1 reads data from S 2 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 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 (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 reads

Read 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 writes

Write 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 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 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 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).

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 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 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).