CAP Theorem
The fundamental trade-off in distributed systems: you can only pick two of three.
1The ATM Analogy
Option 1 (Consistency): Both ATMs refuse the transaction until network recovers. Safe but unavailable.
Option 2 (Availability): Both allow withdrawal. You now have -$20 balance. Available but inconsistent.
CAP Theorem: In a distributed system, during a network partition, you must choose between Consistency and Availability. You cannot have both.
2The Three Properties
Consistency
Every read receives the most recent write or an error. All nodes see the same data at the same time.
Example: After updating your profile, everyone sees the new version immediately.
Availability
Every request receives a response (not an error). The system is always operational.
Example: The website always responds, even if showing slightly stale data.
Partition Tolerance
System continues operating despite network partitions between nodes.
Example: If US and EU data centers can't communicate, both keep working.
3The Trade-off Reality
Key insight: Network partitions WILL happen in distributed systems. So you're really choosing between CP (consistency + partition tolerance) or AP (availability + partition tolerance).
CP Systems
Sacrifice availability during partition. Returns error rather than stale data.
AP Systems
Sacrifice consistency during partition. Returns stale data rather than error.
4Real-World Dry Run
Scenario: E-commerce Inventory During Network Partition
If CP (Consistent)
One or both regions return "Service Unavailable" until partition heals. No overselling, but lost sales.
If AP (Available)
Both orders succeed. After partition heals: 7 + 5 = 12 sold, but only 10 in stock. Oversold by 2.
5How to Choose
Wrong balance is worse than temporary unavailability. Money must be accurate.
Showing slightly stale posts is fine. Users expect availability over perfect freshness.
Cart can merge later. Losing items in cart is worse than brief inconsistency.
Overselling is very costly. Better to show unavailable than overbook.
6Key Takeaways
?Quiz
1. Network partition occurs. Your bank app shows 'Service Unavailable'. This is:
2. Twitter shows you a tweet from 2 seconds ago instead of the latest. This is: