Skip to content

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.

  • ✅ No data loss on primary failure
  • ❌ 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_replication to 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
  • Asynchronous between regions; last-writer-wins conflict resolution
  • Use GLOBAL to query the nearest region replica
  • Leaderless, peer-to-peer replication
  • Replication Factor (RF): how many nodes store each row
  • NetworkTopologyStrategy for 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.