Bookmarks

Replication

Copying data from one node to others so that the system keeps several consistent copies and survives the failure of a node.

Also: data replication, database replication, master-slave replication

checked 9 Nov

All terms

Replication keeps identical data on several nodes: one node accepts changes and the others repeat them locally. This lets a database survive a server failure without stopping the service, and spreads reads across several copies.

Primary and replicas

The classic scheme is one primary (a master) and several replicas (standbys). All writes go through the primary: it applies a change and records it in its log — the write-ahead log, the binary log or the operation log. The replicas read this log and repeat the same changes locally, so their copies usually lag behind the primary by fractions of a second.

SystemWhat is sent to replicas
PostgreSQLa stream of WAL records — the write-ahead log
MySQLbinary log (binlog) events
MongoDBthe operation log (oplog)
Redisa stream of the commands applied by the primary

There is also a multi-primary scheme, where several nodes accept writes at once, but it requires conflict resolution and is used less often.

Synchronous and asynchronous

  • Asynchronous: the primary acknowledges a write straight away, without waiting for the replicas. Fast, but if the primary fails the last writes can be lost.
  • Synchronous: the primary waits for an acknowledgement from a replica and only then commits the transaction. Writes are not lost, but every write costs at least one round trip between nodes.

Intermediate modes exist almost everywhere: in PostgreSQL the synchronous_standby_names parameter makes every commit wait until a replica has written the commit record to its own disk. In MySQL semi-synchronous replication waits for an acknowledgement from at least one replica. In Kafka the producer chooses the acknowledgement level itself: with acks=all a write counts as successful once every replica in the in-sync set (ISR) has confirmed it, so with a replication factor of 3 and min.insync.replicas=2 it survives the failure of one broker out of three.

What it gives and what it costs

The main benefit is availability: when the primary fails, a replica is promoted to the new primary and the service keeps running. The second is read scaling: reports and heavy queries go to the replicas. The price is replication lag: a user who has just written data may not see it on the next request if the answer came from a replica that has not caught up yet. Replication is no substitute for backups: mistakenly deleted data spreads to every copy at once.

Sources

the term is written from these pages

Verified 9 Nov

Not to be confused with

what it is mixed up with and how it differs

  • sharding

    Sharding splits the data into parts that live on different nodes, replication copies the same data in full onto every node; in practice the two are often combined.

  • 3-2-1 backup rule

    Replication protects against the failure of a node, but not against data being corrupted or deleted: a corrupted write spreads to every replica.

In depth in the references

where the topic is covered in detail