Skip to content
WebsiteHosting.com

troubleshooting Data & storage

Replica lag and read-your-writes behavior

Your next read can reach a replica before it replays your acknowledged write. To establish read-your-writes, check that replica against the session’s write token and use a snapshot new enough to include the write.

Latest recorded source check · P0240

What it means

A replica can return the old value after a successful save. The primary may have acknowledged the write while the replica serving your next read has not yet replayed it. PostgreSQL 18: standby progress

What matters is which standby the acknowledgment covers and what it has done. For example, with one required synchronous standby in PostgreSQL 18, remote_write waits for a write to the standby’s operating system; remote_apply waits for replay on that standby. Neither tells you the replay position of a different replica selected for the read. PostgreSQL 18: synchronous commit

A session can carry a token marking its acknowledged write position, w. A position-gated read compares that token with the selected replica’s replay position, r. If the positions belong to the same replication history and the read takes a fresh snapshot after replay reaches w, then a replay position r at least as large as w establishes visibility of that write for this read.

Try it

This local simulation uses synthetic positions. It does not route a query, wait on a server or change a database.

Replica lag and read-your-writes behavior

Advance synthetic primary and replica positions; route a subsequent read and compare unrestricted reads with a declared session-position condition.

Simulation

Local simulation only: no device, service, stored object or database is changed, and no real operation is authorized.

Modeled outcome: read may be stale

The replay position does not cover the known write; unrestricted reading and position-gated reading have different outcomes.

Replica lag and read-your-writes behavior: synthetic scenario inputs

Scenario: Starting example

Each preset replaces all fields. Editing any field preserves the other values exactly, including invalid entries.

Known write position → replay condition → read outcome

Visibility condition established
No
Replay gap
3 position units, not seconds
Read-routing outcome
read-may-be-stale

Evaluated evidence

Known write position (position units)
12
Replica replay position (position units)
9
Read-routing policy
unrestricted
Position-wait state
waiting
Replication-history comparison
same
Read snapshot is sufficiently new
yes

Synthetic position units are not elapsed lag or byte throughput. This tests visibility of one known write, not the value after later overwrites, global linearizability or failover correctness. No routing or waiting occurs outside the model.

About this model

Every input is local synthetic evidence. Numbers stay as raw text until this model validates them. The shown evaluated values come from the same result as the explanation.

Presets replace the entire scenario. Reset restores all starting fields, scenario selection and model state; it does not change the open or closed state of this help or article FAQ.

No external request, device command, stored-input history or measurement is performed.

  1. Start with Known write position 12, Replica replay position 9 and Read-routing policy unrestricted. The history is same and the snapshot is fresh. The replay gap is 12 − 9 = 3 position units—not three seconds or a measure of throughput. The outcome is read-may-be-stale: the policy permits the read even though replay has not covered the write.
  2. Choose Gate the read on replay position. With the same gap, the policy is now gated and the outcome is wait. Change Position-wait state from waiting to expired: the outcome is stop-without-read, not a successful read. With a fresh snapshot and the same history, setting replay to 12 instead satisfies the visibility condition.
  3. Choose Inspect the evidence boundary. Both positions are 12, but Read snapshot is sufficiently new is no. A query using an older snapshot does not gain a new view just because replay caught up. The model reports unknown, not visibility established.

Positions accept whole-number decimal strings from 0 through 1000000. A fractional write token such as 12.000000000000000001 is invalid, not rounded to 12. By contrast, unknown is a valid entry for Replica replay position: the gap is unknown, not zero. Reset restores all six starting inputs and the default scenario, returns the step and virtual time to zero, and clears history, errors and the comparison.

In practice

For a read that must reflect a save, keep the write token with the session and check the replica that will actually serve the query. If its comparable position is behind, wait for replay or choose a read path you can verify covers the write. If the wait expires, stop rather than serve an unverified read as current. If the position is missing or the history is different or unknown, there is no usable comparison. If replay covers the token but the query already holds an older snapshot, check the snapshot rather than waiting for more replay.

Passing the gate does not promise the original value after a later overwrite, a global order for all reads and writes, or correctness after failover. Read-your-writes is a per-session decision: use this write’s token, this replica’s history and this read’s snapshot—not an average lag number.

Frequently asked questions

Editorial questions based on the listed sources and explicitly labeled local examples.

Why can a successful write be followed by old data?

The write may be acknowledged before a replica has replayed it. An unrestricted read routed to that replica can therefore precede the known write position, even though the primary accepted the write.

Does low average lag guarantee read-your-writes?

No such guarantee is modeled. The gate compares this session’s required write position with the selected replica’s replay position, and additionally requires the right lineage and a fresh read snapshot. An average is not that per-read proof.

What if replica position is unknown?

The gated read remains unknown or waiting; the model does not treat a missing replay position as zero lag. A timeout yields a stopped read, not invented evidence that the replica caught up.

Does a session token establish global consistency?

No. The declared token condition covers a particular write’s visibility on a compatible read path. It does not define a global order, a consistent snapshot across all queries or behavior on another timeline.

Sources & model boundaries

Primary references used for this explanation. The interactive example is an educational model, not a live check.

  1. Log-shipping standby servers

    Streaming replication; synchronous replication; compatibility. Standby progress and acknowledgment policies are separate; asynchronous progress can lag the primary. Physical log shipping has version/platform constraints.

    Checked .

  2. Write-ahead log runtime configuration

    synchronous_commit. remote_write waits for standby OS write, not merely receipt; on waits for flush and remote_apply for replay. Empty synchronous_standby_names changes waiting semantics.

    Checked .

Report an issue. Include the article URL and ID, not passwords or access keys.