Playground: feel the CAP theoremPlayground: CAP theorem ko mehsoos karo
Two datacenters, one link. Cut the link, then write to one side and read from the other. AP answers with stale data; CP refuses to answer. You cannot have both.Do datacenters, ek link. Link kaato, phir ek taraf write karo aur doosri taraf se read karo. AP stale data deta hai; CP jawab dene se mana kar deta hai. Dono ek saath nahi milte.
The statement, precisely
CAP says: when a network Partition happens (two parts of your system cannot talk), a distributed system must choose between Consistency (every read sees the latest write, or an error) and Availability (every request gets a non-error response, possibly stale). You can have at most two of three, and since partitions are a fact of networks, the real choice during a partition is only between C and A.
The two ATMs (the example that makes it stick)
A bank runs 2 ATMs in different cities showing the same account balance. The link between them breaks. Two options:
- Refuse withdrawals (CP): ATM says "try later". Balance is never wrong, but the ATM is unavailable. Correct over available.
- Keep dispensing (AP): ATM allows withdrawal using the last known balance. Available, but if the user also withdraws in the other city, the account goes negative. Available over correct.
A bank chooses CP for balances. A social network chooses AP for likes: nobody dies if a like count is stale for 30 seconds.
Why P is not optional
Cables get cut, switches fail, cloud regions partition. If you build a distributed system (more than one node), you will face partitions. So "CA" systems only exist as single-node systems, which are not really distributed and are themselves SPOFs.
Real systems on the spectrum
- CP: ZooKeeper, etcd, HBase, MongoDB (default majority reads/writes). Used for coordination, config, leader election: being wrong is worse than being down.
- AP: Cassandra, DynamoDB, CouchDB. Used for carts, sessions, feeds: being down is worse than being briefly stale.
- Postgres/MySQL single primary: consistent when up, unavailable during failover: effectively CP with a manual partition story.
PACELC: the fuller picture
CAP only describes behavior during partitions. PACELC adds the normal case: if Partition, choose Availability or Consistency; Else (normal operation), choose Latency or Consistency. Dynamo-style systems: PA/EL (available under partition, low-latency normally, eventual consistency always). This explains "eventual consistency" as an everyday latency optimization, not only a partition behavior.
Quorums: the tunable middle
Dynamo-style systems answer CAP with knobs: N replicas, W write acknowledgments, R read acknowledgments. If W + R > N, reads see the latest write (strong-ish). W=1, R=1: fastest, weakest. W=N, R=1: every read fast, writes wait for all replicas. In interviews, naming N=3, W=2, R=2 and why (W + R = 4 > 3) is the mark of someone who has actually used these systems.
Common interview traps
- "The system is CA" for anything distributed.
- Treating CAP as a one-time system-wide label instead of a per-feature choice made during partitions.
- Forgetting the latency angle: PACELC.
Interview Q&A
Q: Design a shopping cart: CP or AP?
A: AP. A cart must accept adds even during network issues (availability drives revenue), and a rare stale merge (two devices' carts merged) is acceptable. Checkout/payment, however, is CP: money must be exactly right.
Q: What happens to your design when the network partition heals?
A: AP systems reconcile: last-write-wins, vector clocks or CRDTs merge divergent writes. CP systems had nothing to reconcile: they refused writes on the minority side.
Theorem, sahi shabdon mein
CAP kehta hai: jab network Partition ho jaaye (system ke do hisse baat nahi kar pa rahe), to distributed system ko choose karna padta hai: Consistency (har read ko latest write mile, ya error) ya Availability (har request ko non-error response mile, chahe stale). Teen mein se max do mil sakte hain, aur partitions network ka fact hain, isliye partition ke dauran asli choice sirf C aur A ke beech hai.
Do ATMs (example jo yaad reh jaata hai)
Ek bank ke 2 ATMs alag shehron mein hain, same account balance dikhate hain. Beech ka link toot gaya. Do options:
- Withdrawal mana (CP): ATM bolta hai "baad mein aao." Balance kabhi galat nahi, par ATM unavailable. Correct > available.
- Paise dete raho (AP): ATM purane balance se withdrawal allow karta hai. Available, par user ne doosre sheher se bhi nikala to account negative. Available > correct.
Bank balance ke liye CP chunta hai. Social network likes ke liye AP: like count 30 second stale ho to kisi ki jaan nahi jaati.
P optional kyun nahi hai
Cable kat jaati hai, switch fail hota hai, cloud region partition hota hai. Distributed system (ek se zyada node) banaoge to partition milega hi. Isliye "CA" systems sirf single-node hote hain, jo distributed nahi hote aur khud SPOF hain.
Asli systems spectrum par
- CP: ZooKeeper, etcd, HBase, MongoDB (default majority reads/writes). Coordination, config, leader election ke liye: galat hona down hone se bura hai.
- AP: Cassandra, DynamoDB, CouchDB. Carts, sessions, feeds ke liye: down hona thoda stale hone se bura hai.
- Postgres/MySQL single primary: up ho to consistent, failover ke dauran unavailable: effectively CP jiska partition story manual hai.
PACELC: poori tasveer
CAP sirf partition ke dauran ka behavior batata hai. PACELC normal case bhi jodta hai: Partition ho to Availability ya Consistency; Else (normal time) Latency ya Consistency. Dynamo-style systems: PA/EL (partition mein available, normal time low-latency, hamesha eventually consistent). Isse "eventual consistency" sirf partition behavior nahi, roz ka latency optimization bhi lagta hai.
Quorums: tunable beech ka raasta
Dynamo-style systems CAP ka jawab knobs se dete hain: N replicas, W write acknowledgments, R read acknowledgments. Agar W + R > N, reads latest write dekhte hain (strong-ish). W=1, R=1: sabse fast, sabse weak. W=N, R=1: reads fast, writes sab replicas ka wait karein. Interview mein N=3, W=2, R=2 bolna reason ke saath (W + R = 4 > 3) dikhata hai ki tumne ye systems sach mein use kiye hain.
Common interview traps
- Kisi bhi distributed system ko "CA" bolna.
- CAP ko ek system-wide label samajhna, jabki ye per-feature choice hai jo partition ke dauran hoti hai.
- Latency angle bhoolna: PACELC.
Interview Q&A
Q: Shopping cart design karo: CP ya AP?
A: AP. Cart ko network issues mein bhi adds accept karne chahiye (availability revenue hai), aur kabhi-kabhi stale merge (do devices ke carts merge hue) acceptable hai. Lekin checkout/payment CP hai: paise bilkul sahi hone chahiye.
Q: Partition heal hone par tumhare design mein kya hota hai?
A: AP systems reconcile karte hain: last-write-wins, vector clocks ya CRDTs divergent writes merge karte hain. CP systems ko reconcile kuch karna hi nahi hota: unhone minority side par writes mana kar diye the.