Replication Strategies

How databases keep copies of data on several machines: one leader, many leaders or no leader, synchronous or asynchronous, and what replication lag, failover and write conflicts cost you.

Overview

Replication means keeping a copy of the same data on several machines, called replicas. Every production database does it, for three reasons: to survive the loss of a machine, to spread read traffic over more machines, and to put data close to users in other regions.

Copying data that never changes is easy. The hard part is changes. Every write must reach every replica, over a network that is slow and sometimes broken. Three questions decide how a system handles that: which replicas accept writes, whether a write waits for other replicas before it is confirmed, and what happens when two replicas accept conflicting writes.

This chapter walks through the answers databases actually use, from PostgreSQL's streaming replication to Cassandra's quorums, with what each one costs.

What you will be able to do

  • Compare single-leader, multi-leader and leaderless replication and pick one for a given workload.
  • Explain what synchronous, semi-synchronous and asynchronous replication each promise, and what a failover can lose.
  • Name the anomalies replication lag causes and the fix for each.
  • Walk through a safe failover, including how to stop a split brain.
  • Explain how write conflicts arise and the main ways to resolve them.

Key concepts

  • Single leader
  • Multi-leader
  • Leaderless
  • Synchronous and asynchronous
  • Replication log
  • Replication lag
  • Failover and fencing
  • Conflict resolution
  • Quorums

Interview questions

  • Compare single-leader, multi-leader and leaderless replication.
  • What are the trade-offs between synchronous and asynchronous replication?
  • A user updates their profile and then does not see the change. What happened, and how do you fix it?
  • Walk through a failover. What can go wrong?
  • How are write conflicts resolved in multi-leader or leaderless systems?
  • How does quorum replication work, and does W + R > N give strong consistency?
  • What do you monitor on a replicated database?
  • How would you replicate a database across three regions?

Sign up free to read the full topic

More Databases & SQL topics

Start free on Cariara · Coding problems · System design studio