etcd读取的serializable和linearizable选项

WithSerializable

WithSerializable makes ‘Get’ request serializable. By default, it’s linearizable. Serializable requests are better for lower latency requirement.
serializable可以使延迟更小,那么是什么原理呢?

Linearizability

Linearizability (also known as Atomic Consistency or External Consistency) is a consistency level between strict consistency and sequential consistency.

For linearizability, suppose each operation receives a timestamp from a loosely synchronized global clock. Operations are linearized if and only if they always complete as though they were executed in a sequential order and each operation appears to complete in the order specified by the program. Likewise, if an operation’s timestamp precedes another, that operation must also precede the other operation in the sequence.

For example, consider a client completing a write at time point 1 (t1). A client issuing a read at t2 (for t2 > t1) should receive a value at least as recent as the previous write, completed at t1. However, the read might actually complete only by t3. Linearizability guarantees the read returns the most current value. Without linearizability guarantee, the returned value, current at t2 when the read began, might be "stale" by t3 because a concurrent write might happen between t2 and t3.

etcd does not ensure linearizability for watch operations. Users are expected to verify the revision of watch responses to ensure correct ordering.

etcd ensures linearizability for all other operations by default. Linearizability comes with a cost, however, because linearized requests must go through the Raft consensus process. To obtain lower latencies and higher throughput for read requests, clients can configure a request’s consistency mode to serializable, which may access stale data with respect to quorum, but removes the performance penalty of linearized accesses' reliance on live consensus.

说起来有点啰嗦,并且对于read来说,貌似也没啥用,因为服务器和客户端之间还有一段不可预知的网络延迟,等到客户端真正收到答案的时候,即便是linearizable,也有可能过了好长一会了,etcd里的数据早就变更了。所以直接使用WithSerializable返回数据吧。
这里还有一个知识点,涉及到etcd的内部数据和流程,一个etcd read并不是瞬间完成的,它要先经过etcd内存中的b+树查找version,然后到bbolt里面根据version获得真正的value值。这个过程中有可能该key对应的value已经改变了,如果不是linerizable那么返回的数据就是过期的。不过,如我上面所说,因为客户端获得数据的时间点还要加上etcd和客户端之间不可控的网络延迟,所以这个linerizable其实也没啥用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值