CAP Theorem

About

The CAP Theorem states that you cannot have a system that is Partitioned (redundant/partition tolerance), Consistent (linearizability) and Available.

Consistency

Every read receives the most recent successful write, or an error. - If client A writes x=5; - Client B reads x; - B must see 5, not 4;

Availability

Every request receives a non-error response, even if it is stale. - The system must respond; - It cannot say "sorry, try later"; - But the response does not need to be correct (eventual consistency);

Partition Tolerance

The system continues to operate despite network partitions. - Messages may be delayed, dropped, or reordered; - Nodes cannot reliably communicate

The choices

You can only guarantee at most two of these three properties.

If:

  • You use more than one node;

  • Over a network;

  • And don't control the network perfectly;

You have already have P (distributed systems).

So the choice is, during a partition, do I prefer to be:

  • wrong (AP), opting for availability, or

  • unavailable (CP), opting for consistency;

If:

  • Single node, or

  • Perfect network (highly improbable)

Then you can be consistent and available (AC).

Type
Description

AC

Without partitioning, the data are consistent and available.

AP

With partitioning, if you opt by availability, you lose consistency.

CP

With partitioning, if you opt by consistency, you lose availability.

Why use it

Distributed systems fail in ways local systems don't, and CAP defines the failure behavior.

CAP forces you to answer:

When the network breaks, what do I sacrifice?

And the answer must be intentional, not accidental.

How to use it

In system design you use CAP by identifying failure scenarios.

What happens if:

  • Node A cannot reach Node B?

  • Half the cluster is unreachable?

  • Leader is isolated?

  • ...

And by classifying data by business semantics. Not all data needs the same CAP behavior.

Type
CAP preference

Payments

CP (needs consistency)

Auth tokens

CP (needs consistency)

Inventory count

CP or hybrid

User profile

AP (better availability)

Metrics/logs

AP (better availability)

Tunable consistency (Dynamo model)

Systems like Cassandra allow:

  • R read quorum;

  • W write quorum;

  • N replication factor;

R+W>NStrong consistencyR +W > N \longrightarrow Strong\ consistency

R+WNEventual consistencyR + W \le N \longrightarrow Eventual\ consistency

You are explicitly trading C vs A per request.

Last updated