SkillSprout
Chapter 2 · Lesson 4
Intermediate
9 min read

Replication: Leaders, Followers and Lag

Overview

Replication means keeping copies of the same data on several machines. It gives you two things at once: durability and availability (if one machine dies, another has the data) and read scaling (many machines can answer reads). The price is that copies can disagree for a short time.

This lesson explains the standard leader-follower model, the difference between synchronous and asynchronous copying, what replication lag does to your users, how failover works, and how to design around stale reads in an interview.

Detailed Notes

What is it?

A replica is a copy of a database on another machine. In leader-follower replication (also called primary-replica):

  • One node is the leader (primary). All writes go to it.
  • The other nodes are followers (replicas). They receive every change from the leader and apply it in the same order. Clients may read from them.

The stream of changes the leader sends is its replication log (the write-ahead log from the relational lesson, or a similar change log). A follower that applies the log in order ends up identical to the leader.

Leader-Follower Replication
Architecture
DatabaseApplicationstateless serversDB routerwrite vs readLeaderall writesFollower 1zone BFollower 2zone Cwritesreadsreadsloglog
Key point. Replicas scale reads and survive failures. They do not scale writes.

Writes go to one leader. Followers apply the same log and serve reads.

Why do we need it?

Three reasons, each with numbers.

  1. Availability. A machine fails (say 99.9% available, about 9 hours of downtime a year). If two copies fail independently, the chance both are down at once is 0.001 x 0.001 = 0.000001, so the pair is available 99.9999% of the time. This only holds if a follower can take over quickly.
  2. Durability. A disk failure must not lose data. A copy on another machine, ideally in another availability zone (an isolated data centre in the same city), survives it.
  3. Read scaling. Suppose the app needs 20,000 reads per second and 2,000 writes per second, and one node serves about 5,000 reads per second. One leader cannot do it. With 4 followers handling reads, each serves 5,000, and the leader keeps its capacity for writes. Read replicas scale reads, not writes: every write still goes through one leader, and every follower must apply every write.

How does it work?

Synchronous vs asynchronous

When does the leader tell the client "done"?

  • Asynchronous. The leader commits locally, answers the client, and sends the change to followers afterwards. Writes are fast and survive a slow follower. If the leader dies before sending, those latest writes are lost when a follower is promoted.
  • Synchronous. The leader waits until a follower confirms before answering. No acknowledged write is lost. But each write is as slow as the slowest synchronous follower, and if that follower is down, writes stall.
  • Semi-synchronous (common compromise). Exactly one follower is synchronous, the rest are asynchronous. The data exists on at least two machines before the client is told "success", and if the synchronous follower falls over another one takes its place.
text
Write latency, leader in the same zone as followers (about 1 ms network)
Asynchronous     : 5 ms local commit
Semi-sync        : 5 ms + 1 ms follower ack = 6 ms
Synchronous to a follower in another region (80 ms round trip): 5 + 80 = 85 ms

The last line is why cross-region replication is nearly always asynchronous.

Replication lag and its three famous bugs

With asynchronous replication the follower is always slightly behind. The delay is replication lag: usually milliseconds, but seconds or minutes under heavy load or a network problem. Three user-visible problems follow, and each has a standard fix.

Replication Lag: Reading Your Own Write
Request flow
UserLeaderFollower1POST comment2201 saved3GET comments50 ms later4list without commentlag is 200 ms: stale read5replicate commentarrives at 200 ms6GET comments (fix)read from leader after a write7list with comment
Key point. The fix: send a user's reads to the leader for a few seconds after they write.

The comment is saved on the leader, but the follower has not received it yet.

  1. Read-your-writes violation. You post a comment, the page reloads from a lagging follower, and your comment is missing. Fixes: read from the leader for data the user just changed (for example for 10 seconds after a write, or for their own profile), or remember the timestamp of the user's last write and only read from a follower that has caught up to it.
  2. Monotonic reads violation. You refresh twice: the first read hits an up-to-date follower and shows the comment; the second hits a lagging one and it vanishes. Time seems to go backwards. Fix: pin each user to one follower (for example by hashing the user id), so their view only moves forward.
  3. Consistent-prefix violation. Two messages sent in order ("Where are you?" then "At home") appear reversed to a reader because they live on different partitions replicating at different speeds. Fix: keep related writes on the same partition so they keep order.

Worked example: leader handles 2,000 writes per second, and a follower can apply only 1,500 per second during a burst. The backlog grows by 500 per second, so after 60 seconds the follower is 30,000 writes behind and lag keeps rising. Fix by throttling writes, adding capacity, or removing the slow follower from the read pool. This is why you monitor replication lag and alert (for example at 5 seconds).

Failover: promoting a follower

When the leader fails, a follower must take over. This is failover:

  1. Detect. Others stop receiving the leader's heartbeat (a small periodic "I'm alive" message) for a timeout, say 10 seconds.
  2. Choose. Elect the most up-to-date follower as the new leader.
  3. Reconfigure. Clients and the other followers are redirected to the new leader. The old leader, if it returns, must become a follower.

What goes wrong:

  • Lost writes. With asynchronous replication, writes the old leader had not yet shipped are lost.
  • Split brain. The old leader is only slow, not dead, and wakes up believing it is still leader, so two nodes accept writes. Prevention: a coordination service or lease (a time-limited claim) decides the single leader, and the old leader is fenced (blocked from writing).
  • Timeout tuning. Too short, and a brief network blip causes needless failovers. Too long, and the outage lasts longer.

Other topologies

  • Multi-leader. Several leaders, usually one per region, each accepting local writes and syncing to the others. Good for low-latency writes in many regions, but two leaders can modify the same record at once, so you need conflict resolution (for example "last write wins" by timestamp, which can silently drop an update). Use only when you truly need local writes in every region.
  • Leaderless. Any node accepts writes and readers query several nodes; quorums decide correctness (see the consistency lesson).

Interview tip. Do not draw replicas and say "so it's available and consistent". Say what lag does: "reads from followers can be stale, so for read-after-write I'd route the user's own reads to the leader for a few seconds". That one sentence shows depth.

Real-world example

A social app stores profiles in one leader and three followers across three availability zones. Reads for other people's profiles go to followers, spreading 15,000 reads per second. When a user edits their bio, their next 10 seconds of requests go to the leader, so they never see their old bio. One night the leader's machine fails; a monitoring service detects missing heartbeats, promotes the most caught-up follower in 12 seconds, and about 40 ms of unreplicated writes are lost and reported to the on-call engineer.

System design example

Design the database layer of a news site: 50,000 reads per second, 500 writes per second, readers in Europe and Asia.

  • Read-to-write is 100:1, so use one leader plus read replicas. 50,000 / 5,000 per node = 10 replicas at peak; round up to 12 for failures and maintenance.
  • Put replicas in each region. Asynchronous cross-region replication with lag typically under 1 second is acceptable: a reader seeing an article a second late is harmless.
  • Editors publishing an article read from the leader so they see their own change immediately.
  • Writes stay in one region (the leader's). 500 writes per second is well within one node, so multi-leader is unnecessary complexity.
  • Use semi-synchronous replication to one follower in the same region, so a leader crash never loses an acknowledged publish.
  • Monitor lag per replica and automatically remove a replica from the read pool when lag exceeds 5 seconds.

Interview discussion. Say: "Reads dominate 100 to 1, so I'd scale reads with replicas and keep a single leader for writes. Replicas are asynchronous, so I'd accept sub-second staleness for readers and route the author's own reads to the leader."

Trade-off thinking. Synchronous replication protects data but hurts write latency and availability. Asynchronous replication is fast but can lose the last writes on failover and serves stale reads. Choose per requirement: payments lean synchronous, a like counter leans asynchronous.

Key interview takeaway

Replication buys availability, durability and read capacity, not write capacity. Always mention the cost: lag, stale reads, and failover risks (lost writes, split brain). Name the consistency fix you use, such as read-your-writes through the leader, and say you will monitor lag.

Real World Example

A social app serves other people's profiles from followers but routes a user's own reads to the leader for 10 seconds after an edit, so they never see their old bio.

Common Interview Questions

Finished this lesson?

0 of 44 lessons done · 0% of the course