LESSON 02 · Replication lag

Show users their own writes

A read from a lagging follower can hide a change the user just made, and there are several ways to prevent it.

SYSTEM VIEW

The replication path

Diagram view
Follower at—
Read from—
Page shows—

Watch a saved address seem to vanish when the reload reads a lagging follower, then replay it with fixes that show users their own writes.

  1. The user saves a new shipping address from their laptop. The leader commits it at position 9,150 in its replication log and answers at once. Replication is asynchronous, so the follower, 2 s behind, has applied only up to 9,120.

    What changed

    • Leader commits 9,150
    • Save confirmed
  2. The page reloads at 1 s, and the read goes to the follower. It has applied only up to 9,120, so it returns the old address. The user assumes the save failed and tries again.

    What changed

    • Reload reads the follower
    • Old address shown
    • User retries the save
  3. At 2.5 s the follower applies 9,150, 2 s after the commit. Copies converge once writes pause, which is what eventually consistent means, but the wait has no promised length: a load spike can stretch it to minutes.

    Lag
    often milliseconds
    Upper limit
    none

    What changed

    • Follower applies 9,150
    • Copies agree
  4. Replay it with the reload sent to the leader, which has 9,150, so the new address shows. By ownership, your own profile always comes from the leader; that fails when viewers edit most data. By time, the user’s reads go to the leader for 30 s after a write; that only guesses at lag.

    By ownership
    your own data
    By time
    30 s after a write

    What changed

    • Reload reads the leader
    • New address shown
  5. Replay it tracking a position: the save’s reply carries 9,150, and the reload’s read asks for at least 9,150. The follower has only up to 9,120, so it holds the read rather than answer with the old address.

    Postgres
    LSN
    MySQL
    GTID

    What changed

    • Save returns 9,150
    • Read needs 9,150
    • Follower holds the read
  6. At 2.5 s the follower applies 9,150 and answers the waiting read with the new address. A position gives an exact answer where a time window only guesses; when the wait would be long, the read goes to the leader instead.

    What changed

    • Follower applies 9,150
    • New address shown
  7. Replay it with the user’s phone. If only the laptop’s browser keeps 9,150, the phone’s read carries none and gets the old address. Kept per user on the server, 9,150 reaches every device; the follower is behind, so the read goes to the leader. Every fix sends some reads there, eating into what followers save. Try both.

    In the browser
    that device only
    On the server
    every device
    Cost
    reads back on the leader

    What changed

    • The user’s phone
    • The phone reads the address
    Position kept

Space plays · ← → step · Home resets

THE IDEA

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.

DECISION POINT

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?

Write a sentence or two to unlock the guidance. Committing first is what makes it stick.

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.
CHECK YOUR UNDERSTANDING

Put it into practice.

1 OF 3
DiagnoseQUESTION 01

Users sometimes see their old profile photo right after uploading a new one, and it gets worse during nightly batch jobs. What is the most likely cause?

Choose an answer. You’ll see the explanation once you get it right.

Next lesson

0 of 3 checkpoints answered correctly to complete this lesson.