4.4.25 · HinglishDatabases

CAP theorem — consistency, availability, partition tolerance

1,521 words7 min readRead in English

4.4.25 · Coding › Databases


WHAT — har letter ka asli matlab


WHY — teeno ek saath possible kyun nahi hain (scratch se derivation)

Socho do nodes aur hain jo value ki replica rakhte hain.

  1. Ek partition link kaata hai: aur baat nahi kar sakte.
  2. Ek client ko write karta hai . , ko bata nahi sakta (link down hai).
  3. Doosra client se read karta hai.

Ab ke paas exactly do options hain:

kya karta hai... Result Property rakhi Property khoyi
Purana return karta hai (jawab deta hai) Galat par responsive A C
Link theek hone tak refuse / error karta hai Sahi ya chup C A
Figure — CAP theorem — consistency, availability, partition tolerance

HOW — real systems kaise choose karte hain

  • 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.

Worked examples


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.


Active recall

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.

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