Sub topic 10
Causal Consistency
This chapter assumes you know what replication is, how linearizability and sequential consistency differ, and how happens-before and vector clocks work. It also assumes you can read simple Python.
What causal consistency promises
Causal consistency requires every client to see causally related operations in the same order. Operations are causally related when one could have influenced the other. This happens in three cases:
- Both operations come from the same client, and one precedes the other.
- One operation reads a value that the other wrote.
- The relation follows by transitivity through a chain of the first two cases.
Operations with no such link are concurrent. Different replicas may apply concurrent operations in different orders.
Take a comment thread. Alice posts "Is the server down?" Bob reads it and replies "Yes, restarting now." The reply depends on the question. Under causal consistency, no client can see Bob's reply without also seeing Alice's question. Under plain eventual consistency, a replica might receive the reply first and show an answer to a question that does not exist yet.
Where it sits among the models
Causal consistency is weaker than sequential consistency. Sequential consistency requires one total order that every client agrees on. Causal consistency only orders related operations. It is stronger than eventual consistency, which promises only that replicas converge.
Causal consistency does not require coordination across a partition. A replica can accept reads and writes during a partition and stay causally consistent, so it is a common choice when availability matters.
How a system tracks causality
A replica attaches dependency metadata to each write. A common approach uses a vector clock, as in the earlier chapter. A replica receiving a write checks that every dependency has already been applied. If one is missing, it holds the write back and applies it later.
The compare function below, from the vector clock chapter, gives the core test. A write is safe to apply when the clock describing its dependencies is before or equal to the replica's own state.
def compare(a, b):
"""Compare two vector clocks given as dicts of node -> counter."""
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)
if le and ge:
return "equal"
if le:
return "before"
if ge:
return "after"
return "concurrent"
def can_apply(deps, replica_state):
"""A write may be applied once all its dependencies are visible."""
return compare(deps, replica_state) in ("before", "equal")
print(can_apply({"A": 1}, {"A": 0})) # False: question not applied yet
print(can_apply({"A": 1}, {"A": 1})) # True: reply may now be appliedCommon errors
- Assuming causal means convergent. Two concurrent writes to the same key may leave replicas with different values. Causal consistency orders related operations only, so you still need a conflict resolution rule such as last-write-wins or a CRDT.
- Forgetting hidden causality. Systems track only dependencies they can see. If a user reads a value, then phones a friend who writes something, the database has no record of that link. Causality outside the system is not protected.
- Ignoring metadata cost. Dependency tracking grows with the number of nodes or clients. Real systems prune or compress it, which adds complexity.
Exercise
Replica R2 receives Bob's reply, whose dependencies are {"A": 1}, while R2's state is {"A": 0}. What should R2 do, and why?
Show answer
R2 should buffer the reply and not make it visible. compare({"A": 1}, {"A": 0}) returns "after", so the dependency has not been applied. Showing the reply now would violate causal consistency. R2 applies the reply once the question arrives and its state reaches {"A": 1}.