Ramesh Style | Distributed Systems | Interview Preparation
The source explains CAP using a simple shared-writing / coffee-shop analogy and emphasizes that CAP is a fundamental limitation of distributed systems, not merely an implementation or performance limitation.
1. What is CAP Theorem?
CAP =
C → Consistency
A → Availability
P → Partition ToleranceThe core idea:
A distributed system cannot guarantee Consistency, Availability, and Partition Tolerance simultaneously when a network partition occurs.
The source traces the idea to Eric Brewer's 2000 conjecture, which was later formalized as the CAP theorem.
2. C — Consistency
Simple meaning
Every read should return the most recently written value.
Example:
WRITE
customer:101
coffee = EspressoImmediately after the write:
READ customer:101
↓
EspressoThe source describes consistency as always seeing the most recent value written for a given key.
Easy memory
WRITE → Espresso
READ → EspressoNo stale value.
3. A — Availability
Simple meaning
Every request receives a response, rather than being told to wait indefinitely or come back later.
For example:
Client
|
↓
READ
|
↓
Database
|
↓
Response ✅Availability means the database continues responding to operations.
Important:
Availability does not necessarily mean the response contains the latest value.
That's the important difference between C and A.
4. P — Partition Tolerance
This is the concept people most commonly misunderstand.
Partition ≠ database table partition
Here, network partition means:
Some nodes in a distributed system can no longer communicate with other nodes.
Example:
NETWORK
X
X
X
┌─────────┐ ┌─────────┐
│ Node A │ │ Node B │
│ Node C │ │ Node D │
└─────────┘ └─────────┘
Group 1 Group 2The two groups cannot communicate.
Possible causes include:
Network failure
↓
Broken network connection
OR
Long GC pause
↓
Node temporarily unreachable
OR
Data-center connectivity failure
↓
Nodes cannot communicateThe source describes partitions as parts of a distributed database becoming unable to communicate with other parts.
5. Why is P Almost Mandatory?
This is the most important CAP point.
In a distributed system:
Node A
Node B
Node C
Node Dnodes can independently fail or become disconnected.
Therefore:
Distributed System
↓
Network partition can happen
↓
Must tolerate partition
↓
P becomes unavoidableThe source explicitly explains that distributed systems are anchored in P, because nodes fail independently and parts of the system can become disconnected.
Therefore, during a partition, the practical choice becomes:
P
|
┌────┴────┐
↓ ↓
CP AP
↓ ↓
Consistency Availability6. CP — Consistency + Partition Tolerance
Suppose:
NETWORK PARTITION
X
X
┌───────────┴───────────┐
↓ ↓
Node A Node BNode A doesn't know what Node B has done.
A client asks:
"Give me the latest value."The system could say:
"I cannot confirm the latest value."
↓
Request rejected / unavailableSo:
Consistency ✅
Partition Tolerance ✅
Availability ❌CP
During a partition, prefer correctness/consistency over serving a potentially stale response.
The source's writing-pad example demonstrates exactly this: the author refuses to answer because they cannot know what the other writer has done.
7. AP — Availability + Partition Tolerance
Alternative:
NETWORK PARTITION
X
X
Node A Node BNode A doesn't know what Node B has done.
Client asks:
"What's the current value?"Node A says:
"I don't know what Node B has done,
but here's the latest value I know."So:
Availability ✅
Partition Tolerance ✅
Strong Consistency ❌The response may be stale.
The source's example explicitly describes this choice: return the latest locally known version even though it may not be the globally latest version.
8. CAP Visual
CAP
△
/ \
/ \
/ \
Consistency Availability
\ /
\ /
\ /
Partition
ToleranceThe key statement is:
You cannot guarantee all three during a network partition.
9. Coffee-Shop Example — Very Easy to Remember
Imagine two people are writing the same story.
Each person has a copy:
Person A Person B
| |
Story Copy A Story Copy BWhile they can communicate:
A writes paragraph
↓
Tells B
↓
B updates copyBoth copies stay synchronized.
Network Partition
Now communication breaks:
Person A ❌ Person B
| |
Copy A Copy BBoth continue working independently.
Now the copies can differ:
Copy A → Espresso version
Copy B → Cappuccino versionThat's the partition.
10. Now the CAP Choice
Someone asks:
"What is the latest version?"
Choice 1 — Consistency
Person A says:
"I cannot tell you because I don't know what B has written."
Consistency ✅
Availability ❌
Partition Tolerance ✅
↓
CPChoice 2 — Availability
Person A says:
"I don't know what B has done, but I'll give you my latest version."
Consistency ❌
Availability ✅
Partition Tolerance ✅
↓
APThis is the CAP theorem in action.
11. What About CA?
You may see:
CA = Consistency + AvailabilityBut here's the important interview point:
For a truly distributed system, you cannot simply choose CA and ignore partitions.
Because:
Distributed system
↓
Network partition possible
↓
P matters
↓
During partition → choose C or AThe source explains that if you're not dealing with a distributed system, you can have both consistency and availability without being forced into this partition trade-off.
Example
A single PostgreSQL server:
Application
↓
Single DB ServerThere is no distributed network partition between database nodes because there aren't multiple database nodes participating in the system.
But once you distribute the database:
App
↓
Node A ↔ Node B ↔ Node Cpartition becomes a real concern.
12. CAP vs R + W > N
This connects directly with your previous class notes.
CAP asks:
During a network partition, do we prioritize consistency or availability?
Partition
↓
┌──┴──┐
↓ ↓
C AR + W > N asks:
Do read and write quorums overlap?
N = total replicas
R = read quorum
W = write quorum
R + W > N
↓
Quorum overlapSo:
CAP
↓
System-level trade-off
R + W > N
↓
Quorum-level consistency mechanismThey are related, but not the same concept.
13. CAP + Consistent Hashing + Replication
Now connect everything you've learned:
DISTRIBUTED DATABASE
|
↓
Consistent Hashing
|
↓
Find data nodes
|
↓
Replication
|
┌────────┴────────┐
↓ ↓
Replica 1 Replica 2
↓ ↓
└────────┬────────┘
↓
Network Partition
↓
Replicas disagree
↓
┌──────┴──────┐
↓ ↓
CP AP
↓ ↓
Prefer C Prefer A
reject/ serve
wait possibly
stale data14. Interview Example
Question:
"What happens when two replicas cannot communicate?"
Answer:
Network Partition
↓
Replicas may diverge
↓
System cannot guarantee both
strong consistency AND availability
↓
Choose based on requirements
↓
CP OR APCP:
Don't know latest value
↓
Reject / wait
↓
Consistency maintainedAP:
Don't know latest value
↓
Return locally available value
↓
Availability maintained
↓
Possible stale data15. Common Interview Mistakes
❌ Mistake 1
"CAP means you can only have two of the three at all times."
Too simplistic.
Better:
CAP says that when a network partition occurs, a distributed system cannot guarantee both consistency and availability simultaneously.
❌ Mistake 2
"P means database partitioning/sharding."
Wrong.
CAP Partition
=
Network communication failureNot:
Database partition
=
Sharding❌ Mistake 3
"AP means the database is always inconsistent."
Wrong.
AP systems can often be consistent when there is no partition.
The trade-off becomes relevant during a partition.
❌ Mistake 4
"CA is a valid choice for every distributed database."
Not really.
If your system is genuinely distributed, network partitions are part of the failure model.
🧠Ramesh Memory Trick
Remember:
C = Correct latest value
A = Answer every request
P = Partition/network failure toleranceThen:
NETWORK PARTITION
↓
┌──────┴──────┐
↓ ↓
CP AP
↓ ↓
"Don't know" "I'll answer
→ Reject what I know"
↓ ↓
Consistency Availability🔥 One-line interview answer
"CAP theorem says that in a distributed system, when a network partition occurs, we cannot guarantee both strong consistency and availability at the same time. Since partition tolerance is fundamental to distributed systems, we have to choose whether to favor consistency (CP) or availability (AP)."
No comments:
Post a Comment