Managing Data Replication

1. Understanding Replication Strategies

StrategyDescriptionExamples
Single-leaderOne leader; followers replicatePostgreSQL, MySQL, MongoDB
Multi-leaderMultiple writable nodesBDR, CouchDB, multi-region MySQL
LeaderlessAny replica accepts writes; quorumCassandra, Dynamo, Riak
ChainLinear chain head→tailFAWN, CRAQ

2. Implementing Synchronous Replication

PropertyValue
LatencyBounded by slowest replica RTT
DurabilityStrong — survives N-1 failures if N replicas
AvailabilityReduced — write blocks if replica down
Use CaseFinancial records, leader logs

3. Implementing Asynchronous Replication

PropertyValue
LatencyLocal commit only
DurabilityRisk of data loss on leader crash
AvailabilityHigh
LagReplication lag observable to readers

4. Implementing Semi-Synchronous Replication

MechanismDetail
At least one sync replicaOthers async
MySQLrpl_semi_sync_master_enabled
PostgreSQLsynchronous_standby_names
Trade-offBalanced durability/latency

5. Understanding Replication Lag and Staleness

AnomalyCauseMitigation
Read your writes failureRead hits stale followerRoute post-write to leader; version token
Monotonic read failureReads from different replicasSticky session
Causal violationEffect read before causeCausal tracking
Unbounded lagSlow followerAlert on lag SLI; remove follower

6. Implementing Read Replicas

PatternDetail
Read splitWrites → leader; reads → followers
Lag-aware routingReject replicas exceeding lag threshold
Cross-region replicasLocal low-latency reads, async lag
ToolsProxySQL, PgBouncer, RDS read endpoint

7. Handling Replication Conflicts

ResolutionMechanism
Last Write Wins (LWW)Highest timestamp wins; data loss possible
Multi-value (siblings)Keep all; app reconciles (Riak, Dynamo)
CRDTsAuto-merge by data-type semantics
Custom mergeApp-specific conflict handler

8. Implementing Conflict-Free Replicated Data Types (CRDTs)

CRDTOperationUse Case
G-CounterIncrement-onlyPage views
PN-CounterIncrement + decrementInventory
G-SetAdd-onlyTags
2P-SetAdd + remove (no re-add)Friends list
OR-SetAdd + remove with unique tagsShopping cart
LWW-RegisterSingle value with timestampUser profile field
RGA / LogootSequenceCollaborative text edit

9. Understanding Active-Active vs Active-Passive

Active-Active

  • All sites serve traffic
  • Better resource use
  • Conflict resolution required
  • Lower failover time

Active-Passive

  • One active, others standby
  • Idle capacity
  • No conflict
  • Failover detection delay

10. Implementing Chain Replication

AspectDetail
TopologyLinear: head → middle(s) → tail
WritesSent to head, propagated to tail
ReadsTail only (strong consistency)
CRAQ extensionReads from any node with version check
ProsStrong consistency + high read throughput

11. Implementing Quorum Reads and Writes (R + W > N)

ConfigRead LatencyWrite LatencyUse Case
N=3, R=2, W=2MediumMediumDefault balanced
N=3, R=1, W=3LowHighRead-heavy
N=3, R=3, W=1HighLowWrite-heavy
N=3, R=1, W=1 (sloppy)LowestLowestEventual; risk of lost updates
Note: R + W > N guarantees overlap of read/write quorums (strong consistency on key); does NOT make multi-key ops atomic.