Sub topic 5
The CAP theorem: what it says and what it does not
This chapter assumes you know what replication is, and that you have met linearizability, which is the guarantee that a replicated register behaves like a single copy. It also assumes you can read short Python snippets.
What the theorem states
The CAP theorem says that a replicated data store cannot, during a network partition, be both consistent and available. The three letters mean specific things here:
- Consistency (C) means linearizability. Every read sees the most recent completed write.
- Availability (A) means every request sent to a non-failed node gets a non-error response, without a guarantee that the response is the latest.
- Partition tolerance (P) means the system keeps operating when the network drops or delays messages between nodes.
Partitions happen in real networks, so P is not optional. The practical choice is what to do when a partition occurs: refuse some requests (choose C) or answer them with possibly stale data (choose A).
Why the trade-off exists
Suppose two replicas, R1 and R2, are cut off from each other. A client writes x = 1 to R1. Another client then reads x from R2.
R2 cannot know about the write. It has two options:
- Return an error or wait. This keeps the data consistent but sacrifices availability. Systems that behave this way are called CP.
- Return
x = 0. This keeps the node available but the read is stale, so the history is not linearizable. These systems are called AP.
The history below shows the second case. Client 2's read starts after the write completed, yet returns the old value.
Client A: write(x, 1) starts t=0, completes t=2
Client B: read(x) starts t=3, returns 0
Initial value of x is 0.What the theorem does not say
CAP is often quoted loosely. Keep these points in mind:
- It is not "pick any two of three." You cannot give up partition tolerance on a network that can partition. The theorem only constrains behavior during a partition.
- It says nothing about normal operation. When the network is healthy, a system can be both consistent and available. Latency trade-offs in that case are the subject of PACELC, covered in the next chapter.
- The terms are narrow. "Consistency" means linearizability, not the "C" in ACID. "Availability" means every non-failed node answers, which is stricter than "the service is mostly up."
- Systems are not strictly CP or AP. Many offer per-request settings, and different operations in one product may make different choices.
- It does not rule out weaker models. Causal consistency and session guarantees can remain available during a partition, because they do not demand a single global order.
A small decision helper
The function below models the decision a replica makes when it cannot reach a majority of the cluster.
def handle_read(reachable, total, prefer_consistency):
"""Decide how a replica answers a read during a possible partition."""
has_majority = reachable > total // 2
if has_majority:
return "serve latest value"
if prefer_consistency:
return "reject request (CP)"
return "serve local, possibly stale value (AP)"
print(handle_read(3, 5, True)) # serve latest value
print(handle_read(2, 5, True)) # reject request (CP)
print(handle_read(2, 5, False)) # serve local, possibly stale value (AP)Notice that the majority side keeps working in both modes. Only the minority side must choose.
Exercise
A team says, "Our database is CA, because we picked consistency and availability and dropped partition tolerance." What is wrong with this claim, and what choice does the system really face?
Show answer
A network can always partition, so partition tolerance cannot be dropped. CAP only constrains behavior during a partition, where the system must choose between consistency (rejecting some requests) and availability (answering with possibly stale data). A system that is "CA" simply fails to define its behavior under partitions, or it stops being available when one occurs.