Sub topic 2
The CAP Theorem and Consistency Trade-offs
This chapter assumes basic client-server systems, networks, databases, and simple Python.
The CAP theorem: what it says and what it does not
CAP describes a replicated system during a network partition, when some nodes cannot communicate. The system must choose between:
- Consistency: every successful read sees the latest completed write.
- Availability: every request to a reachable node receives a response.
- Partition tolerance: the system continues operating despite communication failures.
When a partition occurs, a system cannot guarantee both consistency and availability. CAP does not mean that systems always choose only two properties. Partitions are unavoidable in distributed systems, so the real choice is usually whether to reject or delay requests, or to return possibly stale data.
| Choice during partition | Typical behavior |
|---|---|
| Consistency | Reject or delay operations that cannot be safely coordinated |
| Availability | Serve requests using reachable replicas, even if data may be stale |
PACELC: latency versus consistency when there is no partition
PACELC extends CAP:
- P: if there is a partition, choose Availability or Consistency.
- E: else, choose lower Latency or stronger Consistency.
Even when all replicas can communicate, waiting for remote acknowledgements increases latency. A system may prefer local, fast reads and accept weaker freshness.
Eventual consistency and its anomalies
Eventual consistency guarantees that replicas converge if updates stop and communication continues. It does not guarantee that a read sees the newest value immediately.
Common anomalies include stale reads, reading different values in succession, and conflicting concurrent writes. A user can write a profile update and then read an older version from another replica.
> callout note > Eventual consistency is a convergence guarantee, not a freshness guarantee.
Quorums: tuning N, R and W
With N replicas, a write quorum W acknowledges a write, and a read quorum R consults replicas. The condition R + W > N guarantees that every read set overlaps every write set:
def quorums_overlap(n, r, w):
return r + w > n
print(quorums_overlap(3, 2, 2)) # TrueOverlap alone does not provide linearizability. Replicas may return stale versions, clocks may be incorrect, or failures may trigger special routing such as sloppy quorums.
Ordering events: happens-before and vector clocks
An event happens-before another when causality proves the first preceded the second. Vector clocks record a counter for each replica. If one vector is less than or equal to another in every component, it came first. If neither dominates, the events are concurrent and require conflict resolution.
def compare(a, b):
keys = set(a) | set(b)
le = all(a.get(k, 0) <= b.get(k, 0) for k in keys)
ge = all(a.get(k, 0) >= b.get(k, 0) for k in keys)
return "before" if le and not ge else "concurrent"
print(compare({"A": 1}, {"A": 2})) # beforeExercise
Does R + W > N guarantee linearizability?
Show answer
No. It guarantees quorum overlap, but ordering, version selection, failures, and coordination still matter.