Definition · Replication lag and a stale readReplication lag is the delay between a write committing on the leader and being visible on a follower. A read of that follower issued t after the write is stale if it returns the value from before the write, which happens exactly when the lag exceeds t. So P(stale at t) = P(lag > t).
Lag is not a constant and it is not well described by its average. It is a distribution with a long right tail, produced by whatever the follower is occasionally busy doing — a compaction, a slow disk, a network hiccup, a burst of writes it is still applying.
follower lag, as ms:weight over a total weight of 1000
lag (ms) weight P(lag ≤ this) P(lag > this)
10 500 1/2 1/2
25 300 4/5 1/5
50 120 23/25 2/25
120 60 49/50 1/50
250 15 199/200 1/200
400 5 1 0
a read 50 ms after the write is stale with probability 2/25 = 8%
Every figure in that table is a sum of weights divided by 1000. There is no model being fitted and no parameter being estimated: if you can measure the lag, the staleness probability at any delay is a column of the same table.
Why the mean cannot answer the question
mean lag = (10·500 + 25·300 + 50·120 + 120·60 + 250·15 + 400·5) / 1000
= 31 450 / 1000 = 629/20 = 31.45 ms
P(lag > 31.45 ms) = (120 + 60 + 15 + 5)/1000 = 200/1000 = 1/5
wait for the mean one read in five is stale
wait 250 ms one read in a hundred
wait 400 ms none, on this distribution
“Average lag is fifty milliseconds, so reads after fifty milliseconds are fine” is the sentence this lesson exists to break, and the break is not subtle: at the mean of this distribution, twenty per cent of reads are wrong. Half the mass sits at 10 ms and pulls the average down while the 120, 250 and 400 millisecond events — the ones that actually produce the complaint — sit far above it.
Example · What eight per cent costs at twelve thousand reads a secondA service serving 12 000 follower reads a second with a read-after-write delay of 50 ms produces 12 000 × 2/25 = 960 stale reads every second. That is the number to put in front of anyone who has described the problem as intermittent. It is not intermittent; it is eight per cent, all day, and the reason it is reported as intermittent is that most reads are not of something the reader just wrote.
The repairs are all versions of the same idea: make the read wait, or make it go somewhere that has the data. Route a client's reads to the leader for a while after it writes; pin a session to one follower so monotonic reads hold even though read-your-writes may not; or carry the write's version with the client and have the follower wait until it has reached that version. Each costs something, and this distribution is what says how much of it you need.