🔺 CAP Theorem

CAP Theorem

When you’re working with databases or preparing for a system design interview, one term keeps popping up — the CAP Theorem.

It’s short, simple, and yet one of the most powerful ideas in distributed systems. Let’s break it down in a way that’s engaging, memorable, and practical.

🌐 What is CAP Theorem?

The CAP Theorem (also known as Brewer’s Theorem) states that:

In a distributed system, you can only guarantee two out of three properties:
Consistency, Availability, and Partition Tolerance.

Sounds theoretical? Don’t worry — by the end of this article, you’ll see it everywhere.

What is CAP Theorem?
What is CAP Theorem?

🧩 The Three Pillars of CAP

1. Consistency ©

Every read receives the most recent write.

  • Example: If you transfer money from your bank account, both mobile and ATM should instantly show the updated balance.

2. Availability (A)

Every request receives a response, even if it’s not the latest data.

  • Example: On social media, you’ll see some posts even if one server is temporarily down.

3. Partition Tolerance (P)

The system continues to operate even when network failures split servers into disconnected groups.

  • Example: In a global app, if the U.S. and Europe servers can’t talk, each region should still serve users somehow.

🔺 The CAP Triangle

Think of CAP as a triangle:

  • You can pick Consistency + Availability, but not Partition Tolerance.
  • Or Consistency + Partition Tolerance, but not Availability.
  • Or Availability + Partition Tolerance, but not full Consistency.

In real life, Partition Tolerance is unavoidable because networks always fail.
So the real choice is usually:
👉 CP vs AP.

The CAP Triangle
The CAP Triangle

⚖️ Real-World Examples

  • CP Systems (Consistency + Partition Tolerance)
  • Examples: MongoDB (in strong consistency mode), HBase
  • Prioritize correctness over availability.
  • Use case: Banking systems — better to reject a transaction than show wrong balance.
  • AP Systems (Availability + Partition Tolerance)
  • Examples: Cassandra, DynamoDB
  • Prioritize availability, even if some data is stale.
  • Use case: Social media feeds — it’s okay to see slightly old posts.
  • CA Systems (Consistency + Availability)
  • Only possible when there’s no partition — which is unrealistic in large distributed systems.

📝 A Simple Analogy

Imagine you and your friend are editing a shared Google Doc.

  • If the internet is perfect, both of you always see updates immediately.
  • If the network breaks (a partition happens):
  • Stop editing until the network is fixed → Consistency over Availability (CP).
  • Keep editing offline, sync later (even if conflicts occur) → Availability over Consistency (AP).

This is CAP Theorem in action.

🚀 Why CAP Matters Today

Modern databases and distributed systems (like Kafka, Cassandra, DynamoDB, MongoDB) are designed with CAP trade-offs in mind.

When architects design systems, they don’t ask “Can we have all three?” —
They ask: “Which two are most critical for our business?”

  • Banks → CP (correctness first).
  • Social networks → AP (availability first).
  • Small apps without distribution → may feel like CA, but only until they scale.

🎯 Final Takeaway

The CAP Theorem isn’t just theory — it’s a practical guide for system design.

👉 Always assume Partition Tolerance (because networks fail).
👉 Then decide:

  • Do you want Consistency (no stale data)?
  • Or Availability (always responsive)?

That trade-off defines your database and system architecture choices.

Top 5 Interview Questions on CAP Theorem

🔹 1. What is the CAP Theorem?

The CAP Theorem (Brewer’s Theorem) states that in any distributed system, you can only guarantee two out of three properties:

  • Consistency ©: Every read gets the latest write.
  • Availability (A): Every request gets a response, even if some nodes are down.
  • Partition Tolerance (P): The system continues to operate despite network failures that split the system into disconnected parts.

👉 In practice, Partition Tolerance is unavoidable, so most systems trade-off Consistency vs Availability.

🔹 2. Why is Partition Tolerance considered mandatory in distributed systems?

Because in real-world networks, partitions are inevitable — packets can be dropped, latency may spike, or data centers may fail.
If a system cannot tolerate partitions, it risks becoming completely unavailable during a failure.

👉 Therefore, most distributed databases assume Partition Tolerance and then decide between Consistency or Availability.

🔹 3. Give examples of CP and AP systems.

  • CP (Consistency + Partition Tolerance): Systems prioritize correctness over availability.
  • Examples: MongoDB (strong consistency mode), HBase, Zookeeper.
  • Use Case: Banking — better to reject a transaction than show an incorrect balance.
  • AP (Availability + Partition Tolerance): Systems prioritize availability, even if data may be stale.
  • Examples: Cassandra, DynamoDB, Couchbase.
  • Use Case: Social Media Feeds — better to show slightly old posts than nothing.

🔹 4. Can a system be both Consistent and Available (CA)?

Answer:
Yes, but only if there are no partitions (i.e., a perfectly reliable network).
In real-world large-scale distributed systems, partitions are unavoidable, so true CA systems don’t exist at scale.

👉 Small single-node or non-distributed databases (like traditional RDBMS on one server) may feel CA, but once distributed, you must tolerate partitions.

🔹 5. How would you decide between Consistency and Availability for a system?

It depends on the business requirements:

  • If correctness is critical (e.g., banking, stock trading), choose CP.
  • If availability is more important (e.g., social media, e-commerce catalog), choose AP.
  • The trade-off is about user experience vs correctness.

👉 Good interview answer: “Partition tolerance is a given; the choice between consistency and availability depends on whether the business can afford stale data or downtime.”

💡 Pro Tip: If you’re preparing for interviews, always explain CAP Theorem with definitions + examples + real-world trade-offs. It leaves a strong impression.


Read other awesome articles in Medium.com or in akcoding’s posts.

OR

Join us on YouTube Channel

OR Scan the QR Code to Directly open the Channel 👉

AK Coding YouTube Channel

Share with