# 可能是CAP理论的最好解释

6 篇文章 0 订阅

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 记忆公司！ - 不用记忆，从不忘记 您是否曾为健忘而感到沮丧？不用担心，只需一通电话。当您需要记住什么时，拨打555-55，告诉我们您想记住的东西。例如，来电让我们知道您老板的电话号，然后忘了这事。当您想要知道已忘了的东西时，拨打同样的号码，我们将告诉您老板的电话号。费用：每次仅需0.1美元。 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

Ø  客户：嗨！能记一下我邻居的生日吗？

Ø  你：当然可以，他生日是什么时候？

Ø  客户：12

Ø  你：(在笔记本上该客户的那一页记下)记好了！想知道他生日时随时打给我们！

Ø  客户：谢谢！

Ø  不客气，您的信用卡将支付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.

1.    You and your wife both get an extension phone

2.    Customers still dial (555)–55-REMEM and need to remember only one number

3.    A pbx will route the a customers call to whoever is free and equally

1.      你和你妻子都有分机号

2.      客户还是拨打原来的号码

3.      交换机将把客户来电发给你们俩空闲的那位。

Two days after you implemented the new system, 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
(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!)

Ø  Jhon：嗨！

Ø  你：感谢致电记忆公司，有什么可以帮您？

Ø  Jhon：能告诉我第一次飞新德里的时间吗？

Ø  你：当然可以，请稍等(你翻阅你的笔记本，在Jhon那页却没发现航班这一项)

Ø  先生，抱歉，我想您没有告诉过我们您去新德里的航班信息。

Ø  Jhon：什么！？我昨天就打给过你们！(挂断了)

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 :)

02-10 1099
02-27 286
06-11 4232
03-08 2441
05-20 1047
11-20 480
04-21 215
12-24 4100
05-24 2万+
05-15 657
07-15 1万+
04-19 66
08-09 166
09-18 3601
08-07 1万+
11-21 954
04-03 452
09-30 1075
05-26 707

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助