$ man replication # same data, many machines

Replication

Replication means keeping a copy of the same data on several machines (replicas), so the system survives a dead server, serves more reads, and sits closer to users. The hard part is not copying the data once: it is keeping the copies in step while writes keep arriving and machines keep failing.

Every replicated database answers two questions:

  • Who accepts writes? One leader, several leaders, or any replica.
  • When is a write “done”? As soon as one machine has it (asynchronous), or only after others confirm it (synchronous).

Watch a write travel

The most common setup is single-leader (also called primary/replica): clients write to the leader, the leader ships its log to the followers, and reads can go to any of them.

$ ./replicate --mode async --followers 2
An async write, a lagging follower and a stale read
CLIENT LEADER city = New York log pos 41 FOLLOWER 1 city = New York applied 41 FOLLOWER 2 city = New York applied 41 WRITE city=Toronto ACK right away city = Toronto log pos 42 city = Toronto applied 42 applied 41 · lag 2 s still in flight CLIENT 2 New York (stale) FOLLOWER 2 city = Toronto applied 42
NO.STEPFROMTOTYPEINFO
101clientleaderWRITEuser 42 city = Toronto → log pos 42, ack
202leaderfollower1REPLICATEpos 42 applied
302leaderfollower2REPLICATEpos 42 delayed (busy disk, slow link)
403client2follower2READcity = New York · stale by 2 s
504leaderfollower2REPLICATEpos 42 applied · replicas agree
  1. One leader and two followers all store user 42: city = New York. Only the leader accepts writes.
  2. The user moves to Toronto. The leader appends the change to its log and, in async mode, confirms to the client immediately.
  3. The leader streams the log to its followers. Follower 1 applies it; follower 2 is busy and lags behind.
  4. Another client reads from follower 2 and gets New York: a stale read caused by replication lag.
  5. Follower 2 catches up and the replicas agree again. Async replicas are eventually consistent.

If the leader had crashed between step 1 and step 2, the client would have been told “saved” while no other machine had the write. Promoting a follower at that moment loses the write. That is the core trade-off of asynchronous replication.

Synchronous vs asynchronous

Mode Write is acknowledged when Upside Downside
Synchronous every (or a chosen) follower has it No data loss on failover One slow follower slows every write; one dead follower can block writes
Semi-synchronous at least one follower has it Survives a leader crash, bounded slowdown Still pays one network round trip per write
Asynchronous the leader has it Fastest writes, followers can be far away Recent writes are lost if the leader dies; followers serve stale data

PostgreSQL controls this with synchronous_commit and synchronous_standby_names, MySQL with semi-sync plugins, and Kafka with acks=all plus min.insync.replicas. The latency cost of waiting is the “else” half of PACELC.

Three replication topologies

Topology Who accepts writes Examples Watch out for
Single-leader One leader per dataset (or per partition) PostgreSQL, MySQL, MongoDB replica sets, Kafka partitions Failover: detecting a dead leader, avoiding two leaders (split brain)
Multi-leader One leader per region or device Multi-region MySQL/Postgres setups, offline-first apps, CouchDB Write conflicts: two leaders change the same row at once
Leaderless Any replica; client writes to several Cassandra, ScyllaDB, Riak, the original Amazon Dynamo design Needs quorums, read repair and conflict resolution; see Consistency vs Eventual Consistency

Leader election and failover in modern systems usually rely on a consensus protocol (Raft in etcd, CockroachDB, Kafka KRaft; Paxos in Spanner) so that the cluster agrees on exactly one leader.

Living with replication lag

Async followers are always a little behind, normally milliseconds, sometimes minutes under load. Three guarantees fix the most visible problems:

  • Read-your-writes: after a user saves their profile, read it back from the leader (or from a replica that has caught up to that write’s log position).
  • Monotonic reads: pin a user to one replica so a refresh never shows older data than the previous page.
  • Consistent prefix: readers see writes in the order they happened, so an answer never appears before its question.

How replicas are fed

  • Statement-based: ship the SQL text. Breaks on NOW(), RANDOM() and triggers. Mostly historical.
  • Physical log (WAL) shipping: ship disk-level changes. Exact copies, but leader and follower must run the same major version.
  • Logical (row-based) log: ship “row X changed from A to B”. Works across versions and is what change data capture tools like Debezium read.

Replication vs sharding

Replication copies the same data to more machines (more availability, more read capacity). Sharding splits different data across machines (more storage and write capacity). Production systems combine them: each shard is replicated.

Common mistakes

  • Treating a replica as a backup. A DROP TABLE or a bad migration replicates in milliseconds. Keep point-in-time backups too.
  • Reading from a replica right after writing. Classic bug: “I saved it and it disappeared.” Route those reads to the leader.
  • Running analytics on the primary. Point heavy BI or ELT extraction at a read replica or at CDC instead.
  • Ignoring lag metrics. Alert on replica lag (seconds and bytes) the same way you alert on CPU.
  • Automatic failover without fencing. If the old leader is only slow, not dead, you can end up with two leaders accepting writes.

Learning Resources

Books

  • Designing Data-Intensive Applications (2nd ed.), Martin Kleppmann and Chris Riccomini: the replication chapter
  • Database Internals, Alex Petrov: part II on distributed systems

Docs and articles

Videos