Post

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 Explained: Simple Terms & Interview Prep

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:

ChoiceWhat HappensYou Sacrifice
Keep ConsistencyNode B refuses to respond until it syncs with A❌ Availability (B is down)
Keep AvailabilityNode B responds with its own (possibly stale) data❌ Consistency (A β‰  B)
Ignore PartitionOnly 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 DatabasesDescription
MongoDB (default config)Strong consistency, may block during partition
HBaseConsistent reads/writes on Hadoop
Redis (cluster)Can prefer consistency
ZookeeperCoordination service β€” consistency is critical
etcdDistributed 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 DatabasesDescription
CassandraAlways available, eventually consistent
DynamoDBAWS managed, highly available
CouchDBAvailability-first document store
RiakDistributed 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 SystemsDescription
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
SystemDuring Partition (PAC)Else (ELC)Full Classification
CassandraPA (Available)EL (Low Latency)PA/EL
DynamoDBPA (Available)EL (Low Latency)PA/EL
MongoDBPC (Consistent)EC (Consistent)PC/EC
HBasePC (Consistent)EC (Consistent)PC/EC
Cosmos DBPA (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

ScenarioChooseWhy
Bank account balanceCPWrong balance = disaster. Better to block than show wrong amount
Flight seat bookingCPCan’t sell the same seat twice
Social media likesAPShowing 999 vs 1000 likes temporarily is fine
Shopping cartAPCart should always work; sync later
DNS (Domain Name System)APMust always resolve; slight staleness is OK
Stock tradingCPTrades must be accurate and ordered
Chat messagesAPMessage delivery > perfect ordering
Inventory countCPCan’t oversell; block if unsure

🎀 Top Interview Questions & Answers

#QuestionBest Answer
1What is CAP theorem?In a distributed system, you can guarantee only 2 of 3: Consistency, Availability, Partition Tolerance
2Can you have all 3?No. During a network partition, you must choose between C and A
3Is CA realistic?Only for single-node systems. In distributed systems, P is mandatory, so real choice is CP vs AP
4What does Cassandra choose?AP β€” always available, eventually consistent
5What does MongoDB choose?CP β€” prefers consistency, may be unavailable during partition
6What is eventual consistency?All nodes will converge to the same value eventually, if no new writes occur
7When would you pick CP?When wrong data is worse than no data (banking, inventory, booking)
8When would you pick AP?When downtime is worse than stale data (social media, DNS, carts)
9What is PACELC?Extension of CAP: during Partition choose A/C, Else choose Latency/Consistency
10Is 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.”

This post is licensed under CC BY 4.0 by the author.