Presentation is loading. Please wait.

Presentation is loading. Please wait.

CAP:理论与实践 童家旺 http://www.dbthink.com/ Weibo: jametong.

Similar presentations


Presentation on theme: "CAP:理论与实践 童家旺 http://www.dbthink.com/ Weibo: jametong."— Presentation transcript:

1 CAP:理论与实践 童家旺 Weibo: jametong

2 内容大纲 CAP的历史沿革 CAP原理介绍 CAP,Pick Two? CAP权衡中的经济考量 CAP与ACID的关系
分区只是另一段Code Path CAP在实践中的应用 参考资料

3 CAP原理的历史 1997年,Fox & Brewer提出BASE概念 1999年,Fox & Brewer 提出CAP Principle
《Cluster-Based Scalable Network Services》 1999年,Fox & Brewer 提出CAP Principle 《Harvest, Yield, and Scalable Tolerant Systems》 2000年,PODC的Keynotes,正式提出CAP 《Towards Robust Distributed Systems》 2002年,Seth Gilbert & Nancy Lynch证明CAP 《Brewer’s Conjecture and the Feasibility of Consistent, Available, Partition-Tolerant Web Services》

4 CAP流行的几大推手 OSDI 2006,Google发布Bigtable的论文 SOSP 2007,Amazon发布Dynamo的论文
Bigtable: A Distributed Storage System for Structured Data SOSP 2007,Amazon发布Dynamo的论文 Dynamo: Amazon’s Highly Available Key-value Store 2007年8月,Amazon的Werner Vogels推介CAP Availability & Consistency 2007年10月,Werner Vogels撰文介绍最终一致性 Eventually Consistent 2008年1月,Ebay的架构师Dan Prichett介绍BASE BASE: An Acid Alternative

5 CAP Theorem 对于共享的数据系统,仅能同时满足2项: 在广域网的情况下,分区不可避免
Consistency (多节点看到数据的单一/同一副本) Full Consistency? Casual Consistency? Timeline Consistency? Eventual Consistency? Availability (系统总是可以执行变更操作) 牺牲10秒钟的A?牺牲10分钟的A? Partition Tolerance 在广域网的情况下,分区不可避免 => consistency vs. availability

6 CAP ,Pick Two? Traditional databases guarantee consistency.
The CAP theorem tells you that you cannot have consistency, availability, and fault-tolerance at the same time. But we want to build scalable databases, so we forget about consistency. Oh and by the way, who needs consistency anyway?

7 面对P,真的必须牺牲一致性吗? 淘宝的商品信息可以暂时不一致吗? 12306提供的库存信息可以不准确吗?
新浪微博可以丢吗? 淘宝的商品信息可以暂时不一致吗? 12306提供的库存信息可以不准确吗? 比特币(bitcoin)可以不准确吗? 中行信用卡可以不一致吗? 工行可以在主机故障时,牺牲一致性吗?

8 Reliability & $$

9 数据状态机的分类 何谓状态机 简单的理解是,计算机中会发生变化的数据都是状态机,这个数据的值不同可能会带来不同的后果。
分类:按照三个维度:时间、信息含金量、变更频率 持续时间 信息含金量 变更频繁度 例子 瞬时 Shopping Card Session(分) Login Cookie(分) 中等时长 Ecommerce Billing(天) Product Catalog(年) Flight/Train Inventory (月) 无限时长 User Profile(年) Bank Account Balance(年))

10 其实,CAP并没有声明… 放弃一致性 放弃事务 (ACID) 不要使用SQL 不一致应该仅仅是个例外 很多系统牺牲的内容远远超过必要!
很多NOSQL系统中也开始支持SQL 声明性语言(SQL)与CAP配合良好

11 CAP与ACID 当系统没有Partition时: 当系统出现Partition时: 支持Full ACID
Atomic:不同的分区还是应该保持Atomic Consistent:临时违背(如:没有重复?) Isolation:临时牺牲隔离性 Durable:永远不该牺牲它(需要保留它用)

12 像程序员一样思考 分区只是个不同的代码路径

13 User Profile处理 Always Relaxing Read Consistency Cache APP Server
Write to Master, When Failed, Fail the Request Reading From arbitrary node Backup Master Read Slave Read Slave Read Slave Read Slave Near Real Time Redo Shipping Always Relaxing Read Consistency

14 Message Processing App Server Persistent Message in arbitrary Message Node When One Failed Just Pick Next Msg DB1 Msg DB2 Msg DB3 Msg DB4 DB1 Backup DB2 Backup DB3 Backup DB4 Backup When One Node Failed, Just Delay the Message Sending Persistent in that Node, Aka, Sacrificing Availability

15 Shopping Cart Relaxing Consistency When Partitioned Writing Reading
Write multiple replicas to multi node, If one node Fails, Just Skip it. Merging multiple replicas from multi node, if one node Fails, just ignore it. DB1 DB2 DB3 Relaxing Consistency When Partitioned

16 银行ATM机 When Partitioned When Partition Recovered
只允许低于200$的提现操作 在本地记录操作的日志 When Partition Recovered Reapply本地日志 如果有透支,通过外部商业流程进行补偿处理。 Sacrifice Some A,Relax Some Consistency

17 航空订票系统 超卖是一种商业选择 如果出现超卖 When Partition 仅限经济舱 为用户做免费升舱
Relaxing Consistency 一定的补偿机制

18 CAP的实际含义

19 总结 CAP的实际效果 在没有分区发生时 探索适合不同应用的一致性与可用性平衡 可以同时满足C与A,以及完整的ACID事务支持
需要有分区开始前、进行中、恢复后的处理策略 应用合适的补偿处理机制

20 参考资料 NoSQL: Past, Present, Future
By Eric Brewer CAP Twelve Years Later: How the "Rules" Have Changed Towards Robust Distributed Systems Dynamo: Amazon's Highly Available Key-value Store By Giuseppe DeCandia, Werner Vogels etc..

21 Any Questions?


Download ppt "CAP:理论与实践 童家旺 http://www.dbthink.com/ Weibo: jametong."

Similar presentations


Ads by Google