Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

How data replication works

After this lesson: I can explain how copies of data are kept and what a failure does.

55 min lesson4 commandsValkey lab: learn a Redis-compatible key-value store with keys, expiry, lists and counters

Keeping copies of the same data

Replication means keeping copies of the same data on more than one machine. If one machine breaks, another copy can take over and nothing is lost.

Almost every database and store in production is replicated. You will see how the copies are kept in step, and what a failure does to them.

By the end of this lesson

Note

I can explain how copies of data are kept and what a failure does.

Key words

WordWhat it means
Replicationkeeping copies of data on several machines
Primarythe machine that accepts writes
Replicaa copy that follows the primary
Replication logthe ordered list of writes sent to replicas
Replication laghow far a replica is behind the primary
Failovermaking a replica the new primary

The cells students fill in are left blank in this preview.

One primary takes the writes, replicas follow

In the most common design one machine is the primary. Every write goes to it. It puts each write in an ordered list, the replication log, and sends the list to the other machines.

Those machines are replicas. They replay the log in the same order, so they end up with the same data. Reads can go to any copy.

A replica replays the log

Code: python
# The primary writes to its log. A replica replays the same log.
log = []
replica = []
for order in ["order 1", "order 2", "order 3"]:
    log.append(order)
# The replica copies what it has not yet seen, in order.
for entry in log:
    replica.append(entry)
print("primary log:", log)
print("replica has:", replica)
# Add "order 4" to the list in the first loop and run again.

A write log lets a store remember

The log is not only for replicas. A store keeps one on disk so that it can rebuild its data after a restart. Valkey calls it the append-only file: each write is added to the end and nothing is changed.

A replica is the same idea sent over the network. The store flushes its log to disk once a second by default, so a crash can lose the last second.

Make a tiny client called v

Command
v() ( printf '%s\r\n' "$*" | nc -w 1 valkey 6379 )

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Ask whether the log is switched on

Command
v INFO persistence | grep aof_enabled

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Ask how often the log reaches the disk

Command
v CONFIG GET appendfsync

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Write one entry to the log

Command
v SET ledger:7 paid

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

How far behind is each copy?

Code: python
# Each copy remembers how many writes of the log it has applied.
log = ["SET a 1", "SET b 2", "SET c 3", "SET d 4", "SET e 5"]
applied = {"primary": 5, "replica1": 5, "replica2": 3}
for name in applied:
    behind = len(log) - applied[name]
    print(name, "is", behind, "writes behind")
# Change replica2 to 4 and run again.

Worked example: positions in the log

CopyWrites appliedNewest write in the logBehind by
Primary1201200
Replica east1181202
Replica west1201200

The cells students fill in are left blank in this preview.

When the primary fails, a replica takes over

If the primary stops, someone must pick a new one. This is failover. The best choice is the replica that has applied the most of the log, because it has lost the least.

In the table above, west is the best choice. East is 2 writes behind, and those 2 writes would be lost if east were promoted.

Waiting for replicas is safe but slow

The primary can answer the client at once, or it can wait until a replica confirms. The first is asynchronous replication: fast, but a crash can lose writes the replica has not seen.

The second is synchronous: safe, but each write waits for a trip to the replica, and it stops if the replica is down. Most systems wait for one replica and let the rest follow.

A common mistake

Warning

Many people think a replica is a backup. A replica copies every change at once, including a mistaken delete. A backup is a copy from one moment that you keep apart and do not change. You need both.

About this lab and its source: Valkey lab: learn a Redis-compatible key-value store with keys, expiry, lists and counters

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.