Single-leader replication: one primary, several replicas
One place for writes, many places for reads
Section titled “One place for writes, many places for reads”A database on one machine is a single point of failure, and it can only answer as many reads as that machine can serve. Replication keeps copies of the data on other machines. The simplest arrangement has a name that varies by product (leader and follower, master and slave, primary and replica). This course says primary and replica.
- The primary is the only node that accepts writes. It puts each one in order and records it.
- Each replica receives the writes from the primary and applies them in the same order. It is a copy that follows.
- Reads can go to the primary or to any replica.
Amazon’s documentation for read replicas describes the same shape: a read replica is a read-only copy of a database instance, updates are copied to it asynchronously, and you can route queries from your applications to it to reduce the load on the primary.
Two things follow, and they are the two halves of this module. A replica is a second copy, so losing the primary does not have to mean losing the data. And a replica is a copy that is behind, so what it returns may be old.
Mental model. The primary is a clerk who writes every change into a ledger. The replicas are other clerks who copy the ledger, line by line, from a photocopy that arrives a little later. Everyone copies in the same order, so a copy is never wrong. It is only ever behind.
What replication buys, and what it does not
Section titled “What replication buys, and what it does not”Take the shop of the failure labs. It handles 900 requests per second, and one request in five is a write, so the primary takes 180 writes per second and the two replicas answer the other 720 reads between them, 360 each. Add a third replica and each answers 240. Reads scale with the number of replicas.
Writes do not. Every replica has to apply every write, so each of them does the full 180 writes per second of work. A third replica adds read capacity and adds nothing to the number of writes the system can accept: that is still whatever the primary can do. Replication is a way to scale reads and to keep a second copy. Spreading writes over several machines is a different technique, covered in the sharding module.
Watch the replicas follow
Section titled “Watch the replicas follow”The lab below has a primary, Node 1, and two replicas, Node 2 and Node 3. An application sends ten writes, one every 100 ms. The primary acknowledges each one at once and ships it to the replicas, which apply it after a delay. Node 2 receives writes 100 ms after the primary, Node 3 300 ms after.
Press play, or use the step button, and watch the squares fill: each square is a write that the node has applied. Then drag Replication lag to Node 3 to the right and run it again.
A primary accepts 10 writes, one every 100 ms, and ships each one to two replicas. Change how late each replica receives them, and when the primary acknowledges a write.
- A write on its way to a replica
- A write sent to bring a node level
- A write that never arrives
- A link cut by a network partition
- A write the node has just accepted
- A write acknowledged to its client
- A write the node refused
- A read, with the number of the write it returned
Each square is a write, filled once the node has applied it.
Every replica has applied every write.
What the run came to
- Writes acknowledged
- 10 of 10
- Time to acknowledge a write
- At once
- Most writes a replica was behind
- Node 2: 1; Node 3: 3
| Node | Role | State | Writes applied |
|---|---|---|---|
| Node 1 | Primary | Accepting writes | 1, 2, 3, 4, 5, 6, 7, 8, 9 and 10 |
| Node 2 | Replica | In sync | 1, 2, 3, 4, 5, 6, 7, 8, 9 and 10 |
| Node 3 | Replica | In sync | 1, 2, 3, 4, 5, 6, 7, 8, 9 and 10 |
Event log: every event of the run, in order
| Time in the run | Event |
|---|---|
| 100 ms | Write 1 (the application) is accepted by Node 1. |
| 100 ms | Write 1 (the application) is acknowledged at once. |
| 200 ms | Node 2 applies write 1. |
| 200 ms | Write 2 (the application) is accepted by Node 1. |
| 200 ms | Write 2 (the application) is acknowledged at once. |
| 300 ms | Node 2 applies write 2. |
| 300 ms | Write 3 (the application) is accepted by Node 1. |
| 300 ms | Write 3 (the application) is acknowledged at once. |
| 400 ms | Node 3 applies write 1. |
| 400 ms | Node 2 applies write 3. |
| 400 ms | Write 4 (the application) is accepted by Node 1. |
| 400 ms | Write 4 (the application) is acknowledged at once. |
| 500 ms | Node 3 applies write 2. |
| 500 ms | Node 2 applies write 4. |
| 500 ms | Write 5 (the application) is accepted by Node 1. |
| 500 ms | Write 5 (the application) is acknowledged at once. |
| 600 ms | Node 3 applies write 3. |
| 600 ms | Node 2 applies write 5. |
| 600 ms | Write 6 (the application) is accepted by Node 1. |
| 600 ms | Write 6 (the application) is acknowledged at once. |
| 700 ms | Node 3 applies write 4. |
| 700 ms | Node 2 applies write 6. |
| 700 ms | Write 7 (the application) is accepted by Node 1. |
| 700 ms | Write 7 (the application) is acknowledged at once. |
| 800 ms | Node 3 applies write 5. |
| 800 ms | Node 2 applies write 7. |
| 800 ms | Write 8 (the application) is accepted by Node 1. |
| 800 ms | Write 8 (the application) is acknowledged at once. |
| 900 ms | Node 3 applies write 6. |
| 900 ms | Node 2 applies write 8. |
| 900 ms | Write 9 (the application) is accepted by Node 1. |
| 900 ms | Write 9 (the application) is acknowledged at once. |
| 1,000 ms | Node 3 applies write 7. |
| 1,000 ms | Node 2 applies write 9. |
| 1,000 ms | Write 10 (the application) is accepted by Node 1. |
| 1,000 ms | Write 10 (the application) is acknowledged at once. |
| 1,100 ms | Node 3 applies write 8. |
| 1,100 ms | Node 2 applies write 10. |
| 1,200 ms | Node 3 applies write 9. |
| 1,300 ms | Node 3 applies write 10. |
Illustrative values, not benchmarks of any real product.
Model notes
Assumptions
- There is one record. Each write gets the next number, in the order clients send them, and a node’s version is the number of the last write it has applied.
- A replica applies a write a fixed time after the primary accepts it: the lag you set. Confirmations travel back at no cost, so an acknowledgement waits only for replicas.
- A primary acknowledges a write once the number of replicas the mode names have applied it. If it cannot reach enough of them it keeps waiting, because it cannot tell slow from gone.
Simplifications
- Three nodes, one record and fixed lags. A real replication stream has delays that vary, batches, backlogs and many records.
Parameter values are illustrative. They are not benchmarks of any real product.
With the default settings, Node 2 is never more than 1 write behind, and Node 3 is never more than 3 writes behind. That is the lag divided by the interval between writes: 100 ms of lag at one write every 100 ms is one write, and 300 ms is three. At 1,000 ms of lag, Node 3 is 10 writes behind, which is every write the run sends.
This is the figure to remember about lag: it is the same delay measured in time or in writes. A replica that is 300 ms behind a busy primary is many writes behind, and the same 300 ms behind a quiet one is none.
Two other shapes
Section titled “Two other shapes”Single-leader is the common case and the one the rest of the module studies. The other two designs exist to remove its limits, and each pays for it somewhere else. This is an overview, not a lesson on each.
Multi-leader. Several nodes accept writes, each ships its writes to the others, and clients write to whichever is close. Writes no longer all queue at one node, and a leader in each region can keep accepting writes even when the regions cannot reach each other. The price is that two leaders can change the same record at the same moment, and nobody has decided which one wins. The PostgreSQL documentation describes asynchronous multimaster replication in these terms: each server works independently and periodically communicates with the others to identify conflicting transactions, and the conflicts can be resolved by users or by conflict resolution rules. Someone has to write those rules.
Leaderless. There is no primary. A client sends a write to several replicas and reads from several. The Dynamo paper is the classic description. It sets two numbers, R and W: the minimum number of replicas that must take part in a read and in a write. With N replicas, setting R and W so that R + W > N gives a quorum-like system, and the paper notes that latency is set by the slowest of the R or W replicas, so R and W are usually below N. Dynamo aims to be “always writeable”, and pays for that by keeping conflicting versions and handing them to the client to reconcile on a read.
| Design | Who accepts writes | What gets easier | What you must handle |
|---|---|---|---|
| Single-leader | One primary | One order for all writes, no conflicts | The primary is the bottleneck and the single point to replace; replicas lag |
| Multi-leader | Several leaders | Writes near the client, writes while regions are cut off | Conflicting writes to the same record |
| Leaderless | Any replica, by quorum | No failover step, writes keep working | Choosing R and W, reconciling versions |
What happens if a replica falls behind?
Section titled “What happens if a replica falls behind?”Nothing breaks, and that is the danger. The replica keeps applying writes in order, so every state it shows is a state the primary once had. It is not wrong, it is old. Any read that lands on it returns the data as it was some moment ago, and a user who wrote something a second ago may not find it. The next lessons are about how long that moment is, how to bound it, and what happens when the primary itself is lost.
- A primary accepts every write and orders it. Replicas apply the writes in the same order, so a replica is behind, never different.
- Replicas add read capacity and a second copy. They add no write capacity, because every replica applies every write.
- Lag can be read as time or as a number of writes: 100 ms behind one write every 100 ms is one write.
- Multi-leader replication accepts writes in several places and has to resolve conflicts. Leaderless replication writes to and reads from several replicas by quorum and has to reconcile versions.
Your progress
Built by GhaziAlibi.
© ShardCraft. All rights reserved.