05 · Replication Strategies — Copy Data for Availability and Reads¶
Scaling Reads · Topic 5 of 7
What is Replication?¶
Replication copies data from a primary node to one or more replica nodes. Goals:
- High availability — failover if primary dies
- Read scalability — distribute read traffic across replicas
- Geographic distribution — serve data closer to users
Replication Modes¶
Synchronous Replication¶
Primary waits for at least one replica to acknowledge the write before returning success.
- Acknowledged writes survive failures covered by the chosen replica acknowledgment and failover policy; synchronous replication is not an unlimited zero-loss guarantee
- ❌ Higher write latency
Asynchronous Replication¶
Primary returns success immediately; replicas catch up in the background.
- ✅ Low write latency
- ❌ Replication lag — replica may serve stale data
Semi-Synchronous¶
Waits for at least one replica (not all). Balances latency and durability.
Replication Topologies¶
graph LR
P[Primary] -->|WAL stream| R1[Replica 1]
P -->|WAL stream| R2[Replica 2]
R1 -->|Cascading| R3[Replica 3]
- Single-leader: All writes go to one primary
- Multi-leader: Writes accepted at multiple nodes (conflict resolution needed)
- Leaderless: Any node accepts writes; quorum determines success (Cassandra, DynamoDB)
Cloud Implementations¶
- Physical streaming replication (WAL-based)
- Logical replication (row-level, table-selective)
- Synchronous standby:
synchronous_standby_names pg_stat_replicationto monitor lag
- Multi-region Paxos-based replication
- Automatic failover; zero data loss
- Read-only replicas in any region for low-latency reads
- Global Tables: multi-region, multi-master replication
- MREC mode replicates asynchronously across regions and resolves conflicts using last-writer-wins
- MRSC mode provides multi-region strong consistency with synchronous replication, subject to supported topology and feature constraints
- Choose the regional SDK endpoint explicitly; there is no
GLOBALquery keyword
- Leaderless, peer-to-peer replication
Replication Factor(RF): how many nodes store each rowNetworkTopologyStrategyfor multi-DC replication
- Replica Sets: one primary + multiple secondaries
- Oplog-based replication (logical)
readPreference:primary,secondary,nearest
Replication Lag¶
Replication lag is the delay between a write on the primary and its visibility on a replica.
Read-your-writes consistency
If a user writes and immediately reads from a replica, they may not see their own write. Use session consistency or route reads to primary for critical operations.
Define the Acknowledgment Boundary¶
A replica can acknowledge receipt, durable log flush or replay/application. Those are different guarantees. In PostgreSQL, synchronous_commit and synchronous-standby selection affect what the primary waits for. A durable replica can still lag in applying changes to queries.
Before promotion, fence the old primary so two writers cannot diverge (split brain). Define the recovery point objective (RPO: tolerable lost data) and recovery time objective (RTO: tolerable outage), then rehearse failover and restore. Semi-synchronous terminology differs by product; do not assume one universal contract.