Designing Data Consistency Patterns
1. Understanding Strong Consistency
| Aspect | Detail |
| Guarantee | Read returns latest committed write |
| Implementation | Single leader, sync replication, consensus |
| Cost | Higher latency; lower availability under partition |
| Use cases | Inventory, money, locks |
| Systems | Spanner, etcd, ZooKeeper, RDBMS |
2. Understanding Eventual Consistency
| Aspect | Detail |
| Guarantee | Replicas converge given no further writes |
| Window | ms–seconds typically |
| Conflict resolution | LWW, vector clocks, CRDTs |
| Systems | Cassandra, DynamoDB, S3, DNS |
3. Designing Read-Your-Writes Consistency
| Technique | Description |
| Sticky sessions | Route user to same node |
| Read-from-primary window | For N seconds after a write |
| Token-based | Send last write timestamp; replica waits |
| Local cache update | Reflect own write immediately |
4. Designing Monotonic Reads
| Issue | Solution |
| Going back in time | Sticky session to one replica |
| Min-version token | Reject reads older than seen |
5. Designing Monotonic Writes
| Mechanism | Description |
| Per-session ordering | Same client's writes applied in order |
| Sequence numbers | Append seq id; replicas enforce order |
| Single connection | Serialize at conn level |
6. Designing Causal Consistency
| Concept | Detail |
| Definition | If A happens-before B, all clients see A before B |
| Implementation | Vector clocks / dependency tracking |
| Use cases | Comment threads, social timelines |
| Systems | COPS, MongoDB causal sessions |
7. Understanding Session Consistency
| Property | Detail |
| Scope | Within a single client session |
| Includes | Read-your-writes + monotonic reads + monotonic writes |
| Use case | Practical for user-facing apps with weak global consistency |
8. Designing Bounded Staleness
| Bound | Description |
| Time-bounded | "Up to 5 sec stale" |
| Version-bounded | "At most K versions behind" |
| Use cases | Cosmos DB, dashboards, analytics |
9. Designing Conflict Resolution Strategies
| Strategy | Description |
| Last-Write-Wins (LWW) | Pick latest timestamp; loses data |
| Application-defined merge | Custom function (e.g., shopping cart union) |
| CRDTs | Mathematically conflict-free |
| Multi-value | Return all conflicting versions to client |
| Operational transform | Collaborative 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
| Mechanism | Use |
| Version column | Detect concurrent modification |
| ETag (HTTP) | If-Match header for REST |
| CAS (Compare-and-Swap) | Redis WATCH/MULTI, DynamoDB conditions |
| Best for | Low-contention workloads |
11. Designing Pessimistic Concurrency Control
| Mechanism | SQL |
| Row lock | SELECT ... FOR UPDATE |
| Skip locked | FOR UPDATE SKIP LOCKED (job queues) |
| Advisory locks | pg_advisory_lock(key) |
| Distributed lock | Redis Redlock, ZK ephemeral nodes |
Warning: Hold locks briefly. Always set timeouts to avoid deadlocks/blocking.
12. Designing Consistency vs Availability Trade-offs
| Choice | When |
| Strong consistency | Money, inventory, identity |
| Eventual + idempotent ops | Counters, likes, social |
| Read-your-writes | User-visible UI updates |
| Bounded staleness | Dashboards, BI |
| Weak/cache-only | Trending, suggestions |