Understanding Consistency Models
1. Understanding Strong Consistency
| Property | Detail |
|---|---|
| Definition | All replicas appear identical to all observers at all times |
| Implementation | Synchronous replication + consensus (Raft/Paxos) |
| Cost | High latency (cross-replica round trip), reduced availability under partition |
| Examples | Spanner, etcd, ZooKeeper, single-leader RDBMS |
2. Understanding Eventual Consistency
| Property | Detail |
|---|---|
| Definition | If no new writes, all replicas eventually converge |
| Convergence Window | ms (LAN async repl) → seconds (cross-region) |
| Conflict Handling | LWW, vector clocks, CRDTs, app-level merge |
| Examples | DynamoDB (default), Cassandra, S3 (now strong), DNS |
3. Understanding Causal Consistency
| Property | Detail |
|---|---|
| Definition | Causally related ops seen in same order by all; concurrent ops may differ |
| Mechanism | Vector clocks / dependency tracking |
| Use Case | Comments threading, collaborative editing |
| Examples | COPS, Bayou, Riak |
4. Understanding Linearizability (atomic consistency)
| Property | Detail |
|---|---|
| Definition | Each op appears to take effect atomically at some point between invocation and response, respecting real-time order |
| Equivalent To | "Single copy" illusion |
| Cost | Cannot be both linearizable and available under partition (CAP) |
| Examples | etcd, ZooKeeper, single-key DynamoDB strong reads |
5. Understanding Sequential Consistency
| Property | Detail |
|---|---|
| Definition | All ops appear in some sequential order respecting per-process program order; not necessarily real-time |
| vs Linearizability | Sequential allows reorder of non-overlapping ops across processes |
| Examples | Many memory models, ZooKeeper writes |
6. Understanding Read-Your-Writes Consistency
| Property | Detail |
|---|---|
| Definition | A client always sees its own prior writes |
| Implementation | Sticky sessions, version tokens, route to leader |
| Use Case | User edits profile and immediately reloads |
7. Implementing Monotonic Reads
| Property | Detail |
|---|---|
| Guarantee | Subsequent reads never return older versions than previous reads |
| Implementation | Pin 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
| Property | Detail |
|---|---|
| Guarantee | Writes from same client applied in issue order |
| Implementation | Per-client sequence number; replica buffers out-of-order writes |
9. Understanding Session Consistency
| Composes | Guarantees |
|---|---|
| Read-your-writes + monotonic reads + monotonic writes + writes-follow-reads | Strong within a session, eventual across sessions |
| Used By | Cosmos DB, MongoDB causal sessions |
10. Understanding Consistency Trade-offs and Selection
| Workload | Recommended Model |
|---|---|
| Banking, inventory, leader election | Linearizable / Strong |
| User profile, session | Read-your-writes / Session |
| Social feed, analytics | Eventual |
| Collaborative editing | Causal + CRDT |
| Counters, likes | Eventual + CRDT |