esc
  • WorkflowsPages
  • Case studiesPages
  • Machine Learning FoundationsLearn
  • AI EngineeringLearn
  • AI Guardrails & Red TeamingLearn
  • UI/UXLearn
  • Design SystemsLearn
  • Production System DesignLearn
  • Book a callContact
  • Download ResumeContact
Let’s talk

I’ll build the agent for it.

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.

11 min readFree

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.

Key words

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. 1

    A client sends a write

    An app asks the leader to save a new or changed record.

  2. 2

    The leader saves it

    The leader stores the change in its own log, so it can be replayed on other machines.

  3. 3

    The leader sends the change

    The leader streams the change to each follower.

  4. 4

    Followers apply the change

    Each follower applies the change, so its data catches up with the leader.

  5. 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. 1

    Notice the failure

    The system decides the leader is gone, often because it stops answering for a set time.

  2. 2

    Pick a follower

    The system chooses the follower that has the most recent data.

  3. 3

    Promote it

    The chosen follower starts accepting writes as the new leader.

  4. 4

    Point the app at the new leader

    Writes now go to the new leader, and the other followers copy from it.

  5. 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

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. 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. 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. 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. 4

    The primary crashes

    The primary stops answering. The system promotes one replica to be the new leader, so writes can continue.

  5. 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?

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?

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?

Sources

How is this lesson?

Last updated on 17 November 2026

Learn with me

Every lesson is free. I add new ones as I learn. Have a question, or want help with your own work? Book a short call.