Skip to content

Replication

Replication keeps a copy of the same data on several machines. It serves three goals: survive the loss of a node (availability and durability), spread reads over more servers (read throughput), and put data close to users (latency). Every replication scheme answers the same two questions. When is a write acknowledged? And who is allowed to accept writes?

One node, the leader (primary), accepts all writes. It records each change in a log: the WAL in PostgreSQL, the binary log in MySQL. Followers (replicas) stream that log and apply it in the same order. Clients may read from any replica. Older documentation calls this master–slave replication. The industry has moved to leader/follower and primary/replica.

flowchart LR
  app["Application"] -- "writes" --> leader[("Leader")]
  app -- "reads" --> r1[("Replica 1")]
  app -- "reads" --> r2[("Replica 2")]
  leader -- "change log" --> r1
  leader -- "change log" --> r2
  r2 -- "change log" --> r3[("Cascading replica")]

Typical uses:

  • Read scaling. Product pages, feeds and reports go to replicas. This works when reads far outnumber writes.
  • High availability. A replica is a warm standby that can be promoted if the leader fails.
  • Isolation of heavy work. Analytics, backups and exports run on a replica, so they do not slow down production writes.
  • Locality. A replica in another region serves local reads.

Replicas can feed further replicas (cascading). This takes load off the leader, but each hop adds lag.

The log comes in two flavours. Physical replication ships byte-level WAL: the whole cluster is copied, and the replica is read-only. Logical replication ships row changes: you can pick tables and even run different major versions. Logical replication also powers change data capture (CDC) tools that stream database changes into Kafka and search indexes. Since PostgreSQL 17, logical replication slots can be synchronised to a standby, so a CDC pipeline survives a failover.

Synchronous, asynchronous, semi-synchronous

Section titled “Synchronous, asynchronous, semi-synchronous”
sequenceDiagram
  participant C as Client
  participant L as Leader
  participant S as Sync replica
  participant A as Async replica
  C->>L: COMMIT
  L->>L: flush WAL locally
  L->>S: send change
  S-->>L: acknowledged<br/>(flushed)
  L-->>C: OK
  L--)A: send change (no wait)
  Note over L,A: If the leader dies now,<br/>A may lack the last commit
ModeCommit returns whenData loss on leader failureLatency costAvailability risk
AsynchronousLeader has flushed its logLast few transactions can be lostNoneNone from replicas
SynchronousLeader and chosen replica(s) have the changeNone, if a sync replica is promotedOne round trip to the replica on every commitWrites stall if the sync replica is unreachable
Semi-synchronous / quorumAt least k of the replicas acknowledgeNone while k replicas confirmRound trip to the fastest kTolerates slow or failed replicas, up to a point

How the big engines do it:

  • PostgreSQL streams WAL asynchronously by default. The docs note visibility lag is typically under a second. Setting synchronous_standby_names makes commits wait, either for named standbys in priority order (FIRST 2 (s1, s2, s3)) or for any quorum (ANY 2 (s1, s2, s3)). The synchronous_commit setting chooses how far the standby must get: remote_write (in OS memory), on (flushed to disk) or remote_apply (replayed, so visible to readers). If no synchronous standby answers, commits wait indefinitely.
  • MySQL semi-synchronous replication makes the source wait until at least one replica confirms the transaction is in its relay log on disk. After rpl_semi_sync_source_timeout (10 seconds by default) with no acknowledgement, it silently falls back to asynchronous and returns to semi-sync when a replica catches up. Monitor that switch: during it, you have no zero-loss guarantee.
  • Managed cloud databases often replicate at the storage layer. Amazon Aurora writes six copies across three availability zones and considers a write durable when four acknowledge. Compute replicas then read from that shared storage.

A common production setup is one synchronous or quorum replica in another availability zone for zero data loss, plus asynchronous replicas for reads and remote regions.

Replication lag and the anomalies it causes

Section titled “Replication lag and the anomalies it causes”

With asynchronous replicas, a follower is always slightly behind. Lag is usually milliseconds. It can grow to minutes during heavy batch writes, long-running queries on the replica, or network trouble. Lag shows up as user-visible bugs:

AnomalyWhat the user seesFixes
Read-your-writes violatedEdits a profile, refreshes, sees the old oneRead “own” data from the leader. Or read from the leader for a few seconds after a write. Or remember the write’s log position and read only from a replica that has replayed it.
Monotonic reads violatedA comment appears, then disappears on the next refresh (second read hit a more-lagged replica)Pin each user or session to one replica (for example, hash the user ID). Or have the client send the last position it saw.
Consistent prefix violatedSees an answer before the question it replies toKeep causally related writes on the same leader or partition. Or track dependencies.

The position-based fix is precise and cheap. After a write, the application stores the leader’s log position: in PostgreSQL the LSN from pg_current_wal_lsn(), in MySQL the GTID set. On the next read, it picks a replica whose replayed position (pg_last_wal_replay_lsn()) is at least that value. MySQL can also block until a replica catches up with WAIT_FOR_EXECUTED_GTID_SET(); PostgreSQL 19, a release candidate in October 2026 (general availability is planned for November), adds a WAIT FOR LSN command for the same job. PostgreSQL’s synchronous_commit = remote_apply gives the same effect at commit time, at the cost of slower commits.

Also expose lag as a metric and alert on it. Take a replica out of the read pool when its lag exceeds what the product tolerates.

When the leader dies, a follower must be promoted:

  1. Detect the failure. Usually this is missed heartbeats for some seconds. Too short a timeout causes needless failovers. Too long a timeout extends the outage.
  2. Choose the new leader. Pick the most up-to-date replica to minimise lost writes. A synchronous replica is ideal.
  3. Fence the old leader so it cannot accept writes if it is still running or comes back.
  4. Reconfigure. Point clients at the new leader (DNS, a virtual IP, a proxy or service discovery) and point the other replicas at it.

Tools such as Patroni for PostgreSQL use a consistent store (etcd, Consul, ZooKeeper or the Kubernetes API) to hold a leader lease. Only the lease holder may act as primary. Managed services such as RDS, Cloud SQL and Azure Database run this loop for you.

Split brain happens when two nodes both believe they are the leader and both accept writes. The data then diverges, and merging it is manual, painful and sometimes impossible. Defences:

  • Majority quorum for leader election: a node that cannot reach a majority must step down.
  • Fencing tokens: each new leader gets a higher epoch number, and storage rejects writes carrying an older epoch.
  • STONITH (“shoot the other node in the head”): power off or isolate the old leader before promoting a new one.

A real incident shows the stakes. In October 2018, a 43-second network partition at GitHub triggered automatic failover of MySQL primaries to another coast. Both sites ended up holding writes the other lacked. Reconciling them left the site degraded for 24 hours and 11 minutes.

Multi-leader (master–master) replication

Section titled “Multi-leader (master–master) replication”

Several nodes accept writes and replicate to each other asynchronously. The usual reason is one leader per region: users write locally with low latency, and a region can keep accepting writes if another is cut off. Offline-capable apps are the extreme case: every device is a leader.

flowchart LR
  subgraph eu["Region EU"]
    euapp["App servers"] --> eul[("Leader EU")]
  end
  subgraph us["Region US"]
    usapp["App servers"] --> usl[("Leader US")]
  end
  eul <-- "async replication, conflict resolution" --> usl

The price is write conflicts: two leaders change the same record concurrently. Strategies, from crude to careful:

  • Avoid them. Route all writes for a given record (a user, a tenant) to one “home” leader. This is the most common and most effective approach.
  • Last-writer-wins (LWW). Keep the version with the highest timestamp. Simple, but it silently drops the losing writes, and clock skew decides who “wins”. DynamoDB global tables in their default multi-region eventual consistency mode resolve conflicts this way. Since mid-2025 they also offer multi-Region strong consistency: each write reaches a second Region before it returns, and a concurrent write to the same item fails instead of being overwritten.
  • Merge. Keep both versions (siblings, tracked by version vectors) and let the application or user merge them, like a shopping cart that unions items.
  • CRDTs. Conflict-free replicated data types (counters, sets, maps, text sequences) are designed so that concurrent updates always merge to the same result, with no coordination. They are used in collaborative editors, Redis Software’s Active-Active databases and local-first apps.
  • Detect and abort. MySQL Group Replication in multi-primary mode certifies each transaction against concurrent ones. The first to be ordered commits everywhere, and the conflicting one rolls back. The result is consistent, but you get aborts under contention.

Multi-leader setups also need loop prevention, so that a change does not bounce between leaders forever. PostgreSQL 16 added origin = none subscriptions for this. Schema changes, auto-increment keys (use UUIDs or per-node ranges) and constraints such as uniqueness become much harder. Prefer single-leader unless locality or regional autonomy truly requires more.

The 2007 Amazon Dynamo paper popularised a design with no leader. The client, or a coordinator node acting for it, sends each write to all N replicas of a key. The write succeeds once W acknowledge. A read asks replicas and waits for R answers, then returns the newest version. Cassandra, ScyllaDB and Riak follow this model.

flowchart LR
  client["Client or coordinator"] -- "write v2" --> n1[("Replica 1: v2")]
  client -- "write v2" --> n2[("Replica 2: v2")]
  client -. "write lost, node down" .-> n3[("Replica 3: v1")]
  n1 -- "read" --> client
  n3 -- "read" --> client

With N = 3, W = 2 and R = 2 in the diagram, the write reached replicas 1 and 2. A read that hears from replicas 1 and 3 still sees v2, because any two of three nodes overlap. That is the quorum condition: R + W > N.

N, W, RReads see latest write?Tolerates down nodes for writes / readsUse
3, 2, 2Yes (overlap)1 / 1Balanced default (“QUORUM”)
3, 3, 1Yes0 / 2Read-heavy, rare writes
3, 1, 3Yes2 / 0Fast writes, reads must reach all
3, 1, 1No guarantee2 / 2Lowest latency, eventual consistency

Cassandra exposes this per query as consistency levels: ONE, QUORUM, LOCAL_QUORUM (a majority in the local datacenter) and ALL.

Mechanisms that keep replicas converging:

  • Read repair. When a read sees a stale replica, it writes the newer value back to it.
  • Anti-entropy. A background process compares replicas using Merkle trees (hashes of key ranges), so only differing ranges are transferred.
  • Hinted handoff. If a replica is down, another node stores the write with a “hint” and delivers it when the replica returns. Cassandra keeps hints for up to three hours by default.
  • Sloppy quorums. If some of a key’s N home nodes are unreachable, other healthy nodes accept the write, so W acknowledgements still arrive. Writes stay available during partitions, but a read quorum may not overlap them until handoff completes. R + W > N then no longer guarantees fresh reads.

Note that the managed Amazon DynamoDB service shares the name but not this design. Its 2022 USENIX paper describes a Multi-Paxos leader per partition that handles writes and strongly consistent reads.

  • Writes do not scale. Every replica applies every write. Adding replicas adds read capacity, never write capacity. For that, see partitioning and sharding.
  • Possible data loss on asynchronous failover, and stalls with synchronous replication.
  • Lag grows with load. A replica must keep up with the leader’s write rate while also serving reads. Long queries on a replica can delay or conflict with replay. PostgreSQL cancels conflicting standby queries after max_standby_streaming_delay. Turning on hot_standby_feedback avoids most of these cancellations, but dead rows then pile up on the primary. Parallel apply helps: MySQL replicas have used four applier threads by default since 8.0.27.
  • Replicas are not backups. A bad migration or DROP TABLE replicates within milliseconds. Keep point-in-time backups and delayed replicas as well.
  • More moving parts. You get more servers, failover automation to test regularly, connection routing, lag monitoring and runbooks.
Leader–followerMulti-leaderLeaderless
Write availabilityLeader is a single point until failoverHigh: each leader writes independentlyHigh: any W nodes
Write latencyOne regionLocal in every regionWait for the fastest W
ConsistencyStrong on the leader, stale on replicasConflicts must be resolvedTunable, eventual by default
ComplexityLowestHighest (conflicts, loops, schema)Medium (repair, tombstones, tuning)
Typical systemsPostgreSQL, MySQL, SQL Server, MongoDB replica setsMulti-region active-active, collaborative appsCassandra, ScyllaDB, Riak

Based on The System Design Primer by Donne Martin (CC BY 4.0). Restructured, rewritten and updated; site content is licensed under CC BY 4.0.