CAP 定理的简单理解

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/alading2009/article/details/79431007

布鲁斯在1999年提出了 CAP 定理,维基上的定义如下

  • 一致性(Consistence),Every read receives the most recent write or an error
  • 可用性(Availability),Every request receives a (non-error) response – without guarantee that it contains the most recent write
  • 分区容错性(Network partitioning),The system continues to operate despite an arbitrary number of messages being dropped (or delayed) by the network between nodes

当系统是单节点的时候,该节点的状态就是系统的状态。由于扩展系统能力的需要,而将系统扩展为多节点的分布式系统时,就引入了数据一致性的问题。

单节点的时候,系统和节点是二位一体的,节点的状态就是系统的状态,所谓一人吃饱,全家不饿。

而多节点的时候,为了维持所有节点数据的一致性,就必然要将一个节点的数据变化复制到多个节点。

在现实中,可能由于各种原因导致多个节点的通信机制出现问题,被分割为了几个区域,区域间的节点不能互相通信,即不能保持数据的一致性。(P成为了必选,要在出现分区的情况下也能够提供服务)

此时,有两种选择:

  1. 保障A,即数据更新复制到某个分区内的所有节点就行,不用复制到所有分布式节点,此时失去C;

  2. 保障C,将处在其他分区的节点设置为数据更新不可用,此时对外就不存在不同的数据版本,保证了数据的一致性,但是损失了A。

这里的A其实和我们通常认为的可用概念有点不同,容易引起误解。

看布鲁斯12年的 释疑 ,一开始就提到这三种性质

The CAP theorem states that any networked shared-data system can have at most two of three desirable properties:
+ consistency (C) equivalent to having a single up-to-date copy of the data;
+ high availability (A) of that data (for updates); and
+ tolerance to network partitions (P).

他所指的可用性是针对数据的更新操作而言的(而不是节点是否宕机这类整体故障),只有更新才会与强一致性存在直接的取舍权衡关系。

文章中举的这个例子就可以理解了

The easiest way to understand CAP is to think of two nodes on opposite sides of a partition. Allowing at least one node to update state will cause the nodes to become inconsistent, thus forfeiting C. Likewise, if the choice is to preserve consistency, one side of the partition must act as if it is unavailable, thus forfeiting A. Only when nodes communicate is it possible to preserve both consistency and availability, thereby forfeiting P.

可用性不是一个 [0,1] 问题,而是 0%~100% 的问题。当为了保障一致性而停用另一个分区节点的更新操作时,可以认为性质A损失了50%。


参考

http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.67.6951&rep=rep1&type=pdf

https://www.infoq.com/articles/cap-twelve-years-later-how-the-rules-have-changed

https://www.zybuluo.com/jewes/note/68185

猜你喜欢

转载自blog.csdn.net/alading2009/article/details/79431007