# Replication: Keeping Copies of Data on More Than One Machine

> Learn what replication is, how leader-follower and leader-leader setups work, and the trade-offs of copying data fast or safely.

**In short:** Replication means keeping the same data on several machines at once, so one broken machine does not stop the app.

From "System design", a free course by Venkat R. Murty: https://designervenkat.online/learn/system-design/replication

## What You Will Learn

Imagine an app whose whole database lives on one server. If that server breaks, the app stops. If millions of people read from it at once, it slows down. Replication is one of the main tools system designers use to fix both problems. In this lesson you will learn what gets copied, how the common setups work, and what goes wrong when copies are slow or out of date.

You do not need any database background. Read the key words first, then go through the sections in order. The last part has a worked example and a short quiz.

- **Replication:** Keeping copies of the same data, or the same service, on more than one machine.
- **Node:** Any single machine or server that takes part in the system.
- **Leader:** The copy that accepts writes. Some older texts call it the master or primary.
- **Follower:** A copy that receives changes from the leader and serves reads. Older texts call it a slave or replica.
- **Write:** Any change to data, such as adding, editing, or deleting a record.
- **Read:** Asking for data without changing it.
- **Promotion:** Turning a follower into the new leader after the old leader fails.
- **Replication lag:** The delay between a write on the leader and the moment a follower has that same write.
- **Stale read:** A read that returns older data than the newest write.
- **Synchronous copying:** The leader waits for a follower to confirm the copy before it tells the client the write worked.
- **Asynchronous copying:** The leader tells the client the write worked first, and copies it to followers afterwards.
- **Write conflict:** Two copies change the same data at about the same time, so the system must decide which change wins.

## What Does Replication Mean?

Replication means that the same data, or the same service, lives on several machines at once. A single copy is a single point of failure: if it stops, nothing works. With several copies, a failed machine can be bypassed. The copies also share the read work, so more read requests can be handled at the same time.

- **Survive failures:** If one machine crashes, another copy still holds the data. The system can keep working, or recover faster, because the data is not stuck on one box.
- **Spread read traffic:** Many apps read far more often than they write. Extra copies can answer read requests, so the main machine does less work.
- **Place data near users:** A copy in another city or country can answer nearby users faster, because the request does not have to travel as far.

Replication is not the same as splitting data into parts. Splitting, called sharding, puts different rows on different machines. Replication puts the same rows on several machines. A later lesson covers splitting, so here we only care about copies.

## What Is Leader-Follower Replication?

Leader-follower replication is the most common setup. One node is the leader. All writes go to it. The leader records each change and sends it to the followers. Followers apply the same changes in the same order, so they end up with the same data. Followers can then serve reads, which takes load off the leader.

1. **A client sends a write:** An app asks the leader to save a new or changed record.
2. **The leader saves it:** The leader stores the change in its own log, so it can be replayed on other machines.
3. **The leader sends the change:** The leader streams the change to each follower.
4. **Followers apply the change:** Each follower applies the change, so its data catches up with the leader.
5. **Reads go to followers:** Read requests can be sent to followers. Those reads are fast, but a follower may be a little behind.

Note that followers usually do not accept writes. That rule keeps the data simple, because there is only one place where changes start.

### What Happens When the Leader Fails?

The leader is a single point of failure for writes. If it crashes, no one can change data until a new leader exists. The usual fix is promotion, where a follower takes the leader's job. The full details of switching over belong to a separate lesson, but the core idea is simple.

1. **Notice the failure:** The system decides the leader is gone, often because it stops answering for a set time.
2. **Pick a follower:** The system chooses the follower that has the most recent data.
3. **Promote it:** The chosen follower starts accepting writes as the new leader.
4. **Point the app at the new leader:** Writes now go to the new leader, and the other followers copy from it.
5. **Bring the old leader back as a follower:** When the old machine returns, it copies the data it missed instead of taking over again.

> **Tip:** Promotion can lose data if the chosen follower was behind. Writes the old leader confirmed but had not yet copied can disappear. Keep this in mind when we talk about asynchronous copying below.

## What Is Leader-Leader Replication?

Leader-leader replication, also called master-master or multi-leader replication, lets more than one node accept writes. Each leader saves its own writes and passes them to the other leaders. This can be useful when users in different places need to write quickly, because each group can write to a leader nearby.

- **More write places:** Writes are spread across several leaders, so one machine does not take every write. Some setups run leaders in several locations.
- **Write conflicts:** If two leaders change the same record at about the same time, the copies disagree. The system needs a rule to pick a winner, or it needs people or code to merge the two changes.
- **More complexity:** Finding conflicts, deciding how to fix them, and keeping all leaders in step takes more design work and monitoring than a single leader.

A simple example shows the problem. Two people edit the same shared address record in two cities at the same moment. Each leader accepts its edit. When the leaders swap changes, they find two different addresses for one record. Only a rule, such as last write wins or a merge step, can settle that.

## Synchronous and Asynchronous Copying

The next question is when the leader should tell the client that a write worked. There are two main choices. The choice affects both speed and safety.

- **Synchronous copying:** The leader waits until at least one follower confirms it has the write. If the leader then crashes, that follower already holds the data. The cost is slower writes, and a slow or missing follower can hold up the leader.
- **Asynchronous copying:** The leader confirms the write right away and sends the copy in the background. Writes are fast, but a write the leader confirmed can be lost if the leader dies before the copy leaves.
- **Semi-synchronous copying:** One follower is synchronous and the rest are asynchronous. If the synchronous follower fails, another follower can take its place. This is a common middle ground.

> **Tip:** Synchronous copying protects data but adds waiting to every write. Asynchronous copying is fast but risks losing the newest writes. Many teams pick semi-synchronous copying to get some of both.

### What Is Replication Lag?

Replication lag is the gap between a write on the leader and the same write on a follower. Under normal conditions the gap is often small, measured in milliseconds or under a second. Under heavy load, the gap can grow, because the follower has to process a backlog of changes before it catches up.

Lag matters because a read sent to a lagging follower can return old data. This is a stale read. A user may save a change, reload the page, and see the old version. The system is not broken in the usual sense. The data simply has not arrived yet.

```python
leader = {"address": "Old St"}
follower = dict(leader)

leader["address"] = "New Ave"
print("leader:", leader["address"])
print("follower before sync:", follower["address"])

follower.update(leader)
print("follower after sync:", follower["address"])
```

Output:

```text
leader: New Ave
follower before sync: Old St
follower after sync: New Ave

```

The sample above is a tiny model, not a real database. It shows the same idea: the leader has the new value first, and the follower sees it only after it catches up.

## Trade-offs for Availability and Consistency

Replication lets you choose how much to protect availability, which means the system keeps answering, and how much to protect consistency, which means every reader sees the same, latest data. You cannot get the best of both in every case, so you pick what your app needs most.

- **Synchronous copying favours consistency:** A confirmed write is on at least two machines, so it survives a leader crash. But if the synchronous follower is down, writes can stall. The system gives up some availability to keep data safe.
- **Asynchronous copying favours availability:** The leader keeps accepting writes even when followers are down. The trade-off is possible data loss on failover and stale reads from lagging followers.
- **Leader-leader adds conflict risk:** Several writable nodes keep the system running when one site is cut off. But they can disagree, and the system must merge or pick a winner later.

The earlier lesson on availability and consistency explains these two goals in general. Replication is one place where the choice becomes concrete, because each copy setting picks a different balance.

> **Tip:** When you design, ask two questions. What happens to a confirmed write if the leader dies right now? And can a user ever see old data right after they change it? Your answers point to the copy setting you need.

## Worked Example: A Photo Post in a Social App

This example is a simplified picture of a social app, similar to Instagram, used to show the ideas. It is not a description of how any real company builds its system. Follow one photo from upload to feed.

1. **The post is written to the primary:** When you share a photo, the app writes the post to the primary database, which is the leader.
2. **The primary copies the post:** The leader sends the new post to several read replicas, which are followers. Here the copies are asynchronous, so the write is confirmed before every copy has finished.
3. **Feeds are read from replicas:** Millions of people load feeds every hour. Their reads go to the replicas, so the primary is free to handle the writes that come in.
4. **The primary crashes:** The primary stops answering. The system promotes one replica to be the new leader, so writes can continue.
5. **A fresh photo may be missing:** A replica that was lagging may not have the newest post yet. For a short time, a photo you just shared may not show up in some feeds. Once the replica catches up, it appears.

The example shows the whole lesson in one flow. Copies spread the read load and help the app survive a crash. Asynchronous copying keeps writes fast, and it also explains the short gap where a new photo is missing.

## Check Your Understanding

**Quick check:** An online shop has one leader database and two follower databases. A customer changes their delivery address, then reloads their profile and sees the old address. What most likely caused this, and how could the shop avoid it?

A. The leader lost the address change, so the change was never saved anywhere.
B. A follower had not yet received the change, and the reload was served from that lagging copy. The shop can send the customer's own reads to the leader for a short time after a write.
C. The shop deleted the customer's profile when they reloaded the page.
D. The browser kept an old copy of the page, so the shop should turn off all copies.

**Answer:** B. The write reached the leader, so the leader is fine. The reload read from a follower that was still catching up, which is replication lag causing a stale read. Sending that customer's reads to the leader for a short period, or waiting for the follower to catch up, avoids the problem.

**Quick check:** A travel app wants a second leader in another region, so users there can save bookings locally. What new problem should the team plan for first?

A. Followers will refuse to serve any reads at all.
B. Two leaders may change the same booking at about the same time, which creates write conflicts that need a rule.
C. The first leader will stop sending its changes to the second leader.
D. All reads will have to go back to the first region.

**Answer:** B. Leader-leader replication lets several nodes accept writes, and that is exactly where conflicts can appear. The other options describe problems that do not come from adding a second writable leader.

**Quick check:** A payment app must never lose a payment that the app has already confirmed to a customer, even if the main database crashes right after confirming. Slower writes are acceptable. Which copying choice fits best?

A. Asynchronous copying, so the leader confirms each payment at once.
B. Synchronous copying, so at least one follower has the payment before the leader confirms it.
C. Leader-leader setup with no rule for conflicts.
D. Followers only, with no leader at all.

**Answer:** B. Synchronous copying makes the leader wait for a follower, so a confirmed payment already exists on a second machine. Asynchronous copying is faster but can lose recent confirmed writes if the leader fails before copying them.

## Sources

- [PostgreSQL documentation: Log-Shipping Standby Servers (current)](https://www.postgresql.org/docs/current/warm-standby.html)
- [PostgreSQL documentation: Log-Shipping Standby Servers (version 15)](https://www.postgresql.org/docs/15/warm-standby.html)
- [The Main Thread: Database Replication Topologies](https://themainthread.beehiiv.com/p/database-replication-topologies)
- [Tutorial Edge: Database Replication Explained](https://tutorialedge.net/software-eng/database-replication-explained/)
- [Resumax: Database Replication, Leader-Follower and Tradeoffs](https://resumax.ai/system-design/database-replication)
- [Notes on Replication Strategies in Distributed Data Systems](https://notes.elimelt.com/systems/databases/distributed-data/ch5-replication)
- [GeeksforGeeks: Types of Database Replication](https://www.geeksforgeeks.org/system-design/types-of-database-replication-system-design/)
- [Muthu System Design: Master-Slave Architecture](https://system-design.muthu.co/posts/data-management/master-slave-architecture/index.html)
- [OpenGenus IQ: Master-slave and Master-master replication in Databases](https://iq.opengenus.org/master-slave-and-master-master/)
- [Medium: Understanding Leader-Follower Replication in Distributed Systems](https://medium.com/@abdulakeemabdulafeez/understanding-leader-follower-replication-in-distributed-systems-666b737de4d1)
- [Bugfree.ai: Master-Slave vs Multi-Master Replication](https://bugfree.ai/knowledge-hub/master-slave-vs-multi-master-replication)
- [The Coding Gopher: Database Replication Explained](https://thecodinggopher.substack.com/p/database-replication-explained)
