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
| Word | What it means |
|---|---|
| Replication | keeping copies of data on several machines |
| Primary | the machine that accepts writes |
| Replica | a copy that follows the primary |
| Replication log | the ordered list of writes sent to replicas |
| Replication lag | how far a replica is behind the primary |
| Failover | making 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
# 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
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
v INFO persistence | grep aof_enabledShown 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
v CONFIG GET appendfsyncShown 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
v SET ledger:7 paidShown 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?
# 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
| Copy | Writes applied | Newest write in the log | Behind by |
|---|---|---|---|
| Primary | 120 | 120 | 0 |
| Replica east | 118 | 120 | 2 |
| Replica west | 120 | 120 | 0 |
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.