Database.What is CAP theorem?

🧠 What is the CAP Theorem?

The CAP theorem is a rule that says a distributed system (like a NoSQL database spread over many servers) can only guarantee 2 out of 3 things at the same time:

C = Consistency
A = Availability
P = Partition Tolerance

Let’s break each one down in a fun, easy way:

🍰 Imagine: You and Your Friend Have a Cake Shop

You both work in two cities and share the same order notebook (database). You talk by phone or the internet to keep in sync.


1. ✅ Consistency

“Everyone sees the same data at the same time.”

If a customer orders chocolate cake in Moscow, your friend in Yekaterinburg sees that order immediately — no delay, no mistakes.

🟢 Good for banks and payments
❌ Hard when the network is slow or broken

2. ✅ Availability

“The system always responds — even if it returns old or different data.”

If your friend’s phone is dead, you still take cake orders in your city — the shop doesn’t close! But your friend might not know about new orders right away.

🟢 Good for fast services, search engines
❌ You might serve outdated data

3. ✅ Partition Tolerance

“Even if the connection between you and your friend breaks (network partition), the system keeps going.”

You keep working even if you can’t talk to each other — maybe your friend takes new orders, and so do you, but you’re now out of sync.

🟢 Needed in every distributed system (because networks can break)
✅ This is always assumed in CAP

📏 CAP Rule

You can only choose 2 out of 3:

CombinationWhat It MeansExample
CPConsistent and Partition-tolerantSystem may be slow or deny writes
APAvailable and Partition-tolerantYou might see old data
CAConsistent and Available — but ⚠️ no partitionsOnly works in non-distributed setup

✅ Real-World Analogy

🔌 You’re messaging a friend.
Do you want your message to arrive fast (Availability),
to arrive in order and never be lost (Consistency),
or to keep chatting even if Wi-Fi is bad (Partition Tolerance)?

In a real system, you can’t have all 3 at once.


🧠 Why It Matters

Distributed systems like:

  • MongoDB
  • Cassandra
  • DynamoDB
  • Etcd
  • Kafka

All must make trade-offs:

  • Some give up consistency for speed.
  • Others sacrifice availability during failures to stay accurate.

✅ Summary Table

PropertyDescriptionExample Requirement
Consistency (C)All users see same data at the same timeBanking
Availability (A)System always respondsShopping website
Partition (P)Works even if network failsCloud systems
This entry was posted in Без рубрики. Bookmark the permalink.