03. Data & Storage / Lesson 8 of 11

CAP & Consistency

When the network splits, a database must choose between answering and being right.

  • 10 min
  • Intermediate
  • 10 steps
  • Stale readsnone (good)
  • Requests answered100% (good)
  • Write time—
replicationAlice (EU)Replica EUbalance 150Replica USbalance 150Bob (US)
  • Request
  • Response
  • Write
  • Failed request
Step 1/10

Step 1 of 10: Two copies of the same data

Read every step as text
  1. Step 1: Two copies of the same data

    To survive a failure and to be close to users, we keep a copy of the data in two regions: EU and US. Say the account balance is 100 in both.

    Alice changes it to 150 through the EU copy. The change is then sent to the US copy. The round trip between the regions is about 90 ms, so a copy takes about 45 ms one way.

    Every replicated database has the same job: keep the copies equal, even when machines and networks fail.

  2. Step 2: The link between the regions breaks

    A network partition means the machines are alive but cannot talk to each other: a cut cable, a bad router update, a congested link.

    Alice sets the balance to 150 in the EU. The copy cannot reach the US. Now Alice reads 150 and Bob reads 100 from the same account, and neither copy knows it is out of date.

    Partitions are not rare events you can design away. Any network between machines can split, so you must decide what to do when it does.

  3. Step 3: The choice: stay correct or keep answering

    The CAP theorem says that when a partition happens, a system can keep only one of two promises. Consistency (C): every read sees the latest write, or gets an error. Availability (A): every request to a working node gets an answer.

    So it is not "pick any two of three". Partitions (P) will happen, so the real choice is between C and A, and only while the network is split.

    When the link is fine, you can have both. CAP only forces a choice during a partition.

  4. Step 4: CP: the side without a majority says no

    A CP system keeps data correct. Take three replicas (EU, US, Asia). A write or read needs a majority, which is 2 of 3. The US replica is cut off and sees only itself, so it refuses and returns an error.

    EU and Asia still form a majority and keep working. Bob gets errors and nobody ever reads an old value. With users spread evenly, 67% of requests are answered.

    Use CP when a wrong answer is worse than no answer: money, locks, leader election. etcd, ZooKeeper and Spanner behave this way.

  5. Step 5: AP: every side keeps answering

    An AP system keeps working on both sides. Alice writes 150 in the EU, and at the same time Bob writes 120 in the US. Both writes succeed, so nobody sees an error.

    The price comes when the link heals: the copies disagree. The system must merge them. Last write wins is simple but silently drops one update; version vectors or CRDTs can keep both.

    Use AP when being up matters more than being exact: shopping carts, likes, feeds. DNS, Cassandra and DynamoDB lean this way (both databases can be tuned).

  6. Step 6: Strong consistency: wait for the copy

    Strong consistency (also called linearizability) means that once a write is done, every later read, anywhere, sees it. The system behaves as if there were one copy.

    The simple way is to answer Alice only after the US copy confirms. That write takes 2 + 90 = 92 ms instead of 2 ms.

    The farther apart the replicas, the slower every write. Light in fibre cannot be sped up.

  7. Step 7: Eventual consistency: answer now, copy later

    With eventual consistency the EU confirms the write after 2 ms and copies it in the background. If nothing new is written, all copies end up equal.

    For a short time, up to 45 ms here, a read in the US can still return 100. Writes are fast and a partition does not stop them.

    Most large systems read from nearby copies and accept a small delay. The gap is usually milliseconds, not hours.

  8. Step 8: Small promises that make eventual feel fine

    Read your writes: after Alice saves a change, her own reads must show it. Monotonic reads: a user never sees newer data and then older data.

    Cheap ways to give these: send a user to the same replica each time (sticky), or give the client a version number so a replica that is behind must wait or forward the read.

    You do not need strong consistency for everyone. You need it for what each user, or each action, depends on.

  9. Step 9: Even with no partition, speed costs consistency

    CAP says nothing about normal days, but the trade-off is still there. PACELC reads: if there is a Partition choose A or C; Else (normal operation) choose Latency or Consistency.

    A store that waits for far copies pays 92 ms per write. One that does not pays 2 ms and may serve old data for up to 45 ms.

    Dynamo-style stores usually pick availability and latency; Spanner-style stores pick consistency in both cases.

  10. Step 10: Consistency in one picture

    Networks split, so every replicated system answers one question in advance: when the copies cannot talk, do we refuse (CP) or answer with what we have (AP)?

    In normal times the same trade-off shows up as time: 92 ms for strong writes against 2 ms for eventual ones. Pick it per feature: strong for balances and locks, eventual for feeds and counters.