Managing Data Consistency
1. Understanding Eventual Consistency
| Property | Detail |
|---|---|
| Definition | Replicas converge given no new writes |
| Convergence window | Milliseconds–seconds typical |
| Trade-off | Availability over immediate consistency |
| Use | Most cross-service reads |
2. Implementing Strong Consistency
| Mechanism | Detail |
|---|---|
| Single-writer DB | All writes through one node |
| Quorum reads/writes | R + W > N (Cassandra LOCAL_QUORUM) |
| Linearizability | etcd, Spanner, FoundationDB |
| Cost | Higher latency, lower availability |
3. Using Causal Consistency
| Concept | Detail |
|---|---|
| Definition | Causally related writes seen in order |
| Mechanism | Vector clocks / dependency tags |
| Use | Comments-on-posts, chat threads |
4. Implementing Read-Your-Writes Consistency
| Technique | Detail |
|---|---|
| Sticky session | Pin user to primary for short window |
| Read from primary | For just-written records |
| Pass write timestamp | Read replica waits for catch-up |
| Client cache | Display optimistic value |
5. Using Optimistic Locking
Example: version column
UPDATE orders
SET status = 'PAID', version = version + 1
WHERE id = $1 AND version = $2;
-- 0 rows affected → conflict; reload & retry
| Aspect | Detail |
|---|---|
| Mechanism | Version / ETag check on update |
| Pros | Lock-free, scalable |
| Cons | Retries under contention |
6. Implementing Pessimistic Locking
| Mechanism | Detail |
|---|---|
| SELECT FOR UPDATE | Row lock until tx commits |
| Distributed lock | Redis Redlock, Zookeeper |
| Use | High contention, short critical sections |
| Risk | Deadlocks, throughput loss |
7. Using Idempotency for Safe Retries
| Pattern | Detail |
|---|---|
| Idempotency-Key header | Server caches result by key |
| Upsert | INSERT ... ON CONFLICT DO NOTHING |
| State machine guard | Re-applying same transition is no-op |
8. Implementing Conflict Resolution
| Strategy | Detail |
|---|---|
| Last-Write-Wins | Use HLC/wall clock; loses data |
| Custom merge | App-specific business rules |
| CRDTs | Mathematically merge-able types (G-Counter, OR-Set) |
| Manual | Surface to user (e.g. doc collaboration) |
9. Managing Data Versioning
| Approach | Detail |
|---|---|
| Integer version | Increment per update |
| ETag / hash | Content-derived |
| Timestamp | HLC preferred over wall clock |
| History table | Audit + temporal queries |
10. Understanding Consistency Trade-offs
| Aspect | Strong | Eventual |
|---|---|---|
| Latency | Higher | Lower |
| Availability | Lower under partition | Higher |
| Complexity | Lower (simple model) | Higher (handle stale) |
| Throughput | Limited | High |
11. Implementing Consistency Patterns
| Pattern | When |
|---|---|
| Saga | Multi-service workflow |
| Outbox | Reliable event publication |
| Inbox | Dedup at consumer |
| CDC | Stream DB changes |
| Event Sourcing | State = event log |
| Read-Repair | Fix stale on read |
12. Using Consistency Levels
| Level (Cassandra) | Behavior |
|---|---|
| ONE | 1 replica responds |
| QUORUM | Majority of replicas |
| LOCAL_QUORUM | Quorum in local DC |
| EACH_QUORUM | Quorum in every DC |
| ALL | All replicas (highest cost) |
| Tunable | Per-query consistency |