Understanding Consistency Models

1. Understanding Strong Consistency

PropertyDetail
DefinitionAll replicas appear identical to all observers at all times
ImplementationSynchronous replication + consensus (Raft/Paxos)
CostHigh latency (cross-replica round trip), reduced availability under partition
ExamplesSpanner, etcd, ZooKeeper, single-leader RDBMS

2. Understanding Eventual Consistency

PropertyDetail
DefinitionIf no new writes, all replicas eventually converge
Convergence Windowms (LAN async repl) → seconds (cross-region)
Conflict HandlingLWW, vector clocks, CRDTs, app-level merge
ExamplesDynamoDB (default), Cassandra, S3 (now strong), DNS

3. Understanding Causal Consistency

PropertyDetail
DefinitionCausally related ops seen in same order by all; concurrent ops may differ
MechanismVector clocks / dependency tracking
Use CaseComments threading, collaborative editing
ExamplesCOPS, Bayou, Riak

4. Understanding Linearizability (atomic consistency)

PropertyDetail
DefinitionEach op appears to take effect atomically at some point between invocation and response, respecting real-time order
Equivalent To"Single copy" illusion
CostCannot be both linearizable and available under partition (CAP)
Examplesetcd, ZooKeeper, single-key DynamoDB strong reads

5. Understanding Sequential Consistency

PropertyDetail
DefinitionAll ops appear in some sequential order respecting per-process program order; not necessarily real-time
vs LinearizabilitySequential allows reorder of non-overlapping ops across processes
ExamplesMany memory models, ZooKeeper writes

6. Understanding Read-Your-Writes Consistency

PropertyDetail
DefinitionA client always sees its own prior writes
ImplementationSticky sessions, version tokens, route to leader
Use CaseUser edits profile and immediately reloads

7. Implementing Monotonic Reads

PropertyDetail
GuaranteeSubsequent reads never return older versions than previous reads
ImplementationPin client to a replica; or use version vector ≥ last-seen

Example: Version-token guard for monotonic reads

public class MonotonicReadClient {
    private long lastSeenVersion = 0;

    public Record read(String key) {
        Record r = replica.read(key);
        while (r.version < lastSeenVersion) {
            r = replica.readFrom(leader, key);
        }
        lastSeenVersion = Math.max(lastSeenVersion, r.version);
        return r;
    }
}

8. Implementing Monotonic Writes

PropertyDetail
GuaranteeWrites from same client applied in issue order
ImplementationPer-client sequence number; replica buffers out-of-order writes

9. Understanding Session Consistency

ComposesGuarantees
Read-your-writes + monotonic reads + monotonic writes + writes-follow-readsStrong within a session, eventual across sessions
Used ByCosmos DB, MongoDB causal sessions

10. Understanding Consistency Trade-offs and Selection

WorkloadRecommended Model
Banking, inventory, leader electionLinearizable / Strong
User profile, sessionRead-your-writes / Session
Social feed, analyticsEventual
Collaborative editingCausal + CRDT
Counters, likesEventual + CRDT