Managing Data Consistency

1. Understanding Eventual Consistency

PropertyDetail
DefinitionReplicas converge given no new writes
Convergence windowMilliseconds–seconds typical
Trade-offAvailability over immediate consistency
UseMost cross-service reads

2. Implementing Strong Consistency

MechanismDetail
Single-writer DBAll writes through one node
Quorum reads/writesR + W > N (Cassandra LOCAL_QUORUM)
Linearizabilityetcd, Spanner, FoundationDB
CostHigher latency, lower availability

3. Using Causal Consistency

ConceptDetail
DefinitionCausally related writes seen in order
MechanismVector clocks / dependency tags
UseComments-on-posts, chat threads

4. Implementing Read-Your-Writes Consistency

TechniqueDetail
Sticky sessionPin user to primary for short window
Read from primaryFor just-written records
Pass write timestampRead replica waits for catch-up
Client cacheDisplay 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
AspectDetail
MechanismVersion / ETag check on update
ProsLock-free, scalable
ConsRetries under contention

6. Implementing Pessimistic Locking

MechanismDetail
SELECT FOR UPDATERow lock until tx commits
Distributed lockRedis Redlock, Zookeeper
UseHigh contention, short critical sections
RiskDeadlocks, throughput loss

7. Using Idempotency for Safe Retries

PatternDetail
Idempotency-Key headerServer caches result by key
UpsertINSERT ... ON CONFLICT DO NOTHING
State machine guardRe-applying same transition is no-op

8. Implementing Conflict Resolution

StrategyDetail
Last-Write-WinsUse HLC/wall clock; loses data
Custom mergeApp-specific business rules
CRDTsMathematically merge-able types (G-Counter, OR-Set)
ManualSurface to user (e.g. doc collaboration)

9. Managing Data Versioning

ApproachDetail
Integer versionIncrement per update
ETag / hashContent-derived
TimestampHLC preferred over wall clock
History tableAudit + temporal queries

10. Understanding Consistency Trade-offs

AspectStrongEventual
LatencyHigherLower
AvailabilityLower under partitionHigher
ComplexityLower (simple model)Higher (handle stale)
ThroughputLimitedHigh

11. Implementing Consistency Patterns

PatternWhen
SagaMulti-service workflow
OutboxReliable event publication
InboxDedup at consumer
CDCStream DB changes
Event SourcingState = event log
Read-RepairFix stale on read

12. Using Consistency Levels

Level (Cassandra)Behavior
ONE1 replica responds
QUORUMMajority of replicas
LOCAL_QUORUMQuorum in local DC
EACH_QUORUMQuorum in every DC
ALLAll replicas (highest cost)
TunablePer-query consistency