4.4.25Databases

CAP theorem — consistency, availability, partition tolerance

1,719 words8 min readdifficulty · medium5 backlinks

WHAT each letter actually means


WHY you can't have all three (derivation from scratch)

Imagine two nodes N1N_1 and N2N_2 holding a replica of value x=v0x = v_0.

  1. A partition cuts the link: N1N_1 and N2N_2 cannot talk.
  2. A client writes x=v1x = v_1 to N1N_1. N1N_1 cannot tell N2N_2 (link down).
  3. Another client reads xx from N2N_2.

Now N2N_2 has exactly two options:

N2N_2 does... Result Property kept Property lost
Returns old v0v_0 (it answers) Wrong but responsive A C
Refuses / errors until link heals Correct or silent C A
Figure — CAP theorem — consistency, availability, partition tolerance

HOW real systems choose

  • CP systems (sacrifice A during partition): refuse/block to avoid stale data. Examples: HBase, etcd/ZooKeeper, MongoDB (majority writes), traditional RDBMS replication.
  • AP systems (sacrifice C during partition): always answer, reconcile later (eventual consistency). Examples: Cassandra, DynamoDB, Riak, CouchDB.

Worked examples


Recall Feynman: explain to a 12-year-old

You and your friend keep the same scoreboard in two rooms connected by a phone line. Suddenly the phone line breaks. You add a point in your room. Your friend can't hear you. Now someone asks your friend the score. Friend has two choices: say the old number (answers fast but it's wrong now), or say "I can't tell you until the phone works" (correct, but unhelpful). You cannot make your friend both answer and be right — because they never heard about your new point! That's the whole CAP theorem.


Active recall

What does Consistency mean in CAP?
Every read returns the most recent successful write (or an error) — all nodes see one up-to-date value (linearizability).
What does Availability mean in CAP?
Every request to a non-failing node returns a non-error response (no guarantee it's the latest data).
What does Partition tolerance mean?
The system keeps operating despite the network dropping/delaying messages between nodes.
Why is "pick any 2 of 3" misleading?
Partitions happen unavoidably, so P isn't optional; the real choice is C vs A during a partition.
What must a partitioned node choose between?
Returning possibly-stale data (keep A, lose C) or refusing/erroring (keep C, lose A).
Give the CAP rule in symbols.
Partition ⇒ system is CP or AP; no partition ⇒ both C and A possible.
Name two CP systems.
etcd/ZooKeeper, HBase (also MongoDB majority writes).
Name two AP systems.
Cassandra, DynamoDB (also Riak, CouchDB).
Is CAP's C the same as ACID's C?
No. CAP-C = linearizability across replicas; ACID-C = preserving DB invariants in a transaction.
What does PACELC add over CAP?
Even with no partition (Else), you trade Latency vs Consistency.
Why can't a partitioned node return the latest write?
The partition prevents it from receiving the write, so it has no information about the new value.

Connections

  • Distributed Systems
  • ACID vs BASE
  • Eventual Consistency
  • Replication and Quorums
  • PACELC theorem
  • Consensus — Paxos & Raft
  • NoSQL Databases

Concept Map

network can break

is not optional

choose

choose

read returns latest write

non-failing node answers

stay correct, may reject

stay up, may be stale

contradictory during partition

Distributed database

Partition tolerance P

Consistency C

Availability A

Forced trade-off

CP systems

AP systems

etcd, HBase, MongoDB

Cassandra, DynamoDB, Riak

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, CAP theorem sirf distributed databases ke liye relevant hai — jahan data ek se zyada machines (nodes) pe replicate hota hai. Idea simple hai: network kabhi na kabhi tootega hi (cable cut, machine slow, message drop). Is tooting ko bolte hain partition. CAP kehta hai ki jab partition ho jaye, tab tum ek hi cheez choose kar sakte ho — ya to Consistency (sabko latest correct data dena) ya Availability (har request ka jawab dena, chahe purana data ho). Dono ek saath possible nahi.

Reason bilkul logical hai: socho do node hain, link toot gaya, ek node pe naya write hua x=v1x=v1. Doosre node ko pata hi nahi chala. Ab agar koi doosre node se padhe, to wo ya to purana v0v0 degi (matlab galat, par available) ya bolegi "abhi nahi bata sakti" (matlab correct, par unavailable). Wo dono kaise karegi? Use to naye value ka pata hi nahi! Isi liye proof ban jaata hai.

Isiliye "any 2 of 3 choose karo" waala statement thoda galat hai. P optional nahi hai — partition tumhare control me nahi. Asli choice hai: partition aane par CP bano (correct raho, zaroorat pade to reject karo — jaise bank balance) ya AP bano (jawab dete raho, baad me sync karo — jaise shopping cart). Bank me galat data se paisa loss hota hai, isliye CP. Cart me thoda stale chalega par website down nahi honi chahiye, isliye AP.

Ek aur important baat: PACELC. CAP sirf partition time ki baat karta hai, lekin reality me network theek hone par bhi tradeoff hota hai — agar tum saare replicas ka wait karoge consistency ke liye, to latency badhegi. Toh roz-marra me bhi Latency vs Consistency ka game chalta rehta hai. Yeh samajh lo to interview aur real design dono crack ho jaayenge.

Go deeper — visual, from zero

Test yourself — Databases

Connections