mongodb4.0学习总结二(read concern)

  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之后没有写操作。

Timeline of a write operation to a three member replica set.

TimeEventMost Recent WriteMost Recent w: “majority” write
t0Primary applies Write0

Primary: Write0

Secondary1: Writeprev

Secondary2: Writeprev

Primary: Writeprev

Secondary1: Writeprev

Secondary2: Writeprev

t1Secondary1 applies write0

Primary: Write0

Secondary1: Write0

Secondary2: Writeprev

Primary: Writeprev

Secondary1: Writeprev

Secondary2: Writeprev

t2Secondary2 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

t4Primary 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

t6Secondary2 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读到的数据

Timeline of a write operation to a three member replica set.

Read TargetTime TState of Data
PrimaryAfter t0Data reflects Write0.
Secondary1Before t1Data reflects Writeprev
Secondary1After t1Data reflects Write0
Secondary2Before t2Data reflects Writeprev
Secondary2After t2Data 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:

Timeline of a write operation to a three member replica set.

Read TargetTime TState of Data
PrimaryBefore t3Data reflects Writeprev
PrimaryAfter t3Data reflects Write0
Secondary1Before t5Data reflects Writeprev
Secondary1After t5Data reflects Write0
Secondary2Before or at t6Data reflects Writeprev
Secondary2After t6Data reflects Write0
  • "linearizable":查询数据返回读操作之前完成的被大多数成员确认的写操作,在返回结果之前,查询可以等待并发执行的将数据复制到大多数成员的写入操作。

如果大部分副本集成员在读取操作后崩溃并重新启动,在 writeConcernMajorityJournalDefault设置为true时,返回的文档是持久化的,设置为false时,写操作在大多数节点奔溃时会回滚。

只能对primary节点设置linearizable read concern,并且对因果一致性session是不可用的。

  • ​​​​​​​"snapshot":只在多文档事务中可用(还未仔细研究)
  1. 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.
  2. 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/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值