DEV Community

Rossano D'Angelo
Rossano D'Angelo

Posted on

2

CAP Theorem: Consistency, Availability, Partition tolerance

This theorem states that it is impossible for a distributed system to simultaneously provide more than two of these three guarantees:

  • Consistency: all clients see the same data at the same time no matter which "node" of the system they connect to

  • Availability: any client which requests data gets a response even if some of the "nodes" of the system are not available

  • Partition tolerance: the system continues to operate despite network partitions1

CAP Theorem explained

Ideal situation

In an ideal world, network partition never occurs

CAP Theorem Ideal World

Real world distributed system

In a distributed system, data is usually replicated multiple times because partitions cannot be avoided.
In the example below, node n3 becomes unavailable and cannot communicate with nodes n1 or n2. Also data cannot be propagated to n3 from n1 or n2.

CAP Theorem Real world

If we choose consistency over availability (CP system) we must block all write operations to nodes n1 and n2 to avoid data inconsistency among these three nodes, and this makes the system unavailable.

However, if we choose availability over consistency (AP system) the system remains available and accepts read operations, even though it might return stale data. For write operations, nodes n1 and n2 will keep accepting them and data will be synced to node n3 when the network partition is resolved.


  1. a partition indicates a communication break between two "nodes" 

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay