CAP Theorem Explained: Simple Terms & Interview Prep
Understand the CAP theorem (Consistency, Availability, Partition Tolerance) in layman's terms. Master distributed database tradeoffs for your next data engineering interview.
CAP Theorem
The CAP Theorem is a fundamental concept in distributed systems and a must-know topic for Data Engineering, Backend, and System Design interviews.
π§ What is CAP Theorem? (The Elevator Pitch)
βIn a distributed system, you can only guarantee 2 out of 3 properties at any given time: Consistency, Availability, and Partition Tolerance.β
Proposed by Eric Brewer in 2000 (hence also called Brewerβs Theorem).
πΊ The CAP Triangle
1
2
3
4
5
6
7
8
9
10
11
C (Consistency)
/\
/ \
/ \
CP / \ CA
/ β \
/ Can't have \
/ all three! \
/________*_______\
P (Partition A (Availability)
Tolerance) AP
π The 3 Properties Explained
πΉ C β Consistency
βEvery read receives the most recent write or an error.β
1
2
3
4
5
6
7
Client writes X = 5
β
βββββββββββ΄ββββββββββ
β Node A: X = 5 βββsyncβββΆβ Node B: X = 5 β
βββββββββββββββββββββ βββββββββββββββββββββ
β
Client reads from ANY node β always gets X = 5
- All nodes see the same data at the same time
- Like a single database experience β no stale reads
- Memory aid: βEveryone agrees on the answer.β
πΉ A β Availability
βEvery request receives a response (not an error), even if itβs not the most recent data.β
1
2
3
4
5
6
7
8
9
10
11
12
Client sends request
β
ββββββ΄βββββ
β Node A β ββββ β
Responds (maybe stale data)
β (UP) β
βββββββββββ
β Node B β ββββ β
Also responds
β (UP) β
βββββββββββ
β NEVER returns: "Service Unavailable" or timeout
- System is always responsive β no downtime
- May return slightly outdated data, but always returns something
- Memory aid: βAlways answers the phone, even if the info is old.β
πΉ P β Partition Tolerance
βThe system continues to operate even if communication between nodes breaks.β
1
2
3
4
5
6
βββββββββββ βοΈ NETWORK βββββββββββ
β Node A β ββββββ PARTITION ββββββββ β Node B β
β X = 5 β (can't talk!) β X = 3 β
βββββββββββ βββββββββββ
System MUST still function despite this break!
- Network failures will happen in distributed systems (itβs not βifβ but βwhenβ)
- The system doesnβt crash β it handles the split
- Memory aid: βSurvives a broken phone line between nodes.β
β‘ Why Canβt You Have All 3?
Hereβs the intuitive proof β imagine a network partition happens:
1
2
3
4
βββββββββββ βοΈ BROKEN βββββββββββ
β Node A β βββββββββββββββββββββββ β Node B β
β β (can't sync!) β β
βββββββββββ βββββββββββ
Now you have a choice:
| Choice | What Happens | You Sacrifice |
|---|---|---|
| Keep Consistency | Node B refuses to respond until it syncs with A | β Availability (B is down) |
| Keep Availability | Node B responds with its own (possibly stale) data | β Consistency (A β B) |
| Ignore Partition | Only works if partition never happens | β Partition Tolerance (unrealistic) |
π‘ βYou canβt have a system that always responds (A), always returns the latest data (C), AND handles network splits (P) β pick two.β
π― The 3 Combinations
Since Partition Tolerance is mandatory in real distributed systems (networks WILL fail), the real choice is:
CP or AP? (CA is mostly theoretical)
πΉ CP β Consistency + Partition Tolerance
βIβd rather give no answer than a wrong answer.β
1
2
3
4
5
6
7
8
βββββββββββ βοΈ PARTITION βββββββββββ
β Node A β ββββββββββββββββ β Node B β
β X = 5 β β X = 3 β
ββββββ¬βββββ ββββββ¬βββββ
β β
Client reads β BLOCKED!
from A β X = 5 "Sorry, can't guarantee
consistency. Try later."
- β Data is always correct
- β System may be unavailable during partitions
- Use cases: Financial systems, banking, inventory management
| CP Databases | Description |
|---|---|
| MongoDB (default config) | Strong consistency, may block during partition |
| HBase | Consistent reads/writes on Hadoop |
| Redis (cluster) | Can prefer consistency |
| Zookeeper | Coordination service β consistency is critical |
| etcd | Distributed key-value store (used by Kubernetes) |
- Interview tip: βChoose CP when wrong data is worse than no data β like bank balances or seat booking.β
πΉ AP β Availability + Partition Tolerance
βIβd rather give a possibly stale answer than no answer.β
1
2
3
4
5
6
7
8
βββββββββββ βοΈ PARTITION βββββββββββ
β Node A β ββββββββββββββββ β Node B β
β X = 5 β β X = 3 β
ββββββ¬βββββ ββββββ¬βββββ
β β
Client reads Client reads
from A β X = 5 β
from B β X = 3 β
(stale but available!)
- β System is always available
- β Data may be inconsistent (temporarily)
- Relies on eventual consistency β nodes will sync later
- Use cases: Social media feeds, shopping carts, DNS, CDNs
| AP Databases | Description |
|---|---|
| Cassandra | Always available, eventually consistent |
| DynamoDB | AWS managed, highly available |
| CouchDB | Availability-first document store |
| Riak | Distributed key-value, AP by design |
| Cosmos DB (configurable) | Can be tuned for AP |
- Interview tip: βChoose AP when downtime is worse than stale data β like social media likes or product catalog.β
πΉ CA β Consistency + Availability (β οΈ Theoretical)
βI want correct data AND 100% uptime β but Iβm pretending network failures donβt exist.β
1
2
3
4
5
6
βββββββββββ β
PERFECT βββββββββββ
β Node A β ββ CONNECTION ββ β Node B β
β X = 5 β (no partition) β X = 5 β
βββββββββββ βββββββββββ
β
Consistent + Available... but only when network is perfect
- β οΈ Not realistic in distributed systems β partitions WILL happen
- Only possible in single-node systems (traditional RDBMS)
| CA Systems | Description |
|---|---|
| PostgreSQL (single node) | Consistent + Available, no partition handling |
| MySQL (single node) | Same β no distribution = no partition problem |
| Oracle (single node) | Traditional RDBMS |
- Interview tip: βCA only works when thereβs no distribution. The moment you go multi-node, you MUST handle partitions (P), so CA breaks.β
πΊοΈ The CAP Database Map
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
CONSISTENCY
β
ββββββββββββββΌβββββββββββββ
β β β
β MongoDB β β
β HBase β MySQL* β
β Redis β PostgreSQL*β
β Zookeeperβ Oracle* β
β etcd β β
β β β
β (CP) β (CA*) β
β β *single β
β β node β
PARTITIONββββββββΌββββββββββββββ AVAILABILITY
TOLERANCE β
β β
β Cassandra β
β DynamoDB β
β CouchDB β
β Riak β
β β
β (AP) β
β β
ββββββββββββββ
π Eventual Consistency β The AP Companion
Since AP systems sacrifice consistency, they rely on eventual consistency:
βIf no new updates are made, eventually all nodes will have the same data.β
1
2
3
4
5
6
7
Time T0 (Write): Time T1 (Syncing): Time T2 (Consistent):
Node A: X = 5 Node A: X = 5 Node A: X = 5
Node B: X = 3 (stale) Node B: X = 5 (syncing) Node B: X = 5 β
Node C: X = 3 (stale) Node C: X = 3 (waiting) Node C: X = 5 β
β Inconsistent β³ Converging β
Eventually Consistent
Consistency Spectrum
1
2
3
4
5
6
7
8
9
Strong ββββββββββββββββββββββββββββββββββββββββββββββββΊ Eventual
Consistency Consistency
β Linearizable β Sequential β Causal β Read-your- β Eventual β
β β β β writes β β
β (Strictest)β β β β(Loosest) β
β β β β β β
β Zookeeper β etcd β β DynamoDB βCassandra β
β Spanner β β β β CouchDB β
π PACELC Theorem β The Extended CAP
βCAP only talks about what happens during a partition. But what about when things are normal?β
PACELC says:
1
2
3
4
5
6
7
8
9
10
11
12
13
IF (Partition happens):
Choose between Availability (A) and Consistency (C)
ELSE (normal operation):
Choose between Latency (L) and Consistency (C)
P A C / E L C
β β β β β β
β β β β β βββ Consistency
β β β β βββββ Latency
β β β βββββββ Else (no partition)
β β βββββββββββ Consistency
β βββββββββββββ Availability
βββββββββββββββ Partition
| System | During Partition (PAC) | Else (ELC) | Full Classification |
|---|---|---|---|
| Cassandra | PA (Available) | EL (Low Latency) | PA/EL |
| DynamoDB | PA (Available) | EL (Low Latency) | PA/EL |
| MongoDB | PC (Consistent) | EC (Consistent) | PC/EC |
| HBase | PC (Consistent) | EC (Consistent) | PC/EC |
| Cosmos DB | PA (configurable) | EL (configurable) | PA/EL (tunable) |
- Interview tip: βPACELC extends CAP by adding the latency vs consistency tradeoff during normal operations. Itβs a more complete picture.β
π― Real-World Scenario Decisions
| Scenario | Choose | Why |
|---|---|---|
| Bank account balance | CP | Wrong balance = disaster. Better to block than show wrong amount |
| Flight seat booking | CP | Canβt sell the same seat twice |
| Social media likes | AP | Showing 999 vs 1000 likes temporarily is fine |
| Shopping cart | AP | Cart should always work; sync later |
| DNS (Domain Name System) | AP | Must always resolve; slight staleness is OK |
| Stock trading | CP | Trades must be accurate and ordered |
| Chat messages | AP | Message delivery > perfect ordering |
| Inventory count | CP | Canβt oversell; block if unsure |
π€ Top Interview Questions & Answers
| # | Question | Best Answer |
|---|---|---|
| 1 | What is CAP theorem? | In a distributed system, you can guarantee only 2 of 3: Consistency, Availability, Partition Tolerance |
| 2 | Can you have all 3? | No. During a network partition, you must choose between C and A |
| 3 | Is CA realistic? | Only for single-node systems. In distributed systems, P is mandatory, so real choice is CP vs AP |
| 4 | What does Cassandra choose? | AP β always available, eventually consistent |
| 5 | What does MongoDB choose? | CP β prefers consistency, may be unavailable during partition |
| 6 | What is eventual consistency? | All nodes will converge to the same value eventually, if no new writes occur |
| 7 | When would you pick CP? | When wrong data is worse than no data (banking, inventory, booking) |
| 8 | When would you pick AP? | When downtime is worse than stale data (social media, DNS, carts) |
| 9 | What is PACELC? | Extension of CAP: during Partition choose A/C, Else choose Latency/Consistency |
| 10 | Is CAP a strict rule? | Itβs a spectrum β systems can be tuned. E.g., Cosmos DB lets you configure consistency levels |
π Quick Revision Cheat Sheet
1
2
3
4
5
6
7
8
9
10
11
12
13
14
πΊ CAP = Consistency + Availability + Partition Tolerance (pick 2)
π P is mandatory in distributed systems β Real choice: CP vs AP
CP β "Correct or nothing" β Banks, Bookings β MongoDB, HBase, Zookeeper
AP β "Always respond" β Social, Carts, DNS β Cassandra, DynamoDB, CouchDB
CA β "Only single-node" β Traditional RDBMS β PostgreSQL*, MySQL* (*single node)
π Eventual Consistency = AP systems sync data over time
π PACELC = CAP + (Else: Latency vs Consistency)
Memory Aid:
C = "Everyone sees the SAME thing"
A = "Everyone gets a RESPONSE"
P = "Survives a NETWORK BREAK"
π‘ Pro Interview Tip: When asked about CAP, donβt just define it β give a real-world analogy:
βImagine two bank branches (Node A & B) with the phone line cut (partition). A customer deposits βΉ10,000 at Branch A. Now Branch B has two options:
- CP: Block all queries about that account until the phone line is restored (consistent but unavailable)
- AP: Show the old balance and sync later (available but inconsistent)
You canβt do both β thatβs CAP theorem in action.β
