4.4.25 · Coding › Databases
Ek distributed database mein, network zaroor tootega (machines ka contact chut jaata hai). Jab aisa hota hai, tumhare saamne ek brutal choice aati hai: ya toh jawab dene se mana karo taaki correct raho, ya toh jawab do aur galat hone ka risk lo. Dono ek saath possible nahi. Yahi ek forced trade-off CAP ka 95% matlab hai.
Definition Teen properties
Consistency (C) — har read most recent successful write return karta hai (ya ek error). Sabhi nodes ek single up-to-date value pe agree karte hain. (Yeh "linearizability" hai, ACID wala C NAHI.)
Availability (A) — non-failing node ko har request pe non-error response milta hai (guarantee nahi ki latest hai).
Partition tolerance (P) — system tab bhi kaam karta rehta hai jab network nodes ke beech messages drop ya delay kare (ek "partition").
Common mistake Steel-man: "CAP kehta hai koi bhi 2 of 3 chuno."
Sahi kyun lagta hai: famous triangle picture suggest karta hai ki teeno symmetric, equal choices hain.
Galat kyun hai: kisi bhi real distributed system mein, partitions hote hain chahe tumhe pasand ho ya na ho — tum unhe "choose" nahi kar sakte ki tolerate na karo, kyunki cosmic rays, cable cuts, aur GC pauses permission nahi maangte.
Fix: P optional nahi hai. Asli statement hai: jab partition aata hai (P), tumhe C aur A ke beech choose karna padta hai. Bina partition ke tum dono C aur A rakh sakte ho. Toh CAP actually hai "CP ya AP, partition ke dauran."
Socho do nodes N 1 aur N 2 hain jo value x = v 0 ki replica rakhte hain.
Ek partition link kaata hai: N 1 aur N 2 baat nahi kar sakte.
Ek client N 1 ko write karta hai x = v 1 . N 1 , N 2 ko bata nahi sakta (link down hai).
Doosra client N 2 se x read karta hai.
Ab N 2 ke paas exactly do options hain:
N 2 kya karta hai...
Result
Property rakhi
Property khoyi
Purana v 0 return karta hai (jawab deta hai)
Galat par responsive
A
C
Link theek hone tak refuse / error karta hai
Sahi ya chup
C
A
proof kyun hai, sirf ek story nahi
N 2 ke paas write ke baare mein koi information nahi hai — partition ne yeh guarantee ki hai. Toh woh literally v 1 return nahi kar sakta (usne kabhi suna hi nahi) aur ek non-error response bhi return kare jo correct ho. Dono demands kam se kam ek node ke liye logically contradictory hain. Isliye partition ke dauran: C aur A dono ek saath hold nahi kar sakte. ■
CP systems (partition ke dauran A sacrifice karte hain): stale data se bachne ke liye refuse/block karte hain. Examples: HBase, etcd/ZooKeeper, MongoDB (majority writes), traditional RDBMS replication.
AP systems (partition ke dauran C sacrifice karte hain): hamesha jawab dete hain, baad mein reconcile karte hain (eventual consistency). Examples: Cassandra, DynamoDB, Riak, CouchDB.
Intuition PACELC — woh upgrade jo sabh bhool jaate hain
CAP sirf partition time ki baat karta hai. PACELC : agar Partition hai toh A ya C choose karo, Else (normal operation mein) L atency ya C onsistency choose karo. Healthy network pe bhi, sabhi replicas ke agree hone ka wait karna latency badhata hai. Toh real-life trade-off roz chalta hai, sirf outages ke dauran nahi.
Worked example Example 1 — Bank balance (CP choose karo)
Do ATMs tumhara balance replicate karte hain. Network split ho jaata hai. Tum ATM-2 pe withdraw karne ki koshish karte ho.
CP kyun? Stale balance return karna overdraw allow kar sakta hai → real paisa gaya. Correctness > uptime.
Step: ATM-2 dekhta hai ki woh cluster se confirm nahi kar sakta → transaction reject karta hai ("baad mein try karo").
Yeh step kyun? Refuse karna (A khoona) hi ek tarika hai jo double-spend guarantee nahi karta (C rakhna).
Worked example Example 2 — Shopping cart (AP choose karo)
Amazon-style cart, data centers ke beech network split.
AP kyun? Momentarily galat cart annoying hai; unavailable "Add to Cart" turant sales kho deta hai.
Step: Dono sides writes accept karti hain; baad mein jab partition theek hota hai toh carts merge ho jaate hain (items ka union).
Yeh step kyun? Availability abhi paisa kamaati hai; conflicts ek commutative merge se resolve hote hain toh koi write nahi jaata.
Worked example Example 3 — Koi partition nahi, phir bhi ek choice (PACELC)
Healthy network, write ek leader ke paas jaati hai jo 5 followers ko replicate karta hai.
Synchronous (C): sabhi 5 ke ack ka wait karo → slow hai par reader kahin bhi latest dekhta hai. L khoota hai.
Asynchronous (L): leader write ke baad ack karo → fast hai par follower read stale ho sakta hai. C khoota hai.
Yeh kyun matter karta hai: C-vs-L tension tab bhi live hai jab kuch tuta nahi hota.
Recall Feynman: ek 12-saal ke bacche ko samjhao
Tum aur tumhara dost do rooms mein ek hi scoreboard rakhte ho, phone line se connected. Achanak phone line toot jaati hai. Tum apne room mein ek point add karte ho. Tumhara dost tumhe sun nahi sakta. Ab koi tumhare dost se score poochta hai. Dost ke paas do choices hain: purana number bolo (jaldi jawab deta hai par ab galat hai), ya bolo "jab tak phone kaam nahi karta main nahi bata sakta" (sahi hai, par helpful nahi). Tum apne dost ko nahi bana sakte ki woh jawab bhi de aur sahi bhi ho — kyunki unhe tumhare naye point ke baare mein pata hi nahi tha! Yahi poora CAP theorem hai.
"P-arty ke dauran, tum ek saath Cool aur Available nahi ho sakte."
P (partition) tumhe C aur A ke beech choose karne par majboor karta hai. Aur: CP = "Correct, Pause" , AP = "Answer, Possibly-stale."
P actually optional kyun nahi hai?
Ek sentence mein, CAP tumhe kya choose karne par majboor karta hai aur kab ?
Kya CAP ka "C", ACID ka "C" hai?
PACELC mein "ELC" kya add karta hai?
What does Consistency mean in CAP? Har read most recent successful write return karta hai (ya ek error) — sabhi nodes ek up-to-date value dekhte hain (linearizability).
What does Availability mean in CAP? Non-failing node ko har request pe non-error response milta hai (guarantee nahi ki latest data hai).
What does Partition tolerance mean? System tab bhi operate karta rehta hai jab network nodes ke beech messages drop/delay kare.
Why is "pick any 2 of 3" misleading? Partitions unavoidably hote hain, toh P optional nahi hai; asli choice C vs A hai partition ke dauran .
What must a partitioned node choose between? Possibly-stale data return karna (A rakho, C kho) ya refuse/error karna (C rakho, A kho).
Give the CAP rule in symbols. Partition ⇒ system CP ya AP hai; koi partition nahi ⇒ dono C aur A possible hain.
Name two CP systems. etcd/ZooKeeper, HBase (MongoDB majority writes bhi).
Name two AP systems. Cassandra, DynamoDB (Riak, CouchDB bhi).
Is CAP's C the same as ACID's C? Nahi. CAP-C = replicas ke across linearizability; ACID-C = transaction mein DB invariants preserve karna.
What does PACELC add over CAP? Bina partition ke bhi (Else), tum Latency vs Consistency trade karte ho.
Why can't a partitioned node return the latest write? Partition ne usse write receive karne se rok diya, toh uske paas naye value ki koi information hi nahi hai.
Distributed Systems
ACID vs BASE
Eventual Consistency
Replication and Quorums
PACELC theorem
Consensus — Paxos & Raft
NoSQL Databases
read returns latest write
contradictory during partition
Cassandra, DynamoDB, Riak