How it works
Replication lag is the delay between a commit on the leader and its application on a follower. It is often milliseconds, but a load spike, a slow network, or a long-running transaction can stretch it to seconds or minutes, and an asynchronous follower offers no upper limit. Copies do converge once writes pause, which is what “eventually consistent” means, but the wait has no promised length.
Suppose someone saves a new shipping address, the page reloads, and the read goes to a follower 2 seconds behind. They see the old address, assume the save failed, and try again. Read-your-writes consistency prevents this: a user always sees their own committed changes. It promises nothing about other users’ changes.
Three approaches are common. Route by ownership: read your own profile from the leader and other people’s from followers. That fails when most data is editable by its viewer. Route by time: send a user to the leader for, say, 30 seconds after any write. That is simple, but it only guesses at lag. Track a position: after a write, keep its place in the replication log, such as an LSN in Postgres or a GTID in MySQL. If the write committed at 9,150 and a follower has applied up to 9,120, the read waits or goes to the leader.
All three push some reads back to the leader, which eats into what replicas saved. Multiple devices add a wrinkle: a position kept in one browser does not help the same user’s phone, so it must live on the server.
Your notes app reads from followers in three regions. Each session cookie holds the position of that device’s last write, and read-your-writes works on one device. Users still report that a note saved on a laptop is missing on their phone for a few seconds. What would you change, and what does that cost?
Keep the last-write position per user rather than per device, in a small shared store that every region can reach. Each read then compares the user’s position with the follower’s, and waits or goes to the leader when needed. The cost is one extra lookup per read, plus a slow trip to a distant leader when no nearby follower has caught up. Entries can expire once all followers pass them, which keeps the store small.
Use this as a guide for self review; written answers are not graded.Keep in mind
- Lag is usually small, but an asynchronous follower has no upper limit on it.
- Read-your-writes covers one user’s own changes and says nothing about anyone else’s.
- A log position gives an exact answer; a time window only guesses at lag.