Followers are always a little behind. What users see because of it, and how to cope.
Replication lag is the delay between a change being made on the leader and the same change showing up on a follower. In that gap, reading from the follower returns old data.
In a leader-follower setup, the leader writes each change to its log and ships the log to the followers. A follower then applies the changes, one after another. This takes time: the message must travel, the follower must read it, and then it must do the work.
If the leader does not wait for the followers (asynchronous replication), it confirms the write to the user straight away. So for a short moment the user has been told "saved", but a follower has not seen the change yet. A read that lands on that follower gets the old value. This is replication lag, and the stale answer it gives is an example of eventual consistency: the follower will catch up, just not instantly.
| Problem | What the user sees | Plain-English guarantee that fixes it |
|---|---|---|
| Read-your-writes failure | I posted a comment and it is not there | Read-your-writes: you always see your own changes |
| Monotonic-reads failure | I saw the comment, reloaded, and it was gone | Monotonic reads: time never goes backwards for you |
| Consistent-prefix failure | A reply shows up before the question it answers | Consistent prefix: you see writes in the order they happened |
| Cause | Why it slows a follower |
|---|---|
| Heavy write load | The follower must apply as many changes as the leader, and may be slower |
| One huge transaction | A single big change takes a long time to replay |
| Long queries on the follower | A big report can hold up the work of applying changes |
| Network distance or trouble | Changes arrive late, or in bursts |
| A weaker follower machine | It cannot apply changes as fast as the leader makes them |
Each reload goes to whichever replica the balancer picks (here: a fixed rotation, follower 1 then follower 2).
Reload 1: no rule, so whichever replica was picked answers. Reload 2: no rule, so whichever replica was picked answers. A simplified story with fixed lags and a fixed rotation between followers (no random numbers). Real lag changes from second to second, and the log position is an illustrative number.
| Term | Meaning in easy words |
|---|---|
| Replication lag | How far a follower is behind the leader, in time or log entries |
| Asynchronous replication | The leader confirms a write without waiting for followers |
| Eventual consistency | Followers will catch up if writes stop, but not instantly |
| Stale read | A read that returns old data |
| Read-your-writes | You always see your own recent changes |
| Monotonic reads | You never see data go back to an older state |
| Consistent prefix | You see writes in the order they happened |
| Log position (LSN) | A number that marks how far along the replication log is |
| Semi-synchronous | The leader waits for one follower before confirming |
| Read replica | A follower used to answer reads |
| Sticky replica | Sending one user's reads to the same replica every time |
| Choice | We gain | We lose |
|---|---|---|
| Read from any replica | Maximum read scale | Stale reads and time going backwards |
| Always read from the leader | Always fresh | No read scale, and the leader gets busy |
| Leader for a short time after a write | Read-your-writes, simple to build | A guess about how long the lag lasts |
| Read by log position | Exact guarantee, still uses replicas | More code and a position to carry around |
| Sticky replica per user | Monotonic reads | A slow replica hurts its users, and failover must move them |
| Synchronous or semi-sync | No stale reads for those writes | Slower writes and lower availability |