Sub topic 3
Causal Consistency and Choosing a Model
Assume basic client-server systems, databases, key-value stores, and simple Python. This chapter connects weaker consistency models with implementation and testing choices.
Causal consistency
Causal consistency preserves cause-and-effect order. If a client writes a post and then writes a comment about it, every replica must show the post before the comment. Operations with no causal relationship may appear in different orders at different replicas.
Session guarantees
A session guarantee constrains what one client observes:
- Read-your-writes: a later read includes the client’s earlier successful write.
- Monotonic reads: later reads do not move backward to an older version.
- Monotonic writes: writes from one session are applied in their submission order.
- Writes-follow-reads: a write depends on the version the session previously read.
These guarantees can provide useful behavior without requiring every operation to be linearizable.
Resolving conflicts
Last-write-wins (LWW) selects the value with the greatest timestamp. It is simple, but clock errors or delayed writes can discard valid data. A CRDT is a data type designed so replicas can merge independently and converge. For example, a grow-only counter merges each replica’s count by taking the maximum for that replica, then sums the counts.
Consensus
Consensus protocols such as Raft allow replicas to agree on one ordered log despite failures. A leader proposes entries, and a quorum acknowledges them. A state machine applies committed entries in log order. This is a common implementation of strong, linearizable operations, but it adds coordination latency and may reject writes when a quorum is unavailable.
Serializability versus linearizability
Serializability concerns transactions: the result must match some serial transaction order. Linearizability concerns individual operations and preserves real-time order. Strict serializability combines both, requiring a serial transaction order that also respects real-time order.
Testing claims
Test histories, not just final values. Record invocation and response times, inject delays and replica failures, then check whether the observed history violates the claimed model. A passing test demonstrates an observed behavior, not that all possible histories are safe.
Choosing a model
Choose the weakest model that meets the workload’s user-visible requirements:
- Use linearizability or strict serializability for locks, balances, and unique ownership.
- Use causal consistency for collaborative feeds and dependent updates.
- Use eventual consistency with explicit conflict resolution for offline-first data.
- Add session guarantees when users need predictable personal views.
Exercise
Two concurrent replicas update a profile name. Which tool can merge both values without relying on synchronized clocks?
Show answer
A suitable CRDT, designed with an application-specific merge rule.