Lesson 9 � Intermediate

CAP Theorem & Trade-offs

CAP Theorem distributed systems ka fundamental law hai. Ye batata hai ki jab network partition hoti hai toh tum sirf 2 mein se 3 guarantees choose kar sakte ho.

? 20 min✓ Intermediate✓ Distributed systems basics

CAP Theorem kya hai?

WHAT

CAP theorem kehta hai ki distributed system mein teen guarantees hain � Consistency, Availability, Partition Tolerance. Network failure (partition) hone pe tum sirf 2 choose kar sakte ho. Teesra sacrifice karna padega.

WHEN

Har distributed system mein CAP applicable hai. Database choose karte waqt, architecture design karte waqt � ye theorem hamesha relevant hai.

WHERE

CP databases: MongoDB, HBase, Redis Cluster. AP databases: Cassandra, DynamoDB, CouchDB. CA databases: Single-node MySQL (partition tolerance nahi).

Teen Guarantees

# C - Consistency
✓ Har read ke baad latest write mile
✓ Sab nodes same data dikhaye ek hi time pe
✓ Example: Bank balance updated hai toh sab jagah updated dikhe

# A - Availability
✓ Har request ko response mile (success ya error)
✓ System down nahi hona chahiye
✓ Example: Website always accessible, chahe slow ho

# P - Partition Tolerance
✓ Network partition (nodes ke beech communication failure) 
 hone ke baad bhi system kaam kare
✓ Distributed system mein ye MUST hai
✓ Example: Data center disaster, network cable cut

# THE THEOREM:
Jab network partition ho (P), toh:
 Choose C ✓ Availability sacrifice (requests fail)
 Choose A ✓ Consistency sacrifice (stale data)

# Network partition kab hoti hai?
- Server crash
- Network cable cut
- High latency
- Data center failure
- (Ye REAL mein hota hai, rare nahi!)
Mental Model: CAP jaise traffic light. Teen signals hain � Red, Yellow, Green. Ek time pe sirf ek ho sakta hai. Similarly system mein teen guarantees hain � ek time pe sirf 2 de sakte ho jab partition ho.

CP vs AP vs CA

# 1. CP System (Consistency + Partition Tolerance)
✓ Partition hone pe availability sacrifice
✓ Nodes disconnect hue toh requests reject/fail
✓ Jab tak consistent data nahi milta, response mat do

# Example: MongoDB (with majority read/write concern)
✓ Primary node down ✓ cluster rejects writes
✓ Ensures data consistency over availability

# 2. AP System (Availability + Partition Tolerance)
✓ Partition hone pe consistency sacrifice
✓ Stale data deke bhi response do
✓ Eventually consistent ho jaayega

# Example: Cassandra, DynamoDB
✓ Network partition ✓ nodes independently serve reads
✓ Data eventually sync hoga
✓ Users ko stale data dikh sakta hai temporarily

# 3. CA System (Consistency + Availability)
✓ Partition tolerance nahi
✓ Single node database
✓ Network failure pe system down

# Example: Standalone MySQL, PostgreSQL
✓ Single server, no network = no partition
✓ But not truly distributed!

PACELC Theorem (Extended CAP)

# PACELC: Partition ✓ A or C; Else ✓ Latency or Consistency

# CAP sirf partition ke baare mein baat karta hai
# PACELC normal operation ke baare mein bhi baat karta hai

# When NO partition (normal operation):
 Choose L (Low Latency) ✓ C sacrifice
 Choose C (Consistency) ✓ L sacrifice

# PACELC Examples:
| Database | PACELC | Reason |
|--------------|----------|----------------------------|
| MongoDB | PA/EL | Partition: A, Normal: L |
| Cassandra | PA/EL | Partition: A, Normal: L |
| DynamoDB | PA/EL | Partition: A, Normal: L |
| PostgreSQL | PC/EC | Partition: C, Normal: C |
| Redis Cluster| PC/EL | Partition: C, Normal: L |

# Most distributed systems: PA/EL
✓ Partition tolerance + low latency + eventual consistency

Real-world Trade-offs

# Example 1: Banking System
✓ CP choose karo (Consistency + Partition Tolerance)
✓ Paisa galat dikha toh disaster
✓ Availability sacrifice karo but data galat mat dikhao

# Example 2: Social Media Feed
✓ AP choose karo (Availability + Partition Tolerance)
✓ Thoda purana data chalega, but site down mat ho
✓ Users ko stale posts dikhein but site accessible rahe

# Example 3: E-commerce Inventory
✓ CP for critical items (limited stock)
✓ AP for non-critical (product catalog)

# Hybrid Approach:
✓ Different services, different trade-offs
✓ Payment: CP (consistency critical)
✓ Product listing: AP (availability critical)
✓ User profile: AP (eventual consistency ok)

Exercise

Question: CAP theorem ke 3 guarantees kya hain? (2-3 words mein se koi ek batao)

Question: CAP theorem ke hisaab se distributed system mein kitni guarantees ek saath choose kar sakte ho? (1 word)

Common mistakes

Lesson complete?

CAP Theorem samajh aa gaya✓ Ab HLD problems solve karte hain � URL Shortener design karna seekhte hain.