AlgoPlusAlgoPlus
Learn/System Design
Lesson

Replication

Keep copies of the same data on several machines — for read scale and for surviving a machine failure — via leader and followers.

10 min read Watch it move Build it

Replication keeps copies of the same data on several machines. It buys two things at once: fault tolerance (lose a machine, the data survives on the others) and read scaling (spread reads across the copies). The dominant pattern is leader-follower (also called primary-replica or master-slave).

Leader and followers

One node is the leader (primary) and takes all writes — it's the single source of truth. Every change it applies is streamed to the followers (replicas), which keep matching read-only copies. Reads can be served by any follower, so you add read capacity simply by adding copies. Writes, however, always funnel through the one leader.

  1. 1A client sends a write → it goes to the leader.
  2. 2The leader applies it locally and appends it to a replication log (the ordered stream of changes).
  3. 3Each follower pulls the log and replays the same changes in order, converging on the leader's state.
  4. 4Reads are routed to followers (scaling reads); the leader dies → a follower is promoted to take over.

Synchronous vs asynchronous

The key choice is *when the write is considered done*. Synchronous replication makes the leader wait for a follower to confirm it has the change before acknowledging the client — no data loss on leader failure, but every write pays the slowest follower's latency, and a stalled follower blocks writes. Asynchronous replication acknowledges the client immediately and streams to followers in the background — fast and available, but if the leader crashes before a change propagates, that write is lost. Many systems compromise with semi-synchronous: wait for *one* follower, stream to the rest async.

Replication lag
With async replication a follower is briefly behind the leader — the replication lag. Write your profile, then immediately read it from a lagging follower, and you see the *old* value: your own write appears to have vanished. Usually milliseconds, but under load or long transactions it can stretch to seconds.

Read-your-writes consistency

The lag problem above breaks a guarantee users expect: read-your-writes (read-after-write) consistency — after I write something, I should see it. Common fixes: route a user's reads to the leader for a short window after they write; track the write's log position and route the read to a follower that has caught up to it; or pin a user to one replica so at least they see a monotonic (never-going-backwards) view.

Failover promotes a follower
When the leader dies, failover promotes an up-to-date follower to leader so writes can resume — the whole point of the redundancy. The catch: with async replication the promoted follower may be missing the leader's last few writes, and if two nodes both think they're leader you get split-brain — which is why real systems gate promotion behind a consensus/quorum step.
OperationTimeSpace
Read throughput · reads spread across replicasscales with # followersO(copies)
Write throughput · all writes funnel through itbounded by the one leader
Check yourself
With asynchronous leader-follower replication, a user updates their profile then instantly refreshes and sees the OLD data. Why?