ZooKeeper 在 CAP 定理方面总是一致的吗?

Is ZooKeeper always consistent in terms of CAP theorem?

ZooKeeper 始终是 CP(根据 CAP 定理)是否正确? 或者有没有办法将它用作 AP 来满足服务发现需求?

不,您不能像在其他一些系统中那样更改当前版本的 ZooKeeper 中的一致性保证。

您可以向您的客户端添加一个本地缓存,这将使它们在集群出现故障时拥有只读数据,但就 CAP 而言,它仍然不是 A,因为它需要可用于更新和读取.

如果 ZK 为您的服务发现需求提供了太强的一致性级别,您应该尝试研究其他选项,例如尤里卡、领事或 etcd。

可能相关的读物:

一个很好的问题。

用CAP定理来说,"C"其实就是linearizability:

if operation B started after operation A successfully completed, then operation B must see the the system in the same state as it was on completion of operation A, or a newer state.

由于ZooKeeper中的写入在quorum确认后才算完成,因此仍然可能存在旧数据的陈旧节点。因此,严格来说,ZooKeeper 默认情况下不是 CP 系统,即使它提供了相当高的一致性。您可以通过在读取之前使用 sync 命令来确保线性化。

关于网络分区下的可用性,那些不占多数的节点无法再处理写入请求,因为它们没有法定人数。

另请参阅:

Zookeeper不是A,也不能drop P。所以貌似叫CP。在CAP定理方面,"C"其实就是线性化的意思。

linearizability : if operation B started after operation A successfully completed, then operation B must see the the system in the same state as it was on completion of operation A, or a newer state.

但是, Zookeeper 具有顺序一致性 - 来自客户端的更新将按发送顺序应用。

ZooKeeper 实际上并没有跨客户端视图同时保持一致。 http://zookeeper.apache.org/doc/trunk/zookeeperProgrammers.html#ch_zkGuarantees

ZooKeeper does not guarantee that at every instance in time, two different clients will have identical views of ZooKeeper data. Due to factors like network delays, one client may perform an update before another client gets notified of the change. Consider the scenario of two clients, A and B. If client A sets the value of a znode /a from 0 to 1, then tells client B to read /a, client B may read the old value of 0, depending on which server it is connected to. If it is important that Client A and Client B read the same value, Client B should should call the sync() method from the ZooKeeper API method before it performs its read.

ZooKeeper 提供 "sequential consistency"。这比线性化弱,但仍然很强,比 "eventual consistency" 强得多。 ZooKeeper 还提供了同步命令。如果您调用同步命令然后进行读取,则保证读取至少看到同步开始之前完成的最后一次写入。

linearizability,写入应该是瞬时的。不准确的是,一旦写入完成,所有以后的读取(其中“稍后”由挂钟开始时间定义)应该 return 该写入的值或稍后写入的值。一旦读取 return 一个特定值,所有以后的读取都应该 return 该值或以后写入的值。"

在 Zookeeper 中,他们有 sync() 方法可以在我们需要类似线性化的地方使用。

Serializability 是对事务或对一个或多个对象的一组或多个操作的保证。它保证在多个项目上执行一组事务(通常包含读取和写入操作)等同于事务的某些串行执行(总排序)。

参考: