CAP – Consistency, Availability, Partition Tolerance

----------------------------------------------------------------------------------------------------------------------

http://blog.nahurst.com/visual-guide-to-nosql-systems

相当不错的ppt: http://www.slideshare.net/jboner/scalability-availability-stability-patterns

再Mark一下Nosql的总站, http://nosql-database.org/

对于CAP的权威解释参看下面的文章,

http://www.julianbrowne.com/article/viewer/brewers-cap-theorem

中文翻译版:

http://pt.alibaba-inc.com/wp/dev_related_728/brewers-cap-theorem.html

----------------------------------------------------------------------------------------------------------------------------- 

关系型数据库和 ACID

在讨论大数据和CAP之前先看看关系数据库的ACID

关系数据库, 最大的特点就是事务处理, 即满足ACID, 如下所示, 其实ACID这个理论本身也有些confuse

引用, Jim Gray和我讨论了这些缩写,他欣然认可ACID也有些扭曲(stretch)– A和D(的概念)有相当多的重复部分,C至多也是含糊不清的。

所以我们可以理解为ACID最重要的含义, 就是Atomicity和Isolation , 即强一致型, 要么全做,要么不做, 所有用户看到的数据一致.

这也是关系数据库, 这么多年长盛不衰的原因, 强调数据的可靠性, 一致性和可用性.

ACID, which stands for Atomicity, Consistency, Isolation, and Durability, has become the gold standard to define the highest level of transactional integrity in database systems. As 
the acronym suggests it implies the following:

Atomicity — Either a transactional operation fully succeeds or completely fails. 这个很容易理解, 最简单就是银行的例子, 你修改记录和吐钱, 必须atomic

Consistency — Consistency implies that data is never persisted if it violates a predefined constraint or rule. For example, if a particular field states that it should hold only integer values, then a float value is not accepted or is rounded to the nearest integer and then saved.

这个和nosql里面的意义不一样, 这儿单纯就是不能破坏预定的rule和约束 
Isolation — Isolation gets relevant where data is accessed concurrently.

控制隔离数据以供一个进程使用并防止其它进程干扰的程度的事务属性. 当多个事务同时进行时,通过设置隔离级别来处理脏读、不可重复读、幻读事件

read uncommitted | 0 未提交读

将查询的隔离级别指定为 0。

可以读脏数据

读脏数据:一事务对数据进行了增删改,但未提交,有可能回滚,另一事务却读取了未提交的数据

read committed | 1 已提交读

将查询的隔离级别指定为 1。

避免脏读,但可以出现不可重复读和幻读

不可重复读:一事务对数据进行了更新或删除操作,另一事务两次查询的数据不一致

幻像读:一事务对数据进行了新增操作,另一事务两次查询的数据不一致

repeatable read | 2 可重复读

将查询的事务隔离级别指定为 2。

避免脏读,不可重复读,允许幻像读

serializable | 3 可序列化

将查询的隔离级别指定为 3。

串行化读,事务只能一个一个执行,避免了脏读、不可重复读、幻读

执行效率慢(我遇到过一种情况,用时是隔离级别1的30倍),使用时慎重

Durability — Durability implies that once a transactional operation is confirmed, it is assured. 
An RDBMS often maintains a transaction log. A transaction is confirmed only after it’s written to the transaction log. If a system fails between the confirmation and the data persistence, the transaction log is synchronized with the persistent store to bring it to a consistent state.

 

 

大数据和CAP理论

但是随着数据量越来越大, 分布式数据存储成了必然的选择, 在分布式的环境中, 我们是否可以实现强一致性, 高可用性的数据存储方案了?

这儿就不得不提Brewer(CAP)定理http://code.alibabatech.com/blog/dev_related_728/brewers-cap-theorem.html

这个被认为是具有划时代意义的理论, 表明在分布式环境中, 在强一致性和高可用性之间必须有取舍

引用一下关于CAP的定义,

Consistency

A service that is consistent operates fully or not at all. Gilbert and Lynch use the word "atomic" instead of consistent in their proof, which makes more sense technically.

Availability

Availability means just that - the service is available (to operate fully or not as above).

Partition Tolerance

说白了, 就是在分布式环境中, 如果某些节点crash, 系统是否还能正常工作

Gilbert & Lynch defined partition tolerance as:

No set of failures less than total network failure is allowed to cause the system to respond incorrectly

Parallel processing and scaling out are proven methods and are being adopted as the model for scalability and higher performance as opposed to scaling up and building massive super computers.

Partition tolerance measures the ability of a system to continue to service in the event a few of its cluster members become unavailable.

 

CAP选择

当处理CAP的问题时,你会有几个选择, 参考下图

 

放弃Partition Tolerance

传统的关系型数据库, 把所有数据都存在单个节点上, 所以肯定不具有partition tolerance, 节点crash就无法提供服务.

一般而言, Nosql方案不会放弃这个特性, 因为对于分布式系统, 一个partition crash就导致系统不可用, 那么就没有意义了, 我还不如用关系型数据库了.

所以只能在一致性与可用性之间做出选择

Consistent, Available (CA) Systems have trouble with partitions and typically deal with it with replication. Examples of CA systems include:

  • Traditional RDBMSs like Postgres, MySQL, etc (relational)
  • Vertica (column-oriented)
  • Aster Data (relational)
  • Greenplum (relational)

 

放弃Availability

我们需要保证一致性, 原子性. 当一个数据被别人操作时, 你就必须等待, 等待时就无法保证可用性.

Consistent, Partition-Tolerant (CP) Systems have trouble with availability while keeping data consistent across partitioned nodes. Examples of CP systems include:

 

放弃Consistency

对于大多数的互联网应用来讲,强一致性并不是非常重要的。和一致性比起来,可用性更加重要性一些。最终一致性(Eventually Consistent)简单的讲就是在某一个短暂的时间内数据可以不一致,但是在无限长的时间内,所有节点上的replica最终会达到完全一致。

上面那个问题, 只要放弃强一致性, 就可以保证可用性, 亲, 你先将就读吧...

Available, Partition-Tolerant (AP) Systems achieve "eventual consistency" through replication and verification. Examples of AP systems include:

 

BASE vs. ACID

有一种架构的方法(approach)称作BASEBasically Available, Soft-state, Eventually consistent)支持最终一致概念的接受。BASE(注:化学中的含义是碱),如其名字所示,是ACID(注:化学中的含义是酸)的反面. 关于BASE, 我们参考BASE: An Acid Alternativehttp://www.dbthink.com/?p=483

在对数据库进行分区后,为了可用性(Availability)牺牲部分一致性(Consistency)可以显著的提升系统的可伸缩性(Scalability).

Horizontal data scaling can be performed along two vectors. Functional scaling involves grouping data by function and spreading functional groups across databases. Splitting data within functional 
areas across multiple databases, or sharding, adds the second dimension to horizontal scaling.

 

如图, 面对大数据进行水平扩展时, 有两个维度, functional scaling和sharding.

对于这样的分布式系统, 如果还要保证事务的ACID, 就需要引入2PC(两阶段提交), 该技术提供跨越多个数据库实例的ACID保证.这个协议分为以下两个阶段:

  • 第一阶段,事务协调器要求每个涉及到事务的数据库预提交(precommit)此操作,并反映是否可以提交.
  • 第二阶段,事务协调器要求每个数据库提交数据.

如果有任何一个数据库否决此次提交,那么所有数据库都会被要求回滚它们在此事务中的那部分信息. 这样做的缺陷是, 我们可以在分区之间获得一致性的同时,牺牲了可用性.

那么一种解决方案就是弱化一致性来保证可用性, 但是并不是完全牺牲一致性, 而是达到最终一致性. 这就是BASE的思想

强一致性

 

完全放弃一致性

 

弱一致性(最终一致性)

 

 

也许解释不如上面这三段代码来的清楚

Basically Available, 不是完全可用, 保证部分可用

Soft-state, 这个我的理解, 就是在不一致过渡到最终一致, 之间的状态. 比如事务转账, 钱从一个帐号划出, 但还没有划入另一个帐号, 这之间的延时就照成软状态.

Eventually consistent

本文章摘自博客园,原文发布日期:2012-02-24

时间: 2024-08-30 22:43:52

CAP – Consistency, Availability, Partition Tolerance的相关文章

Deploy Web Apps with High Availability, Fault Tolerance, and Load Balancing on Alibaba Cloud

By Ngoi Se Keng, Alibaba Cloud Tech Share Author 1. Introduction High Availability (HA), Fault Tolerance (FT), and Horizontal Scale Friendly (HSF) are as equally important as to functionality for web applications to run and succeed today. Existing or

NoSQL Databases技术资料整理汇总

0 Reference NoSQL论文 在 Stuttgart Media 大学的 Christof Strauch 历时8个月(2010年6月-2011年2月)完成了一篇150页长的NoSQL相关的论文, 对NoSQL的各个方面做了探讨 http://www.christof-strauch.de/nosqldbs.pdf 分布式系统领域经典论文翻译集 http://duanple.blog.163.com/blog/static/709717672011330101333271/ 2010

微服务~分布式事务里的最终一致性

本地事务ACID大家应该都知道了,统一提交,失败回滚,严格保证了同一事务内数据的一致性!而分布式事务不能实现这种ACID,它只能实现CAP原则里的某两个,CAP也是分布式事务的一个广泛被应用的原型,CAP(Consistency, Availability, Partition Tolerance), 阐述了一个分布式系统的三个主要方面, 只能同时择其二进行实现. 常见的有CP系统, AP系统. 应用于CP和AP的原则在业界出现了一些框架: CP系统就有二阶段提交(强一致性) AP系统就有TCC

领域驱动设计常见问题FAQ

本文出处:http://www.cqrs.nu/Faq What is a domain? The field for which a system is built. Airport management, insurance sales, coffee shops, orbital flight, you name it. It's not unusual for an application to span several different domains. For example, a

架构师论文

如何写论文 用10分钟在草稿纸上写出思维导图, 例如下面是分布式缓存系统设计的导图. 如何提高系统架构师写作水平: http://www.cnblogs.com/muhongxing/archive/2010/10/10/1847476.html 素材 敏捷(scrum,XP),MVC,插件结构(OSGI),云(PaaS, IaaS, SaaS),SOA(Restful) 论文构思 - 使用思维导图 - 仔细阅读题目 - 头脑风暴,将能想到的素材写作草稿纸的右方(好的关键词,或句子) - 拟定总

分布式系统英文参考资料

原文地址:http://www.dancres.org/reading_list.html Introduction I often argue that the toughest thing about distributed systems is changing the way you think. The below is a collection of material I've found useful for motivating these changes. Thought Pr

【原创】RabbitMQ 之 Distributed brokers(翻译)

Distributed RabbitMQ brokers  AMQP and the other messaging protocols supported by RabbitMQ via plug-ins (e.g. STOMP), are (of course) inherently distributed - it is quite common for applications from multiple machines to connect to a single broker, e

关于CAP定理的个人理解

CAP定理简介 在理论计算机科学中,CAP定理(CAP theorem),又被称作布鲁尔定理(Brewer's theorem),它指出对于一个分布式计算系统来说,不可能同时满足以下三点: 一致性(Consistency):同一个数据在集群中的所有节点,同一时刻是否都是同样的值. 可用性(Availability):集群中一部分节点故障后,集群整体是否还能处理客户端的更新请求. 分区容忍性(Partition tolerance):是否允许数据的分区,分区的意思是指是否允许集群中的节点之间无法通

分布式系统设计权衡:CAP

写在最前: 1.为什么学习并记录分布式设计理念一系列相关的东西 在日常工作中系统设计评审的时候,经常会有一些同事抛出一些概念,高可用性,一致性等等字眼,他们用这些最基本的概念去反驳系统最初的设计,但是很多人理解的可用性,一致性等等问题,都是自己拍脑袋想的,或者根本和最原始表达的意思就不是一个东西,在这种情况下PK,就像不再一个频段的人在交流,除了争论,没有任何实质性的进展,所以有必要熟悉其理论基础,以免贻笑大方.(其实类似的例子还有很多,国内的技术人员都喜欢把一些此词模糊化,混淆而谈.例如XX云