Replication
Replication keeps copies of the same data on several machines.
Why it matters
One copy of your data is a single point of failure, and one machine can only answer so many reads. You need copies — and then you have to keep them in agreement.
The idea
Copies give you availability (another copy takes over when one fails), durability, and more read capacity. In leader–follower replication, all writes go to the leader and the followers copy them; reads can go to followers. Copying can be synchronous (safe but slower) or asynchronous (fast, but followers can lag behind). Multi-leader and leaderless designs allow writes in several places but must resolve conflicts.
A teacher writes on the main whiteboard and assistants copy it onto boards in other rooms. Students in those rooms may see each update a moment later.
Assistants copy perfectly and in order. Real followers can lag seconds behind or miss updates during failures, and if the main board breaks, safely choosing a new 'main board' (failover) is tricky.
How it works
- A write arrives at the leader. The leader saves it and adds it to its replication log.
- Followers read that log and apply the same changes in the same order.
- Synchronous: the leader waits until a follower confirms before telling the client 'done'. Asynchronous: it replies immediately and the followers catch up.
- Reads can go to followers to spread the load, but they may be slightly behind.
- If the leader dies: notice it (missed heartbeats), pick the most up-to-date follower, promote it, and send writes to it.
Example
A user changes their profile name and refreshes, but the read goes to a lagging replica and shows the old name. Fix: 'read your own writes' — send that user's reads to the leader for a short time after a write.
Key points
- Leader–follower: simple and common; all writes go through one leader.
- Synchronous: no data loss on failover, but slower writes. Asynchronous: fast, but recent writes can be lost.
- Replication lag causes stale reads; fixes include read-your-own-writes and monotonic reads.
- Failover: detect that the leader is dead, promote a follower, redirect writes. Risk: split brain.
- Multi-leader: writes in several regions; conflicts need rules (last write wins, merging).
- Leaderless (Dynamo-style): quorums with W + R > N.
Common mistakes
- Reading from a replica straight after a write, and being surprised by old data.
- Fully asynchronous replication for data you can't afford to lose.
- Automatic failover with no protection against two leaders (split brain).
Not true: Read from a replica straight after a write: you get the new data.
In fact: Replicas apply changes a little after the leader (replication lag), so a read straight after a write can return old data. Read your own writes from the leader.
Not true: Asynchronous: fast, and no write is lost when the leader fails.
In fact: The leader confirms a write before its followers have it. If the leader dies in that moment, the write is gone. Synchronous replication avoids this, with slower writes.
In the interview
I'd use leader–follower replication: writes to the leader, reads spread across replicas, with at least one synchronous replica in another zone for safe failover. Screens where users must see their own change straight away read from the leader.
Part of Unit 3: Data: databases, storage and scaling data in the free System Design course.