[转]A plain english introduction to cap theorem
A plain english introduction to CAP Theorem
You’ll often hear about the CAP theorem which specifies some kind of an upper limit when designing distributed systems. As with most of my other introduction tutorials, lets try understanding CAP by comparing it with a real world situation.
Chapter 1: “Remembrance Inc” Your new venture :
Last night when your spouse appreciated you on remembering her birthday and bringing her a gift, a strange Idea strikes you. People are so bad in remembering things. And you’re sooo good at it. So why not start a venture that will put your talent to use? The more you think about it, the more you like it. In fact you even come up with a news paper ad which explains your idea
Remembrance Inc! - Never forget, even without remembering!
Ever felt bad that you forget so much? Don't worry. Help is just a phone away!
When you need to remember something, just call 555--55-REMEM and tell us what you need to remember. For eg., call us and let us know of your boss's phone number, and forget to remember it. when you need to know it back.. call back the same number[(555)--55-REMEM ] and we'll tell you what's your boss's phone number.
Charges : only $0.1 per request
So, your typical phone conversation will look like this:
- Customer : Hey, Can you store my neighbor’s birthday?
- You: Sure.. when is it?
- Customer : 2nd of jan
- You: (write it down against the customer’s page in your paper note book )Stored. Call us any time for knowing your neighbor’s birthday again!
- Customer : Thank you!
- You: No problem! We charged your credit card with $0.1
Chapter 2 : You scale up:
Your venture gets funded by YCombinator. Your Idea is so simple,
needs nothing but a paper notebook and phone, yet so effective that it
spreads like wild fire. You start getting hundreds of call every day.
And there starts the problem. You see that more and more of your
customers have to wait in the queue to speak to you. Most of them even
hang up tired of the waiting tone. Besides when you were sick the other
day and could not come to work you lost a whole day of business. Not to
mention all those dissatisfied customers who wanted information on that
day.
You decide it’s time for you to scale up and bring in your wife to help you.
Your start with a simple plan:
- You and your wife both get an extension phone
- Customers still dial (555)–55-REMEM and need to remember only one number
- A pbx will route the a customers call to whoever is free and equally
Chapter 3 : You have your first “Bad Service” :
Two days after you implemented the new system, you get a call from
you get a call from your trusted customer Jhon. This is how it goes:
- Jhon: Hey
- You: Glad you called “Remembrance Inc!”. What can I do for you?
- Jhon: Can you tell me when is my flight to New Delhi?
- You: Sure.. 1 sec sir
(You look up your notebook)
(wow! there is no entry for “flight date” in Jhon’s page)!!!!! - You: Sir, I think there is a mistake. You never told us about your flight to delhi
- Jhon: What! I just called you guys yesterday!(cuts the call!)
How did that happen? Could Jhon be lying? You think about it for a
second and the reason hits you! Could Jhon’s call yesterday reached your
wife? You go to your wife’s desk and check her notebook. Sure enough
it’s there. You tell this to your wife and she realizes the problem too.
What a terrible flaw in your distributed design! Your
distributed system is not consistent! There could always be a chance
that a customer updates something which goes to either you or your wife
and when the next call from the customer is routed to another person
there will not be a consistent reply from Remembrance Inc!
Chapter 4: You fix the Consistency problem:
Well, your competitors may ignore a bad service, but not you. You
think all night in the bed when your wife is sleeping and come up with a
beautiful plan in the morning. You wake up your wife and tell her:
” Darling this is what we are going to do from now”
- Whenever any one of us get a call for an update(when the customer
wants us to remember something) before completing the call we tell the
other person - This way both of us note down any updates
- When there is call for search(When the customer wants information
he has already stored) we don’t need to talk with the other person.
Since both of us have the latest updated information in both of our note
books we can just refer to it..
There is only one problem though, you say, and that is an “update”
request has to involve both of us and we cannot work in parallel during
that time. For eg. when you get an update request and telling me to
update too, i cannot take other calls. But that’s okay because most
calls we get anyway are “search” (a customer updates once and asks many
times) . Besides, we cannot give wrong information at any cost.
“Neat” your wife says, “but there is one more flaw in this system
that you haven’t thought of. What if one of us doesn’t report to work on
a particular day? On that day, then, we won’t be able to take “any”
Update calls, because the other person cannot be updated! We will have Availability
problem , i.e, for eg., if an update request comes to me I will never
be able to complete that call because even though I have written the
update in my note book, I can never update you. So I can never complete
the call!”
Chapter 5: You come up with the greatest solution Ever:
You being to realize a little bit on why distributed system might not
be as easy as you thought at first. Is it that difficult to come up
with a solution that could be both “Consistent and Available”?
Could be difficult for others, but not for you!! Then next morning you
come up with a solution that your competitors cannot think of in their
dreams! You wake your wife up eagerly again..
” look” , you tell her.. “This is what we can do to be consistent and
available” . The plan is mostly similar to what I told you yesterday:
- i) Whenever any one of us get a call for an update(when the
customer wants us to remember something) before completing the call, if
the other person is available we tell the other person. This way both of
us note down any updates - ii) But if the other person is not available(doesn’t report to work) we send the other person an email about the update.
- iii) The next day when the other person comes to work after taking a
day off, He first goes through all the emails, updates his note book
accordingly.. before taking his first call.
Genius! You wife says! I can’t find any flaws in this systems. Let’s put it to use.. Remembrance Inc! is now both Consistent and available!
Chapter 6: Your wife gets angry :
Everything goes well for a while. Your system is consistent. Your
system works well even when one of you doesn’t report to work. But what
if Both of you report to work and one of you doesn’t update the other
person? Remember all those days you’ve been waking your wife up early
with your Greatest-idea-ever-bullshit? * What if your wife decides to
take calls but is too angry with you and decides not to update you for a
day? Your idea totally breaks! Your idea so far is good for consistency
and availability but is not Partition Tolerant!*
You can decide to be partition tolerant by deciding not to take any
calls until you patch up with your wife.. Then your system will not be
“available” during that time…
Chapter 7: Conclusion :
So Let’s look at CAP Theorem now. Its
states that, when you are designing a distributed system you can get
cannot achieve all three of Consistency, Availability and Partition
tolerance. You can pick only two of:
- Consistency: You customers, once they have updated information with
you, will always get the most updated information when they call
subsequently. No matter how quickly they call back - Availability: Remembrance Inc will always be available for calls until any one of you(you or your wife) report to work.
- Partition Tolerance: Remembrance Inc will work even if there is a communication loss between you and your wife!
Bonus : Eventual Consistency with a run around clerk :
Here is another food for thought. You can have a run around clerk,
who will update other’s notebook when one of your’s or your wife’s note
books is updated. The greatest benefit of this is that, he can work in
background and one of your or your wife’s “update” doesn’t have to
block, waiting for the other one to update. This is how many NoSql
systems work, one node updates itself locally and a background process
synchronizes all other nodes accordingly… The only problem is that you
will lose consistency of some time. For eg., a customer’s call reaches
your wife first and before the clerk has a chance to update your
notebook , the customer’ calls back and it reaches you. Then he won’t
get a consistent reply.. But that said, this is not at all a bad idea if
such cases are limited. For eg., assuming a customer won’t forget
things so quickly that he calls back in 5 minutes.
That’s CAP and eventual consistency for you in simple english :)
[转]A plain english introduction to cap theorem的更多相关文章
- 【翻译】Brewer's CAP Theorem CAP定理
Brewer's CAP Theorem 原文地址:http://www.julianbrowne.com/article/brewers-cap-theorem Brewer’s (CAP) The ...
- An Illustrated Proof of the CAP Theorem
An Illustrated Proof of the CAP Theorem The CAP Theorem is a fundamental theorem in distributed syst ...
- A Plain English Guide to JavaScript Prototypes
When I first started learning about JavaScript object model my reaction was of horror and disbelief. ...
- CAP theorem
https://en.wikipedia.org/wiki/CAP_theorem
- 「2014-2-23」Note on Preliminary Introduction to Distributed System
今天读了几篇分布式相关的内容,记录一下.非经典论文,非系统化阅读,非严谨思考和总结.主要的着眼点在于分布式存储:好处是,跨越单台物理机器的计算和存储能力的限制,防止单点故障(single point ...
- Note on Preliminary Introduction to Distributed System
今天读了几篇分布式相关的内容,记录一下.非经典论文,非系统化阅读,非严谨思考和总结.主要的着眼点在于分布式存储:好处是,跨越单台物理机器的计算和存储能力的限制,防止单点故障(single point ...
- 分布式系统(Distributed System)资料
这个资料关于分布式系统资料,作者写的太好了.拿过来以备用 网址:https://github.com/ty4z2008/Qix/blob/master/ds.md 希望转载的朋友,你可以不用联系我.但 ...
- 想从事分布式系统,计算,hadoop等方面,需要哪些基础,推荐哪些书籍?--转自知乎
作者:廖君链接:https://www.zhihu.com/question/19868791/answer/88873783来源:知乎 分布式系统(Distributed System)资料 < ...
- 从事分布式系统,计算,hadoop
作者:廖君链接:https://www.zhihu.com/question/19868791/answer/88873783来源:知乎 分布式系统(Distributed System)资料 < ...
随机推荐
- hadoop主节点(NameNode)备份策略以及恢复方法
link:http://jiajun.iteye.com/blog/809125 一.dits和fsimage 首先要提到两个文件edits和fsimage,下面来说说他们是做什么的. 集群中的名称节 ...
- Linux CC攻击脚本
CC(ChallengeCollapsar)主要是用来攻击页面的.大家都有这样的经历,就是在访问论坛时,如果这个论坛比较大,访问的人比较多,打开页面的速度会比较慢,访问的人越多,论坛的页面越多,数据库 ...
- eclipse-mysql-tomcat搭建jspk开发环境
...本来不想写,刚刚给女朋友又安了一次发现几乎忘了,还是记一下吧.. 1.默认安装好jdk以及eclipse或相关ide. 2.检查jdk环境变量是否配置成功:cmd下输入 java -versio ...
- 伪命题:PHP识别url重写请求
手上有一个网站,然后启用了伪静态,因为一些设置上的原因,一段时间后,发现收录的都是.php的文件,而启用的伪静态地址则收录很少,在更改设置后,想尽快去掉.php的收录,然后想将.php的地址转向.ht ...
- CF 103E Buying Sets 最大权闭合子图,匹配 难度:4
http://codeforces.com/problemset/problem/103/E 这道题首先一看就很像是最大权闭合子图,但是我们可以认为现在有两种点,数字和集合点,我们需要消除数字点的影响 ...
- 微信支付调用JSAPI缺少参数:timeStamp
一般是安卓没问题,苹果会出现这样的问题,弹出下面这样的提示,如果你也是这样,那就恭喜你,现在,你找到解决的方法了 请看红色框框的timeStamp(图片有点小,可以鼠标右键打开图片 查看) 请注意,这 ...
- Spring学习笔记之Bean的一些属性设置
1.beans 里边配置default-init-method="shunge",有这个方法的会执行,没有也不会报错 2.beans 里边配置default-destroy-met ...
- windows操作系统日常使用
快捷键使用: 看实例,学经验,我看行. 1.人体学输入设备被禁用怎么办(鼠标被禁用.键盘被禁用) 有一天脑子抽风,把鼠标给禁用了.以前不常用快捷键,这下必须学学怎么使用快捷键了,现在记下来,防止以后脑 ...
- SVN 首次用TortoiseSVN Checkout 提示Unexpected HTTP status 405
权限错误 首次使用 因为没有 弹出 用户名密码输入框 无法输入 帐户信息. 解决办法: 点击 仓库地址输入栏 右边 的...按钮 此时弹出的输入框浏览器方式访问的.输入用户名和密码,然后在左侧出来的仓 ...
- 更改layout的布局
创建了一个layout,缺省都是LinearLayout,原来都在xml把他改成其他布局,习惯了拖放的方式觉得挺麻烦. 其实vs2013有这个功能,就是在设计界面单机右键可以删除布局,然后拖一个布局进 ...