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.
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.
Local simulation only: no device, service, stored object or database is changed, and no real operation is authorized.
The replay position does not cover the known write; unrestricted reading and position-gated reading have different outcomes.
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.
- Start with Known write position
12, Replica replay position9and Read-routing policyunrestricted. The history issameand the snapshot is fresh. The replay gap is12 − 9 = 3position units—not three seconds or a measure of throughput. The outcome isread-may-be-stale: the policy permits the read even though replay has not covered the write. - Choose Gate the read on replay position. With the same gap, the policy is now
gatedand the outcome iswait. Change Position-wait state fromwaitingtoexpired: the outcome isstop-without-read, not a successful read. With a fresh snapshot and the same history, setting replay to12instead satisfies the visibility condition. - Choose Inspect the evidence boundary. Both positions are
12, but Read snapshot is sufficiently new isno. A query using an older snapshot does not gain a new view just because replay caught up. The model reportsunknown, 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.
- 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 .
- 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 .