Leader Follower Replication

Topics Covered

Why Replicate and the Leader Model

Reasons to replicate

The leader model

What the leader model gives up

Partitioning and replication together

Physical layout of replicas

Reading from followers

A brief note on terminology

Replication is not backup

What a follower actually does

Leader per partition in practice

Costs to remember

Replication means keeping copies of the same data on several machines. It is the most basic tool in distributed systems, and almost every system covered in the rest of this course replicates something. This lesson covers the most common way to do it: one replica, the leader, accepts all writes and sends them to the others, the followers. PostgreSQL, MySQL, MongoDB, Redis, Kafka partitions, etcd and many other systems use some version of this model. Understanding how it works, where it breaks and how to reason about its anomalies is the foundation for the multi leader, leaderless and consensus based designs that come later.

Reasons to replicate

There are four main reasons to keep copies of data on several machines. The first is durability: if one disk or machine fails, the data survives on another. The second is availability: if one machine is down for maintenance or because of a crash, another can keep serving requests. The third is read scaling: if many clients read the same data, spreading reads across copies multiplies capacity. The fourth is latency: placing a copy near users in another region lets them read without crossing an ocean. Different systems weigh these reasons differently, and the right replication design depends on which ones matter most.

All four benefits come with the same core difficulty. Data changes. Copying static data is easy; keeping several copies consistent while writes arrive continuously, machines fail and networks delay messages is the hard part. Every replication scheme is an answer to the question of how changes flow from where they are made to every copy, and what readers see while that flow is in progress.

The leader model

In leader follower replication, also called primary backup or master slave replication in older documentation, one replica is designated the leader. Clients send all writes to the leader. The leader applies each write to its own storage and records it in a replication log. Followers connect to the leader, receive the log and apply the same changes in the same order. Reads can go to the leader or, if the application tolerates some staleness, to any follower.

The appeal of this model is simplicity. Because all writes pass through one node, there is a single order of changes, and there are no conflicting concurrent writes to resolve. Followers are simply replaying the leader history. This is state machine replication from the Lamport clocks lesson, with the leader acting as the authority that decides the order.

All writes go through the leader, which appends them to a log and streams that log in order to every follower.

What the leader model gives up

Simplicity has costs. The leader is a write bottleneck: all writes must pass through one machine, so write throughput is limited by that machine, which is why large systems partition data and run one leader per partition. The leader is also a single point of failure for writes: when it fails, no writes can happen until a new leader takes over, a process called failover, which is the hardest part of this lesson. And clients far from the leader pay network latency on every write. Multi leader and leaderless designs, covered in the next lesson, relax these limits at the cost of having to handle conflicts.

Partitioning and replication together

Real systems combine replication with partitioning. A large dataset is split into partitions, sometimes called shards, ranges or tablets, and each partition is replicated with its own leader and followers. Each machine typically holds leaders for some partitions and followers for others, so write load spreads across the cluster. Kafka assigns a leader per topic partition. CockroachDB and TiKV assign a leader, called the leaseholder or Raft leader, per range. MongoDB assigns a primary per shard replica set. The replication concepts in this lesson apply to each partition independently.

Physical layout of replicas

Where replicas live determines which failures they survive. Replicas on the same machine survive a process crash but not a disk failure. Replicas in the same rack survive a machine failure but not a rack power or switch failure. Replicas in different availability zones, data centers within one region with independent power and networking, survive a zone outage, and cloud providers design zones so that correlated failures across them are rare. Replicas in different regions survive a regional disaster but pay tens to hundreds of milliseconds of latency between them. A common production default is three replicas spread across three zones in one region, with optional asynchronous replicas in another region for disaster recovery.

Reading from followers

Reading from followers scales reads and can reduce latency, but followers may be behind the leader. A read from a follower may return data that is milliseconds, seconds or, under load, minutes old. For some reads this is fine: a product catalog page or an analytics dashboard can tolerate slightly stale data. For others it is not: a user who just changed their password and is told the change failed because a follower still shows the old one will be confused or worse. Much of this lesson is about understanding and controlling that staleness, and the last section covers the specific anomalies it causes and how to prevent them.

A brief note on terminology

Different systems use different words for the same roles. Leader and follower are used by Raft, Kafka and many modern systems. Primary and replica or primary and secondary appear in PostgreSQL, MySQL and MongoDB. Older documentation used master and slave, which most projects have replaced. Some systems call followers standbys, especially when they do not serve reads, and distinguish hot standbys, which do serve reads, from warm standbys, which only replicate. The underlying model is the same in each case.

Replication is not backup

A common confusion is to treat replicas as backups. They protect against different things. A replica protects against losing a machine: when hardware fails, another copy is ready within seconds. A backup protects against losing data through mistakes: when someone deletes the wrong rows, a buggy deploy corrupts records or ransomware encrypts the database, replicas faithfully copy the damage within milliseconds, while a backup taken yesterday still holds the good data. A complete design has both, and it states how quickly each can restore service. Replicas give a recovery time measured in seconds for hardware failures; backups give a recovery point measured in hours for logical failures, and point in time recovery, which replays the archived log up to a chosen moment, narrows that gap.

What a follower actually does

It helps to picture a follower concretely. It holds a full copy of the data, or of the partitions assigned to it. It keeps a connection open to the leader and receives a stream of log entries. For each entry it applies the change to its own storage in log order and records the position it has reached, durably, so that a restart resumes from the right place. It may also serve reads, answering from whatever state it has applied so far. It never accepts writes from clients directly; a client that sends a write to a follower is either rejected or forwarded to the leader. This narrow role is what keeps the model simple: there is exactly one place where the order of changes is decided.

Leader per partition in practice

Consider a cluster of six machines holding twelve partitions, each replicated three times. With leaders spread evenly, each machine leads two partitions and follows four. Writes for any key go to the machine that leads its partition, so write load spreads across all six machines rather than one. When a machine fails, its two partitions elect new leaders among their surviving followers, and the load from those partitions moves to other machines. Systems such as Kafka and CockroachDB also rebalance leadership automatically after a failure heals, moving leaders back so that no machine is overloaded. The single leader model applies within each partition, while the cluster as a whole has many leaders working in parallel.

Costs to remember

Replication multiplies storage cost by the replication factor, adds network traffic for every write and adds operational work: monitoring lag, managing failover and upgrading replicas. Three replicas mean three times the disk and roughly three times the write bandwidth. These costs are usually worth paying for production data, but they are a reason not to replicate data that can be regenerated cheaply, such as caches or derived indexes, at the same level as the source of truth.

One writer, many copies

Leader follower replication sends every write through one replica, which orders writes and streams them to followers, so there are no conflicts to resolve. The price is a write bottleneck, a failover problem when the leader dies and staleness when reading from followers.