Skip to content
tezvyn:

CAP Theorem: Pick Two of Three Guarantees

Source: Wikipedia: CAP theoremMediumHow cards are made

CAP Theorem: Pick Two of Three Guarantees

The CAP Theorem states a distributed system can only have two of three guarantees: Consistency, Availability, or Partition Tolerance. When the network fails (a partition), you must choose: stop responding to ensure data is consistent (CP) or keep responding…

Why it exists

Distributed systems must function over unreliable networks where messages can be lost or delayed. The CAP theorem was formulated to explain the fundamental trade-offs a system must make when its nodes can't communicate. It provides a clear vocabulary for reasoning about system design choices under network failure conditions.

The mental model

Imagine a shared bank account ledger accessible by two tellers in different cities. If the phone line between them goes down (a network partition), they have a choice. To guarantee consistency, they could stop all transactions until the line is restored, ensuring the balance is never wrong. This is a CP system (Consistency over Availability). Or, to stay available, they could allow transactions based on the last known balance, risking an overdraft if both process a withdrawal. This is an AP system (Availability over Consistency). They cannot do both simultaneously.

How it works

The theorem defines three guarantees. Consistency means every read receives the most recent write or an error. Availability means every request gets a non-error response, though it may not be the most recent data. Partition Tolerance means the system continues to operate despite network failures between nodes. The theorem proves that in the event of a partition, a system must choose to sacrifice either Consistency or Availability. You can't have all three.

When to use it

Use CAP as a framework when choosing or designing a distributed database. For systems where data integrity is non-negotiable, like financial transactions or inventory management, a CP system is appropriate. It will refuse requests during a partition to prevent inconsistency. This is the choice for systems that prioritize correctness above all else.

When not to use it

For systems where high uptime is more critical than perfect data consistency, an AP system is a better fit. Examples include social media feeds or analytics dashboards, where serving slightly stale data is an acceptable price for keeping the service online and responsive. A CA (Consistent, Available) system is largely theoretical for distributed systems, as it assumes a perfect network that never fails, which is unrealistic.

One canonical example

During a network partition, a CP database like Google's Spanner might refuse a write request or make a client wait if it cannot guarantee all replicas are updated. In contrast, an AP database like Amazon's DynamoDB will accept the write on an available node and resolve potential conflicts later, ensuring the user's request succeeds even if data is temporarily inconsistent across the cluster.

Interview question

During a network partition, which fundamental trade-off must a distributed system make according to the CAP Theorem?

  • a.Guarantee all three properties: Consistency, Availability, and Partition Tolerance.
  • b.Prioritize either Consistency or Availability, while maintaining Partition Tolerance.Correct
  • c.Choose between high throughput and strong data consistency.
  • d.Sacrifice Partition Tolerance to ensure both Consistency and Availability.
Why?

The CAP Theorem states that in the event of a network partition, a distributed system must choose to sacrifice either Consistency or Availability. Partition Tolerance is a given for distributed systems, meaning they must continue to operate despite network failures. Option D is incorrect because sacrificing Partition Tolerance is not a viable choice for a distributed system, as it assumes a perfect network.

Just read this? Test yourself on what you have been reading.

Read the original → en.wikipedia.org

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on distributed systems — each one lists the topics its interview covers.

See open roles