⚖️ CAP theorem: cheat sheet
🎨 The analogyTwo shop branches whose phone line goes down: close until it's back, or stay open and risk different prices.
🎓 The regular explanationWhen a network split cuts replicas off from each other, a distributed database must choose between staying consistent (refuse some requests) and staying available (answer, possibly with stale data).
- A shop has two branches with a phone line between them. Both show the same price list, with cake at £4.
- The manager changes the price at the north branch. The north branch phones the south branch straight away.
- A shopper asks the south branch and gets £5. While the line works, you can have it all, both branches open and both agreeing.
- A storm cuts the phone line. Both branches are fine, but they can't reach each other.
- The manager wants to raise the price to £6. The north branch can't tell the south, so it refuses rather than let the two disagree.
- The south branch, cut off and unsure whether it's out of date, puts up a "back soon" sign rather than risk quoting a wrong price.
- When the line is repaired, the south branch catches up and opens again. Nobody ever saw two different prices.
- Same storm, different shop. This one would rather stay open, so the north branch takes the new price, £6, anyway.
- A shopper asks the south branch and is told £5. The shop stayed open, but the two branches now disagree.
- When the line comes back, the branches compare notes and agree on the latest change. Eventually, everyone sees £6.
- The phone line is down and both branches stay open. The cake costs £5 in both.
- The north branch is told £6 at 10:00. The south branch is told £7 at 10:01. Neither can hear the other.
- The line comes back. The branches swap what they were told and find they disagree. Someone must pick a winner.
- The shop's rule is that the latest time wins, so £7 stays and the £6 change is thrown away. Shopper A is never told. When Shopper A asks again, they hear £7 with no warning.
🧩 Key questions
- The phone line between the two branches is cut. What choice does each branch face?
- Close to changes until the line is back, or stay open and risk different prices. A branch can't know what it hasn't been told, so there's no third option.
- While the line works, what can the two branches do?
- Both stay open and agree. The hard choice only appears when the line is down.
- A branch stayed open during the storm. What must it do when the line comes back?
- Compare notes with the other branch and agree on the latest price. Shoppers may have seen different prices for a while, but eventually everyone sees the same one.
- Both branches stayed open. The north was told £6, the south was told £7, and the rule is that the latest time wins. What happens when the line returns?
- One change wins by its time and the other is thrown away without warning. Quick rules are simple but can discard a change nobody notices. Keeping both notes avoids that.