What is CAP Theorem?
Learn CAP theorem with clear examples: why distributed databases must trade off consistency and availability during network partitions.
Expected Interview Answer
CAP theorem states that a distributed data store can only guarantee two of three properties at once โ Consistency, Availability, and Partition tolerance โ during a network partition.
Consistency means every read receives the most recent write or an error. Availability means every request receives a non-error response, even if it is not the latest data. Partition tolerance means the system keeps operating despite network failures splitting nodes apart. Since network partitions are unavoidable in real distributed systems, engineers must choose between prioritizing consistency (CP systems like HBase) or availability (AP systems like Cassandra) when a partition occurs.
- Guides distributed database selection
- Clarifies trade-off decisions
- Explains eventual consistency
- Frames outage behavior expectations
AI Mentor Explanation
During a stadium power outage, the scoreboard operators at each end of the ground lose contact with each other via radio. One operator can keep showing the last known score to fans (available but possibly stale), or freeze the display until radio contact is restored (consistent but unavailable). CAP theorem says you cannot have both perfectly synced live scores and an always-updating display when the link between operators is down โ you must pick which failure mode you tolerate.
Step-by-Step Explanation
Step 1
Identify the three properties
Consistency, Availability, and Partition tolerance are the three guarantees under consideration.
Step 2
Accept partitions happen
Real networks fail, so partition tolerance is effectively mandatory in distributed systems.
Step 3
Choose CP or AP
During a partition, decide whether to reject requests to stay consistent (CP) or serve possibly stale data to stay available (AP).
Step 4
Map to real systems
Classify databases like Cassandra (AP), MongoDB (tunable), and HBase (CP) against this trade-off.
What Interviewer Expects
- Correct definitions of C, A, and P
- Recognition that partition tolerance is non-negotiable in practice
- Real database examples classified as CP or AP
- Awareness of eventual consistency as a middle ground
Common Mistakes
- Claiming you can pick all three properties simultaneously
- Confusing CAP consistency with ACID consistency
- Forgetting that the trade-off only applies during an actual partition
- Failing to name a real CP or AP database example
Best Answer (HR Friendly)
โCAP theorem says a distributed database can only guarantee two out of three things at once: consistency, availability, and partition tolerance. Since network failures are unavoidable, in practice teams must choose between staying consistent or staying available whenever a partition happens, and that choice shapes which database technology fits their use case.โ
Code Example
-- CP-style read: require quorum agreement before returning data
SELECT account_id, balance
FROM accounts
WHERE account_id = 1001
FOR UPDATE; -- blocks until the row lock is available on all replicas
-- AP-style read: return local replica data immediately, even if stale
SELECT account_id, balance
FROM accounts_read_replica
WHERE account_id = 1001;Follow-up Questions
- What is the difference between CAP theorem and PACELC?
- Is MongoDB a CP or AP system?
- How does eventual consistency relate to CAP theorem?
- Can a single-node database ever violate CAP theorem?
MCQ Practice
1. According to CAP theorem, during a network partition a distributed system must choose between which two properties?
Partition tolerance is assumed necessary, so the real trade-off during a partition is between consistency and availability.
2. Which database is typically classified as AP (Availability + Partition tolerance)?
Cassandra favors availability and partition tolerance, using eventual consistency across replicas.
3. What does the "P" in CAP theorem stand for?
P stands for Partition tolerance, the ability of a system to continue operating despite network splits.
Flash Cards
What does CAP stand for? โ Consistency, Availability, Partition tolerance.
Why is partition tolerance usually non-negotiable? โ Because real networks fail, so a distributed system must handle partitions to function at all.
What is a CP database example? โ HBase, which favors consistency over availability during a partition.
What is an AP database example? โ Cassandra, which favors availability over strict consistency during a partition.