LESSON 03 · Read ordering

Keep reads moving forward and in order

Two more anomalies appear when reads hit different followers or shards, and each has a specific fix.

SYSTEM VIEW

The replication path

Diagram view
Read from—
Page shows—

Watch a comment vanish when a refresh lands on a slower follower, then a refund show up before its order, and the fix for each.

  1. Someone posts a comment, and the leader commits it at position 4,210 at 0 s. Follower A, 1 s behind, has it at 1 s. Follower B, 5 s behind, won’t have it until 5 s. A load balancer sends each read to either follower.

    Follower A
    1 s behind
    Follower B
    5 s behind

    What changed

    • Leader commits 4,210
    • Follower A has it at 1 s
  2. A reader opens the thread, and the load balancer sends the read to Follower A. A has had the comment since 1 s, so at 3 s it answers with 1 comment.

    What changed

    • Read goes to Follower A
    • Page shows 1 comment
  3. At 4 s the reader refreshes, and the load balancer picks Follower B. B hasn’t applied 4,210, which reaches it only at 5 s, so the page shows 0 comments. The reader saw newer data, then older.

    At 3 s, from A
    1 comment
    At 4 s, from B
    0 comments

    What changed

    • Refresh goes to Follower B
    • Page shows 0 comments
    • B has 4,210 at 5 s
  4. Replay it with each reader pinned to one follower by a hash of the user ID. This reader maps to Follower A, so the refresh at 4 s goes there too and still shows 1 comment. That’s monotonic reads: maybe stale, never backward.

    Pinned by
    hash of user ID
    Reads
    never go backward

    What changed

    • Reader pinned to Follower A
    • Refresh shows 1 comment
  5. Replay it with Follower A failing at 3.5 s. The reader moves to Follower B, which is further behind and would show 0 comments. But the refresh carries 4,210, the last position the reader saw, so B holds the read until it applies 4,210 at 5 s, then answers 1 comment.

    Refresh carries
    position 4,210
    Without it
    0 comments

    What changed

    • Follower A fails
    • Reader moves to Follower B
    • B holds the read until 4,210
  6. Now take two shards, each with its own log. Order 77 is created on the orders shard at 0 s, and its refund goes to the payments shard at 1 s. The orders follower is 4 s behind; the payments follower is current.

    Order 77
    orders shard
    Its refund
    payments shard

    What changed

    • Order 77 on the orders shard
    • Its refund on the payments shard
  7. Payments answers the support dashboard with the refund at 2 s, then the dashboard asks the orders follower for order 77 at 3 s. That follower applies order 77 only at 4 s, so the dashboard shows a refund for an order that doesn’t exist.

    From Payments
    refund for 77
    From Orders
    no order 77

    What changed

    • Dashboard reads both shards
    • Refund shows, order doesn’t
    • Order 77 arrives at 4 s
  8. Replay it with both writes keyed on order ID 77, so they share the orders shard’s log, applied in order. A read at 4 s shows the order without its refund. That’s a consistent prefix: maybe stale, never effect before cause. Otherwise, track dependencies or read cross-shard snapshots, as Spanner does, at a coordination cost.

    Order 77
    applied at 4 s
    Its refund
    applied at 5 s

    What changed

    • Both keyed on order ID 77
    • One log, applied in order
    • Dashboard: order, no refund yet

Space plays · ← → step · Home resets

THE IDEA

How it works

Lag can confuse even a user who never writes. Suppose follower A is 1 second behind, follower B is 5 seconds behind, and a load balancer picks either per request. A reader opens a comment thread through A and sees a comment posted 3 seconds ago. They refresh, land on B, and the comment is gone. Monotonic reads rule this out: once a reader has seen some state, later reads will not show an older one. The data may still be stale, but it does not move backward.

The usual fix is to pin each reader to one follower by hashing the user ID. If that follower fails, the reader moves to another that may be further behind, so the guarantee can break unless the reader also carries the log position it has seen.

Consistent prefix reads address a different anomaly: seeing an effect before its cause. A single log applied in order cannot do this, but shards have separate logs and no shared order. Say “order 77 created” goes to the orders shard and “refund for order 77” to the payments shard. If the orders follower is 4 seconds behind and the payments follower is current, a support dashboard shows a refund for an order that does not exist.

The simplest fix puts related writes on one shard, here by keying both on order ID. Otherwise, track dependencies and hold an event back until its cause is visible, or use a database that reads consistent snapshots across shards, as Spanner does, at a coordination cost.

DECISION POINT

A task-board product shards events by author. Ana creates a card, then Ben moves it to “done”. Viewers read from followers of both shards, and sometimes a board receives the move before the card exists and shows an error. Would you re-shard by board ID or add dependency tracking?

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

Keep in mind

  • Monotonic reads stop a reader seeing newer data and then older data.
  • Pinning a reader to one follower gives monotonic reads until that follower fails.
  • Consistent prefix keeps cause before effect; shards with separate logs can break it.
CHECK YOUR UNDERSTANDING

Put it into practice.

1 OF 3
DiagnoseQUESTION 01

A page counts comment rows. A reader refreshes and sees 12, 14, 13, 14, though no comment was deleted. What is the 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.