Causal Consistency and Read and Write Concerns
With MongoDB’s causally consistent client sessions,different combinations of read and write concerns provide differentcausal consistency guarantees. Whencausal consistency is defined to imply durability, then the following table liststhe 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 andwrite operations with "majority"
write concern canguarantee all four causal consistency guarantees. That is,causally consistent client sessions can onlyguarantee causal consistency for:
- Read operations with
"majority"
read concern; i.e. theread operations that return data that has been acknowledged by amajority of the replica set members and is durable. - Write operations with
"majority"
write concern; i.e.the write operations that request acknowledgement that the operationhas been applied to a majority of the replica set’s voting members.
If causal consistency does not imply durability (i.e. writes may berolled back), then write operations with { w: 1 }
write concern can also provide causal consistency.
Note
The other combinations of read and write concerns may also satisfyall four causal consistency guarantees in some situations, but notnecessarily in all situations.
The read concern "majority"
and write concern"majority"
ensure that the four causal consistencyguarantees hold even in circumstances (such as with a networkpartition) where two members in a replica settransiently believe that they are the primary. And while bothprimaries can complete writes with { w: 1 }
write concern, only one primary will be able to completewrites with "majority"
write concern.
For example, consider a situation where a network partition divides afive member replica set:
With the above partition
- Writes with
"majority"
write concern can completeonP
new but cannot complete onP
old. - Writes with
{ w: 1 }
write concerncan complete on eitherP
old orP
new.However, the writes toP
old (as well as the writesreplicated toS
1) roll back once these members regaincommunication with the rest of the replica set. - After a successful write with
"majority"
writeconcern onP
new, causally consistent reads with"majority"
read concern can observe the write onP
new,S
2,andS
3. The readscan also observe the write onP
old andS
1 once they can communicate with the rest of the replicaset and sync from the other members of the replica set. Any writesmade toP
old and/or replicated toS
1during the partition are rolled back.
Scenarios
To illustrate the read and write concern requirements, the followingscenarios have a client issue a sequence of operations with variouscombination of read and write concerns to the replica set:
- Read Concern "majority" and Write concern "majority"
- Read Concern "majority" and Write concern {w: 1}
- Read Concern "local" and Write concern "majority"
- Read Concern "local" and Write concern {w: 1}
Read Concern "majority" and Write concern "majority"
The use of read concern "majority"
and write concern"majority"
in a causally consistent session providesthe following causal consistency guarantees:
✅ Read own writes ✅ Monotonic reads ✅ Monotonic writes ✅ Writes follow reads
Read Concern "majority" and Write concern {w: 1}
The use of read concern "majority"
and write concern{ w: 1 }
in a causally consistent sessionprovides 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
Read Concern "local" and Write concern {w: 1}
The use of read concern "local"
and write concern{ w: 1 }
in a causally consistent sessioncannot guarantee causal consistency.
❌ Read own writes ❌ Monotonic reads ❌ Monotonic writes ❌ Writes follow reads
This combination may satisfy all four causal consistency guarantees insome situations, but not necessarily in all situations.
Read Concern "local" and Write concern "majority"
The use of read concern "local"
and write concern"majority"
in a causally consistent session providesthe following causal consistency guarantees:
❌ Read own writes ❌ Monotonic reads ✅ Monotonic writes ❌ Writes follow reads
This combination may satisfy all four causal consistency guarantees insome situations, but not necessarily in all situations.