Designing Data Consistency Patterns

1. Understanding Strong Consistency

AspectDetail
GuaranteeRead returns latest committed write
ImplementationSingle leader, sync replication, consensus
CostHigher latency; lower availability under partition
Use casesInventory, money, locks
SystemsSpanner, etcd, ZooKeeper, RDBMS

2. Understanding Eventual Consistency

AspectDetail
GuaranteeReplicas converge given no further writes
Windowms–seconds typically
Conflict resolutionLWW, vector clocks, CRDTs
SystemsCassandra, DynamoDB, S3, DNS

3. Designing Read-Your-Writes Consistency

TechniqueDescription
Sticky sessionsRoute user to same node
Read-from-primary windowFor N seconds after a write
Token-basedSend last write timestamp; replica waits
Local cache updateReflect own write immediately

4. Designing Monotonic Reads

IssueSolution
Going back in timeSticky session to one replica
Min-version tokenReject reads older than seen

5. Designing Monotonic Writes

MechanismDescription
Per-session orderingSame client's writes applied in order
Sequence numbersAppend seq id; replicas enforce order
Single connectionSerialize at conn level

6. Designing Causal Consistency

ConceptDetail
DefinitionIf A happens-before B, all clients see A before B
ImplementationVector clocks / dependency tracking
Use casesComment threads, social timelines
SystemsCOPS, MongoDB causal sessions

7. Understanding Session Consistency

PropertyDetail
ScopeWithin a single client session
IncludesRead-your-writes + monotonic reads + monotonic writes
Use casePractical for user-facing apps with weak global consistency

8. Designing Bounded Staleness

BoundDescription
Time-bounded"Up to 5 sec stale"
Version-bounded"At most K versions behind"
Use casesCosmos DB, dashboards, analytics

9. Designing Conflict Resolution Strategies

StrategyDescription
Last-Write-Wins (LWW)Pick latest timestamp; loses data
Application-defined mergeCustom function (e.g., shopping cart union)
CRDTsMathematically conflict-free
Multi-valueReturn all conflicting versions to client
Operational transformCollaborative editing (Google Docs)

10. Designing Optimistic Concurrency Control

Example: Version-based OCC (SQL)

UPDATE accounts
   SET balance = 100, version = version + 1
 WHERE id = 1 AND version = 7;
-- 0 rows affected → conflict; reload + retry
MechanismUse
Version columnDetect concurrent modification
ETag (HTTP)If-Match header for REST
CAS (Compare-and-Swap)Redis WATCH/MULTI, DynamoDB conditions
Best forLow-contention workloads

11. Designing Pessimistic Concurrency Control

MechanismSQL
Row lockSELECT ... FOR UPDATE
Skip lockedFOR UPDATE SKIP LOCKED (job queues)
Advisory lockspg_advisory_lock(key)
Distributed lockRedis Redlock, ZK ephemeral nodes
Warning: Hold locks briefly. Always set timeouts to avoid deadlocks/blocking.

12. Designing Consistency vs Availability Trade-offs

ChoiceWhen
Strong consistencyMoney, inventory, identity
Eventual + idempotent opsCounters, likes, social
Read-your-writesUser-visible UI updates
Bounded stalenessDashboards, BI
Weak/cache-onlyTrending, suggestions