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

  1. A write arrives at the leader. The leader saves it and adds it to its replication log.
  2. Followers read that log and apply the same changes in the same order.
  3. Synchronous: the leader waits until a follower confirms before telling the client 'done'. Asynchronous: it replies immediately and the followers catch up.
  4. Reads can go to followers to spread the load, but they may be slightly behind.
  5. 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

Common mistakes

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.