当我试图理解CAP中的“Availability”(A)和“Partition tolerance”(P)时,我发现很难理解各种文章的解释。

我有一种感觉,a和P可以同时出现(我知道事实并非如此,这就是我不能理解的原因!)

简单地解释一下,什么是A和P以及它们之间的区别?


当前回答

根据CAP定理,分布式系统有三个特征:

一致性(C)表示所有系统组件具有相同的信息。

系统的可用性(A)意味着它不会因为另一个系统故障而停止工作。

分区容差(P)表示在任意网络包丢失的情况下,系统将继续工作。

根据CAP定理,一个系统最多只能有这三个特征中的两个。(ap, cp, ca)

其他回答

一致性意味着整个集群中的数据是相同的,因此您可以从/写入任何节点并获得相同的数据。

可用性意味着即使集群中的某个节点宕机,也能够访问集群。

分区容忍意味着即使两个节点之间存在“分区”(通信中断)(两个节点都在工作,但不能通信),集群也能继续工作。

为了同时获得可用性和分区容忍,您必须放弃一致性。考虑一下在master-master设置中是否有两个节点X和Y。现在,X和Y之间的网络通信中断了,所以它们不能同步更新。此时你可以:

A)允许节点不同步(放弃一致性),或者

B)认为集群“关闭”(放弃可用性)

所有可用的组合是:

CA - data is consistent between all nodes - as long as all nodes are online - and you can read/write from any node and be sure that the data is the same, but if you ever develop a partition between nodes, the data will be out of sync (and won't re-sync once the partition is resolved). CP - data is consistent between all nodes, and maintains partition tolerance (preventing data desync) by becoming unavailable when a node goes down. AP - nodes remain online even if they can't communicate with each other and will resync data once the partition is resolved, but you aren't guaranteed that all nodes will have the same data (either during or after the partition)

您应该注意,CA系统实际上并不存在(即使有些系统声称存在)。

我觉得任何答案都没有很好地解释分区容忍,所以只是更详细地解释一下CAP定理的意思是:

C:(线性性或强一致性)大致是指

如果操作B在操作A成功完成后启动,则 操作B必须看到系统处于与打开时相同的状态 完成操作A,或更新状态(但绝不是旧状态)。

A:

“系统中非故障[数据库]节点接收到的每个请求 必须导致[非错误]响应”。这对某些人来说是不够的 节点能够处理请求:任何未失败的节点都需要这样做 能够处理它。许多所谓的“高可用性”(即低可用性) 停机时间)系统实际上不符合这个定义 可用性。

P:

分区容忍(命名不当)基本上意味着您 通过可能延迟或中断的异步网络进行通信 消息。互联网和我们所有的数据中心都有这个特性,所以 在这件事上你真的没有选择的余地。

来源:Martin kleppmann的作品

举个例子: 卡桑德拉最多只能是AP系统。但是,如果您将其配置为基于Quorum进行读写,那么它就不会保持CAP可用性(根据CAP定理的定义可用),而只是P系统。

理解CAP定理的简单方法:

In case of network partition, one needs to choose between perfect availability and perfect consistency. Picking consistency means not being able to answer a client's query as the system cannot guarantee to return the most recent write. This sacrifices availability. Picking availability means being able to respond to a client's request but the system cannot guarantee consistency, i.e., the most recent value written. Available systems provide the best possible answer under the given circumstance.

这个解释来自这篇优秀的文章。希望能有所帮助。

根据上图C是断开的,但A,B, D可以继续工作。现在我们可以调用系统部分工作(分区容忍)。

假设一个特定的事务只需要a、B和d,系统可以执行它而不会产生任何不一致。

但是当C必须参与一个特定的事务时,系统可以以两种方式执行。

1.由于C不可用,A可以拒绝用户请求。

So the system has Partition-Tolerance and consistency (P,C).
But no availability, because of the rejection.

2.A可以将C接收到的消息保存在A的本地内存中,并在C连接回来时传输。

So the system has Partition-Tolerance and availability (P,A).
But no consistency.because C has not updated.

简单的CAP定理指出,分布式系统不可能同时提供所有三个保证:

一致性

每个节点同时包含相同的数据

可用性

每次必须至少有一个节点可用以提供数据

分区容忍

系统的故障是非常罕见的

大多数情况下,每个系统只能保证至少两个特性:CA、AP或CP。