Managing Data Consistency
1. Strong Consistency Pattern
| Aspect | Detail |
| Guarantee | All readers see latest write immediately |
| Mechanism | Single primary, synchronous replication, distributed consensus |
| Cost | Reduced availability under partition (CAP) |
| Stores | Single-node RDBMS, Spanner, CockroachDB, etcd |
2. Eventual Consistency Pattern
| Aspect | Detail |
| Guarantee | Replicas converge to same value over time, absent new writes |
| Window | Inconsistency window (ms-seconds typical) |
| UX Mitigation | Show optimistic UI, "saved, syncing" indicators |
| Stores | DynamoDB (default), Cassandra, S3 |
3. Causal Consistency Pattern
| Aspect | Detail |
| Guarantee | Operations causally related observed in same order by all |
| Mechanism | Vector clocks, version vectors |
| Use Case | Social feeds (reply must follow original), chat |
4. Read-Your-Writes Consistency Pattern
| Aspect | Detail |
| Guarantee | Client always reads its own writes immediately |
| Implementation | Sticky session to primary; pass write timestamp on reads |
| Use Case | "My profile" updates appear instantly to user |
5. Monotonic Reads Pattern
| Aspect | Detail |
| Guarantee | Subsequent reads never go backwards in time |
| Mechanism | Pin client to single replica or track read versions |
| Anti-Symptom | Page refresh shows older data than previous load |
6. Monotonic Writes Pattern
| Aspect | Detail |
| Guarantee | Writes from same client applied in submission order |
| Mechanism | Same-session affinity; per-session sequence numbers |
7. Optimistic Locking Pattern
| Aspect | Detail |
| Mechanism | Read with version; write with WHERE version = read_version |
| Conflict | 0 rows affected → another writer won; retry/merge |
| Best For | Low-contention scenarios |
| JPA | @Version annotation auto-handles |
Example: Optimistic UPDATE
UPDATE accounts SET balance = $1, version = version + 1
WHERE id = $2 AND version = $3;
-- if rowcount = 0 → throw OptimisticLockException
8. Pessimistic Locking Pattern
| Aspect | Detail |
| Mechanism | SELECT ... FOR UPDATE acquires row lock |
| Holds Until | Transaction commits/rolls back |
| Best For | High-contention, short-tx workloads |
| Risk | Deadlocks; long transactions block others |
9. Conflict-Free Replicated Data Type Pattern
| CRDT Type | Use Case |
| G-Counter | Increment-only counter (page views) |
| PN-Counter | Increment + decrement counter |
| G-Set | Add-only set |
| OR-Set | Add/remove set with tags |
| LWW-Register | Last-writer-wins value |
| RGA / Yjs | Collaborative text editing |
Note: CRDTs guarantee strong eventual consistency without coordination — replicas converge automatically.
10. Version Vector Pattern
| Aspect | Detail |
| Definition | Map of [node → counter] tracking causal history |
| Compare | Determines: equal, descendant, or concurrent |
| Conflict Resolution | Concurrent → app-level merge or LWW |
| Used In | Dynamo, Riak, Cassandra (timestamps) |