Copies that are a little behind
After this: Say what a read sees while a change is on its way, and fix it for the user who made the change.
On this page · 4 sections · 3 checks
Ana changes her display name from "Ana" to "Ana B" and presses Save. The page reloads, and it still says "Ana". She refreshes a moment later, and now it says "Ana B". Nothing is broken. The copies from lesson 2 are the reason.
Copies take time to arrive
In lesson 2 every change reached the replicas at once. In a real system the primary stores the change, answers the user, and then sends the change over the network to each replica. Answering first and copying afterwards is called , and it is a common default. The copying takes time. The delay between a write on the primary and the same write on a replica is the .
During that delay the replica still holds the old value, and it answers reads with it. A read that returns an older value than the primary holds is a .
A worked example
The replica lags 200 ms behind the primary. Ana's name is "Ana" on every copy.
- At 0 ms Ana writes "Ana B". The primary stores it, and the copy sets off. It arrives at 0 + 200 = 200 ms.
- At 50 ms she reads her name from the replica. The copy has not arrived: "Ana", a stale read.
- At 150 ms she reads again. Still before 200 ms: "Ana", stale again.
- At 250 ms she reads a third time. The copy arrived at 200 ms: "Ana B".
The rule: a read sees a write when it comes at the write's time plus the lag, or later. Here, 2 of Ana's 3 reads were stale.
- Replication · Read from replicaPress Run it and step through. Ana changes her name at 0 ms and reads it at 50, 150 and 250 ms. Watch the copy travel from the primary to the replica, and read what each of her reads returns. Then switch the policy to read your own writes and run it again.
Press Run it, then step through and watch the requests move.
A fix: read your own writes
Most users never notice a delay of 200 ms in someone else's change. The person who notices is the one who made the change and is looking at it straight away. So the fix aims at that person.
For a short time after a user's own write, send that user's reads to the primary, which already has the change. After that time, their reads go back to the replicas. This is called .
Switch the policy above to read your own writes and run it again. With a window of 1 second (1,000 ms), all three of Ana's reads come within 1,000 ms of her write, so all three go to the primary. They all return "Ana B": 0 stale reads, and 3 reads on the primary.
The fix has costs. Those reads add load to the primary, which is why the window is kept short. The window must also be longer than the lag: if the lag grew past 1 second, a read soon after the window could be stale again. And it helps only Ana: her friend Ben, reading her name from a replica at 50 ms, still sees "Ana" until the copy arrives at 200 ms.
Check yourself
The replica lags 200 ms behind the primary. Ana changes her name from "Ana" to "Ana B" at 0 ms. Her next read, at 120 ms, goes to the replica. What does it return?
Users complain: they change their profile, reload the page, and see the old profile. Which fix does this lesson use?
Read your own writes is on, with a lag of 200 ms. Ana changes her name to "Ana B" at 0 ms. Her friend Ben reads her name at 50 ms from a replica. What does Ben see?
- Say what replication lag is: the time a change takes to travel from the primary to a replica.
- Work out what a read sees: it sees a write when it comes at the write's time plus the lag, or later; before that it is a stale read.
- Fix stale reads for the user who made a change: for a short time after their write, send their reads to the primary (read your own writes).
- Name the fix's limits: it adds reads to the primary, and other users can still see the old value until the copy arrives.