Replication

Keep copies of data on several machines: leader-follower, multi-leader and quorums, sync vs async, replication lag, read-your-writes and failover.

19 minLesson 3 of 7
On this page
  1. The idea
  2. Leader and followers
  3. Three ways to arrange replicas
  4. Numbers worth remembering
  5. In the interview
  6. Common mistakes
  7. What to practise next

A shop runs on one database server. At 14:00 its disk fails. The last backup is from 02:00, so 12 hours of orders are gone. Restoring the 2 TB backup at 200 MB/s takes 2 × 1012 ÷ 2 × 108 = 10,000 seconds, nearly 3 hours, and the site is down the whole time.

With a second server receiving every change as it happens, the same failure usually costs under a second of writes and about a minute of downtime.

The idea

Replication keeps copies of the same data on several machines, called replicas. Every change made on one is shipped, through a log of changes, to the others. You get three things: durability (a dead disk loses nothing), availability (another replica takes over), and read capacity (reads spread across copies).

The cost: copies are not updated at the same instant, and between a write and its arrival the replicas disagree. Most replication decisions are about how long that gap is and who can see it.

Leader and followers

The common setup: one replica, the leader, accepts all writes and streams its log of changes to followers, which apply them in the same order. Reads can go to any of them.

A write, a stale read, and a failoverUse the arrows or ← → keys
  1. A write, a stale read, and a failover, step 1User to App server; App server to Leader A (writes); App server to Follower B (reads); App server to Follower C (reads); Leader A to Follower B, asynchronous; Leader A to Follower C, asynchronous.UserApp serverLeader Aname = MoFollower BFollower Cwritesreadsreads
    A log
    1042
    B log
    1041
    C log
    1041

    1A user renames their profile to "Mo". The write goes to leader A, the only replica that accepts writes.

  2. A write, a stale read, and a failover, step 2User to App server; App server to Leader A (writes); App server to Follower B (reads); App server to Follower C (reads); Leader A to Follower B, asynchronous; Leader A to Follower C, asynchronous.UserApp serverLeader AFollower BFollower Cwritesreadsreads
    A log
    1042
    B log
    1041
    C log
    1041

    2A commits and replies at once. Replication is asynchronous: A does not wait for B or C.

  3. A write, a stale read, and a failover, step 3User to App server; App server to Leader A (writes); App server to Follower B (reads); App server to Follower C (reads); Leader A to Follower B, asynchronous; Leader A to Follower C, asynchronous.UserApp serverLeader AFollower BFollower Cold namewritesreadsreads
    A log
    1042
    B log
    1041
    C log
    1041

    3The user reloads. The read goes to follower C, which has not applied entry 1042 yet. They see their old name: replication lag.

  4. A write, a stale read, and a failover, step 4User to App server; App server to Leader A (writes); App server to Follower B (reads); App server to Follower C (reads); Leader A to Follower B, asynchronous; Leader A to Follower C, asynchronous.UserApp serverLeader Aname = MoFollower BFollower Cwritesreadsreads
    A log
    1042
    B log
    1041
    C log
    1041

    4Fix for read-your-writes: the app remembers the user wrote at log position 1042 and sends their reads to the leader until a follower catches up.

  5. A write, a stale read, and a failover, step 5User to App server; App server to Leader A (writes); App server to Follower B (reads); App server to Follower C (reads); Leader A to Follower B, asynchronous; Leader A to Follower C, asynchronous.UserApp serverLeader AFollower BFollower Cwritesreadsreads
    A log
    1042
    B log
    1042
    C log
    1042

    5About 300 ms later, entry 1042 reaches both followers.

  6. A write, a stale read, and a failover, step 6User to App server; App server to Leader A (writes); App server to Follower B (reads); App server to Follower C (reads); Leader A to Follower B, asynchronous; Leader A to Follower C, asynchronous.UserApp serverLeader AdownFollower BFollower Cwritesreadsreads
    A log
    1047
    B log
    1045
    C log
    1044

    6A takes 5 more writes, up to 1047, and dies. B has applied up to 1045, C up to 1044. Writes now fail.

  7. A write, a stale read, and a failover, step 7User to App server; App server to Leader A (writes); App server to Follower B (reads); App server to Follower C (reads); Leader A to Follower B, asynchronous; Leader A to Follower C, asynchronous.UserApp serverLeader AdownFollower Bnew leaderFollower Cwritesreadsreads
    lost writes
    2
    B log
    1045

    7After 10 seconds with no heartbeat from A, the system promotes the most up-to-date follower, B. Entries 1046 and 1047 never left A: those 2 acknowledged writes are lost.

  8. A write, a stale read, and a failover, step 8User to App server; App server to Leader A (writes); App server to Follower B (reads); App server to Follower C (reads); Leader A to Follower B, asynchronous; Leader A to Follower C, asynchronous.UserApp serverLeader AdownFollower BleaderFollower Cfollows Bwritesreadsreads
    lost writes
    2
    B log
    1045

    8Writes go to B, and C follows B. A restarted A must rejoin as a follower and discard 1046 and 1047. If it still acted as leader, two leaders would accept conflicting writes: split brain.

1 / 8

Synchronous or asynchronous

With synchronous replication the leader waits for a follower to confirm before telling the client "saved". No acknowledged write is lost on failover, but every write pays a round trip, and if that follower is down, writes stall. With asynchronous replication writes are fast and never wait, but a failover loses whatever had not shipped, as in the tracer.

The usual middle ground is one synchronous follower and the rest asynchronous: every acknowledged write is on two machines, and if the synchronous follower fails, another takes its role.

Three ways to arrange replicas

Replication topologies
OptionGood atBad atPick it when
Leader-follower Usual defaultSimple: one place orders all writes, so no write conflicts. Reads scale with followers.Write capacity is one machine. Failover needs care, or you lose writes or get two leaders.Almost always. The usual interview answer.
Multi-leaderEach region (or device) accepts writes locally, so writes are fast everywhere and survive a region outage.Two leaders can change the same row at once. You must resolve conflicts, and last-writer-wins silently drops one write.Writes from several regions that must not cross an ocean, or offline-first apps that sync later.
Leaderless (quorums)Any replica takes writes; no failover step. Tolerates slow or dead nodes smoothly.Every read and write contacts several nodes. Concurrent writes still conflict.High-availability key-value or wide-column stores where some staleness is fine.

Quorums: why R + W > N

In a leaderless store each key lives on N replicas. A write succeeds once W of them confirm; a read asks R of them and keeps the value with the highest version. If R + W > N, every set of R replicas shares at least one replica with every set of W, so a read always reaches at least one copy of the latest successful write. This needs a strict quorum: if stand-in nodes may accept writes while the owners are down (a "sloppy" quorum), reads can miss the write until it is handed back.

N = 3, W = 2, R = 2Use the arrows or ← → keys
  1. N = 3, W = 2, R = 2, step 1Client to Coordinator; Coordinator to Replica 1; Coordinator to Replica 2; Coordinator to Replica 3.ClientCoordinatorReplica 1Replica 2Replica 3
    N
    3
    W
    2
    R
    2

    1A client writes x = v2. The coordinator sends it to all 3 replicas, which hold v1.

  2. N = 3, W = 2, R = 2, step 2Client to Coordinator; Coordinator to Replica 1; Coordinator to Replica 2; Coordinator to Replica 3.ClientCoordinatorReplica 1v2Replica 2v2Replica 3v1
    N
    3
    W
    2
    R
    2
    acks
    2

    2Replicas 1 and 2 confirm. Replica 3 is unreachable and misses it. 2 confirmations = W, so the write succeeds.

  3. N = 3, W = 2, R = 2, step 3Client to Coordinator; Coordinator to Replica 1; Coordinator to Replica 2; Coordinator to Replica 3.ClientCoordinatorReplica 1v2Replica 2v2Replica 3v1
    N
    3
    W
    2
    R
    2
    answers
    v2, v1

    3A read asks 2 replicas and gets replicas 2 and 3. They disagree: v2 and v1.

  4. N = 3, W = 2, R = 2, step 4Client to Coordinator; Coordinator to Replica 1; Coordinator to Replica 2; Coordinator to Replica 3.ClientCoordinatorReplica 1v2Replica 2v2Replica 3v1
    N
    3
    W
    2
    R
    2
    returned
    v2

    4Any 2 of 3 overlap any other 2 of 3, because 2 + 2 = 4 > 3. So one answer is always new. The coordinator returns the higher version, v2.

  5. N = 3, W = 2, R = 2, step 5Client to Coordinator; Coordinator to Replica 1; Coordinator to Replica 2; Coordinator to Replica 3.ClientCoordinatorReplica 1v2Replica 2v2Replica 3v2
    N
    3
    W
    2
    R
    2

    5It also writes v2 back to replica 3 (read repair), so the stale copy heals.

  6. N = 3, W = 2, R = 2, step 6Client to Coordinator; Coordinator to Replica 1; Coordinator to Replica 2; Coordinator to Replica 3.ClientCoordinatorReplica 1v2Replica 2Replica 3v1 (stale)
    N
    3
    W
    1
    R
    1
    returned
    v1

    6Contrast W = 1, R = 1: 1 + 1 = 2, not more than 3. A write confirmed by replica 1 alone, then a read that asks only replica 3, returns v1. Faster, and stale.

1 / 6

Be precise about what a quorum does not give you. Two clients writing the same key at once both succeed, and the store must merge them or pick one; last-writer-wins silently drops a write. A write that reached fewer than W replicas reports failure but may still be read later. And mid-write, one reader can see the new value and a later reader the old one. R + W > N guarantees overlap, not the behaviour of a single up-to-date copy (linearizability), which needs extra machinery.

Numbers worth remembering

  • Replication lag inside one region: usually well under a second when healthy; seconds to minutes when a follower is overloaded or catching up.
  • Synchronous replication adds one round trip per write: about 0.5 ms in a data center, 50 to 150 ms between continents.
  • Async failover loses roughly write rate × lag. Detection takes seconds: a shorter timeout replaces leaders that were only slow.
Estimate: What an async failover loses

The leader dies with followers about half a second behind. How many acknowledged writes are lost?

You know
  • 2,000 writes a second at peak
  • Replication is asynchronous
  • The best follower lags 0.5 seconds

Show the working
  1. Writes in the unshipped window: 2,000 a second × 0.5 s
  2. = 1,000 writes acknowledged to users and then lost

Estimate 1,000 (anything from 300 to 3,000 is a good answer)

For likes, acceptable. For payments, no: use a synchronous follower so every acknowledged write is on two machines.

In the interview

Bring up replication when the requirements say "must not lose data" or "highly available", or reads swamp one server. Say which topology, sync or async, and how reads right after a write behave.

Replicating a profile service
Interviewer

Your database is a single point of failure. What do you do?

You

Leader-follower with two followers, one synchronous so an acknowledged write is always on two machines, one asynchronous for reads. Reads go to followers, which spreads our 20,000 reads a second.

Interviewer

A user edits their profile and sees the old one. Why?

You

Their read hit a follower that hadn't applied the change yet. I'd route a user's reads to the leader for a few seconds after they write, or pass the write's log position and only read from a follower that has reached it. Other users seeing the change 300 ms late is fine.

Follow-ups you should expect:

  • "How do you avoid two leaders after a failover?" Elect the leader through a consensus service and give each leader a term number; storage rejects writes carrying an old term (fencing). See consensus and leader election.
  • "Isn't replication a backup?" No. A bad DELETE replicates in milliseconds. Keep backups with point-in-time recovery too.

Common mistakes

Quick check

A leaderless store keeps N = 5 copies and writes with W = 3. What is the smallest R that guarantees reads overlap the latest successful write?

With async replication at 4,000 writes a second and 0.25 s of lag, the leader dies. About how many acknowledged writes are lost?

A user posts a comment and the page reloads without it. Others may see it a second later. Which fix targets this directly?

What to practise next

Replication copies all the data to every replica; when the data itself is too big for one machine, you split it with sharding. The guarantees here are made precise in CAP and consistency, and a quorum store is built end to end in Design a Key-Value Store.