Skip to content
HLD Learn/Unique IDs & Distributing Data
Browsing as a guest. Sign in to save your progress and earn XP as you complete chapters.

Replication: Leader-Follower & Multi-Leader

6 min read

You'll learn to

  • -Explain leader-follower replication and why it helps with read scale and survival of a failure
  • -Compare synchronous, asynchronous and semi-synchronous replication in latency and durability
  • -Explain replication lag, the read-your-writes problem, and a fix
  • -Describe multi-leader and leaderless replication, and how a new leader is elected without split-brain

Sharding splits data up for scale. Replication does the opposite: it keeps copies of the same data on several machines, for two reasons. Reads can be spread across the copies, and if one machine dies, another has the data. Real systems do both at once: each shard is itself replicated. The interesting question is how several copies stay in step with each other, and what you give up when they briefly do not.

An Analogy: A Master Recipe Book

A restaurant chain keeps its master recipe book at head office. Whenever the head chef changes a recipe, a copy of the change is sent to every branch kitchen, and cooks at the branches read from their own copy instead of phoning head office. If head office burns down, the branches still have the recipes. The questions are how quickly a change reaches the branches, and what happens if a branch cook reads the book just before the update arrives.

  • -The master book at head office is the leader (also called the primary): the one place that accepts changes.
  • -The branch copies are the followers (replicas): they receive the stream of changes and serve reads.
  • -Sending the changes out is replication.
  • -A branch that has not yet received the latest change is lagging, which is replication lag.

Leader-Follower Replication

In the most common design, all writes go to the leader, which records each change and streams it to its followers. Reads can go to any follower. Because most applications read far more than they write, adding followers multiplies read capacity without touching the write path, and a follower can be promoted if the leader fails.

Leader
Replica A
Replica B

Leader-follower replication: all writes go to the leader, which streams changes to followers that serve reads.

LeaderReplica A- replicateLeaderReplica B- replicate

Synchronous or Asynchronous?

The leader has to decide when to tell the client the write succeeded. In synchronous replication it waits until the followers confirm they have it. In asynchronous replication it answers straight away and sends the change along afterwards. The trade is speed against safety:

  • -Synchronous: the data is on more than one machine before the client is told. But the write is as slow as the slowest follower, and a follower that is down can block all writes.
  • -Asynchronous: writes are fast, and a slow follower does not matter. But if the leader dies before a change has been sent, that change is lost. At 500 writes a second and a two-second delay, that could be around 1,000 writes.
  • -Semi-synchronous: wait for one follower, send to the rest asynchronously. At least two copies exist when the client hears back, without waiting for every follower. Many systems choose this middle path.

Replication Lag and Read-Your-Writes

With asynchronous followers, a read can arrive before the change does. The classic symptom: a user updates their profile photo, the write goes to the leader, the page refreshes and its read is served by a follower that has not yet received the change, so the old photo appears. The user thinks the save failed. This is the read-your-writes problem. The usual fix is to send a user's reads to the leader for a short time after they write, or to remember the position of their last write and only read from a follower that has caught up to it.

From the Wiki🔄 Database Replication

Copy your data to multiple servers to achieve fault tolerance, read scaling, and geographic distribution.

A newspaper company (Primary) prints the daily paper. It sends copies to hundreds of newsstands (Replicas) across the city. Readers can buy from any newsstand nearby - no need to travel to the print shop. If the print shop burns down, all the newsstands still have yesterday's paper.

Single-Leader (Master-Replica) Replication: One designated node accepts all writes (the leader/primary). It continuously ships write events to one or more follower nodes. Followers are read-only - they replicate and serve reads. This is PostgreSQL streaming replication, MySQL binlog replication, and Redis Sentinel.

This pattern dramatically improves read throughput: you can have 5 replicas and spread your read queries across all 6 nodes (1 primary + 5 replicas). Write throughput is unchanged because all writes still go through one node.

Synchronous vs Asynchronous replication: Synchronous means the primary waits for at least one replica to acknowledge every write before confirming success to the client. Zero data loss, but higher write latency. Asynchronous means the primary confirms immediately and replicates in the background - lower latency, but potential data loss if the primary crashes before the replica catches up.

Multi-Leader Replication: Multiple nodes accept writes. Used for multi-datacenter setups (one leader per data center). Each leader asynchronously replicates to the others. The trade-off: write conflicts can occur when the same row is modified simultaneously in two data centers.

Leaderless Replication (Dynamo-style): Used by Cassandra, Riak, DynamoDB. Any node can accept writes. The client writes to W nodes and reads from R nodes. If W + R > N (total nodes), you are guaranteed to read at least one up-to-date value. This enables extremely high availability - no single leader to fail.

  • -Primary goal: fault tolerance - if the primary fails, promote a replica.
  • -Secondary goal: read scaling - distribute reads across replicas.
  • -Replication lag: replicas may be seconds behind the primary (problematic for "read your own writes").
  • -Automatic failover (sentinel, orchestrator) needed in production.
  • -RPO (Recovery Point Objective) = max data loss tolerable. Sync replication = RPO of 0.
Read the full Wiki entry

Beyond One Leader

  • -Multi-leader: several nodes accept writes, often one per region, so writes are fast everywhere. The price is conflicts: two people edit the same record in different regions at the same moment, and the system needs a rule to resolve it (last write wins, a merge, or special data types designed to merge cleanly).
  • -Leaderless: any node can accept a write. A write goes to W of N copies and a read asks R of them, and if W plus R is more than N, the reads and writes always overlap. With N = 3, W = 2 and R = 2, any two copies you read must include at least one that has the latest write. This is the idea behind Dynamo-style stores such as Cassandra.

How a New Leader Gets Chosen: Consensus

The diagram above begs an obvious question: when the leader crashes, who decides which follower becomes the new leader, and how do you guarantee the whole cluster agrees on the same one? Get this wrong and you get "split-brain": two nodes each believing they're the leader, both accepting writes, silently diverging. This is exactly the problem distributed consensus algorithms solve, and it's worth being able to name one, even at a high level.

Paxos (the original, from Leslie Lamport) and Raft (designed later specifically to be easier to reason about) both work on the same core idea: a node can only become leader if it wins votes from a strict majority of the cluster. Because any two majorities of the same set must overlap by at least one node, it's mathematically impossible for two different nodes to both win a majority in the same term, which is exactly what rules out split-brain. Google's Chubby lock service and Spanner are both built on Paxos-family consensus; etcd, Consul, and CockroachDB are built on Raft.

You will essentially never be asked to implement Paxos or Raft from scratch in an interview; they're notoriously subtle. The signal an interviewer is actually looking for is knowing that leader election in a real distributed system isn't "whichever server notices first." It is a specific, majority-vote-based protocol. It is usually delegated to a battle-tested coordination service (like etcd or ZooKeeper) rather than hand-rolled, and you should be able to say so.

Interview Signal is part of Pro

See a real weak answer next to a real strong one for this exact topic.

Quiz is part of Pro

Test what you just read with a short quiz, and bank the XP.

Ready to Build This?

Level 7 (Consistent Hashing & Load Balancing) and Level 9 (Unique ID Generator) put this whole module into practice.

Discussion0

Join the Discussion

Sign in to leave comments, reply to others, or like insights.

Sign In to ScaleDojo

No comments yet. Be the first to start the thread!