mongodb中的read concern可以用来控制读副本集或分片集时的一致性和隔离性。
通过使用read concern和write concern可以适当的调整一致性和可用性保证级别,比如等待更强的一致性保证,或者放宽一致性要求以提供更高的可用性。
Read Concern级别:
-
"local":查询从实例返回数据,不能保证数据已经写入大多数副本集成员(比如可能会回滚)
default for:
从primary读;
从secondaries读,如果读与因果一致性session关联
"local"可以用在因果一致性session和事务
EXAMPLE:
假设:
1.所有 Write0之前的写操作都成功复制到所有副本集;
2.Writeprev是 Write0之前的写操作;
3.Write0s之后没有写操作。
Time | Event | Most Recent Write | Most Recent w: “majority” write |
---|---|---|---|
t0 | Primary applies Write0 | Primary: Write0 Secondary1: Writeprev Secondary2: Writeprev | Primary: Writeprev Secondary1: Writeprev Secondary2: Writeprev |
t1 | Secondary1 applies write0 | Primary: Write0 Secondary1: Write0 Secondary2: Writeprev | Primary: Writeprev Secondary1: Writeprev Secondary2: Writeprev |
t2 | Secondary2 applies write0 | Primary: Write0 Secondary1: Write0 Secondary2: Write0 | Primary: Writeprev Secondary1: Writeprev Secondary2: Writeprev |
t3 | Primary is aware of successful replication to Secondary1 and sends acknowledgement to client (primary知道成功复制到secondary1了,则向客户端发送确认) | Primary: Write0 Secondary1: Write0 Secondary2: Write0 | Primary: Write0 Secondary1: Writeprev Secondary2: Writeprev |
t4 | Primary is aware of successful replication to Secondary2 | Primary: Write0 Secondary1: Write0 Secondary2: Write0 | Primary: Write0 Secondary1: Writeprev Secondary2: Writeprev |
t5 | Secondary1 receives notice (through regular replication mechanism) to update its snapshot of its most recent w: “majority” write (Secondary1 接到通知) | Primary: Write0 Secondary1: Write0 Secondary2: Write0 | Primary: Write0 Secondary1: Write0 Secondary2: Writeprev |
t6 | Secondary2 receives notice (through regular replication mechanism) to update its snapshot of its most recent w: “majority” write | Primary: Write0 Secondary1: Write0 Secondary2: Write0 | Primary: Write0 Secondary1: Write0 Secondary2: Write0 |
下表总结"local" read concern读到的数据
Read Target | Time T | State of Data |
---|---|---|
Primary | After t0 | Data reflects Write0. |
Secondary1 | Before t1 | Data reflects Writeprev |
Secondary1 | After t1 | Data reflects Write0 |
Secondary2 | Before t2 | Data reflects Writeprev |
Secondary2 | After t2 | Data reflects Write0 |
- "available":查询从实例返回数据,不能保证数据已经写入大多数副本集成员(比如可能会回滚)
default for: 从secondaries读,如果读与因果一致性session没有关联
对不是分片的集合,"available"和"local"行为相同
对于使用因果一致性session,"available" read concern是不可见的。
- "majority":查询结果返回被副本集的大多数成员确认的数据,读操作返回的文档是持久化的,即使在失败的情况下
"majority"可以用在因果一致性session和事务。
对于不在多文档事务中的读操作,"majority" read concern 保证读的结果返回被副本集的大多数成员确认的数据,但是在多文档事务中的读操作,"majority" read concern 只有在write concern是"majority"提供保障。
EXAMPLE:
Read Target | Time T | State of Data |
---|---|---|
Primary | Before t3 | Data reflects Writeprev |
Primary | After t3 | Data reflects Write0 |
Secondary1 | Before t5 | Data reflects Writeprev |
Secondary1 | After t5 | Data reflects Write0 |
Secondary2 | Before or at t6 | Data reflects Writeprev |
Secondary2 | After t6 | Data reflects Write0 |
- "linearizable":查询数据返回读操作之前完成的被大多数成员确认的写操作,在返回结果之前,查询可以等待并发执行的将数据复制到大多数成员的写入操作。
如果大部分副本集成员在读取操作后崩溃并重新启动,在 writeConcernMajorityJournalDefault设置为true时,返回的文档是持久化的,设置为false时,写操作在大多数节点奔溃时会回滚。
只能对primary节点设置linearizable read concern,并且对因果一致性session是不可用的。
"snapshot":只在多文档事务中可用(还未仔细研究)
- If the transaction is not part of a causally consistent session, upon transaction commit with write concern
"majority"
, the transaction operations are guaranteed to have read from a snapshot of majority-committed data. - If the transaction is part of a causally consistent session, upon transaction commit with write concern
"majority"
, the transaction operations are guaranteed to have read from a snapshot of majority-committed data that provides causal consistency with the operation immediately preceding the transaction start.
多文档事务支持的read concern有:"local","majority","snapshot"。
Reference:
[1]mongodb4.0 manual:https://docs.mongodb.com/manual/