CAP Theorem
Distributed system can guarantee at most two of the three properties: Consistency, Availability, and Partition Tolerance.
The three properties
1️⃣ Consistency (C)
Every read receives the most recent write
All nodes see the same data at the same time
Similar to single-machine correctness
📌 Example: After updating a user’s balance, all future reads return the updated balance.
2️⃣ Availability (A)
Every request receives a response (success or failure)
The system never refuses requests
Does not guarantee the data is the latest
📌 Example: You always get a response, even if it’s stale.
3️⃣ Partition Tolerance (P)
The system continues to operate despite network failures
Messages between nodes can be lost or delayed
📌 Example: Data centers cannot talk to each other, but the system keeps running.
Why you can’t have all three
In real distributed systems:
Network partitions are unavoidable → P is mandatory
During a partition, you must choose:
Consistency (C) → reject some requests
Availability (A) → serve possibly stale data
So, you choose CP or AP.
The valid CAP combinations
✅ CP (Consistency + Partition Tolerance)
System may become unavailable during partitions
Ensures correctness over uptime
📌 Examples:
HBase
MongoDB (in majority mode)
ZooKeeper
🧠 Use when: correctness is critical (banking, inventory, pricing)
✅ AP (Availability + Partition Tolerance)
System always responds
Data may be eventually consistent
📌 Examples:
Cassandra
DynamoDB
Riak
🧠 Use when: uptime > strict accuracy (feeds, metrics, logs)
❌ CA (Consistency + Availability)
Works only when there is no partition
Not realistic for distributed systems
📌 Example:
Single-node databases
Strongly consistent DB in a single data center
One important clarification (often asked in interviews)
❗ CAP is about behavior during partitions
Not about normal operation
Many systems are CA when no partition exists
Choice happens only when partition occurs
CAP vs Real Systems (modern view)
Modern systems blur CAP using:
Quorums
Leader elections
Tunable consistency
Example:
DynamoDB lets you tune R + W > N
MongoDB lets you tune read/write concerns

