Replication Lag and Failover
Follow a committed change across an asynchronous standby failure.
database, postgresql, replication, failover, wal, availabilityA replica can be behind
In physical streaming replication, a standby receives and replays WAL from the primary. Asynchronous replication can acknowledge a primary commit before the standby receives it, and standby reads may be stale. This is a trade-off between latency, availability, and potential data loss.
SQL illustration
On an authorised PostgreSQL standby, this read-only diagnostic reports its
last replayed WAL location, or SQL NULL when no WAL has been replayed
during recovery:
SELECT pg_last_wal_replay_lsn();
An LSN by itself does not establish zero lag, durability, data correctness, or safe promotion. Compare appropriate positions and timestamps with the primary using version-specific monitoring and account for connection and replay state. In the prepared scenario below, commit 1 is replayed, commit 2 is acknowledged on the primary but never reaches the standby, and the primary fails with the missing WAL unavailable. After fencing the former primary, the promoted standby has commit 1 but not commit 2.
See the reasoning
Trace an asynchronous failover
Compare what was acknowledged on the primary with what reached the standby. This is a prepared scenario, not live SQL.
Both have the earlier commit
- Primarycommit 1
- Standbycommit 1 replayed
The standby has replayed the first fictional commit.
Promote deliberately
Plan failure detection, fencing, promotion, client routing, reconciliation and rebuilding the old primary. Without fencing, two writable primaries can diverge. PostgreSQL does not itself provide every piece of failure detection and orchestration. Synchronous replication can reduce some loss windows but depends on configuration, acknowledged stages, and failure assumptions; it can increase latency or affect availability. Read-your-writes requires an explicit routing or replay strategy. Logical replication has different scope and limitations from physical WAL replication. Neither replaces a tested, independent backup: mistakes can replicate too.
Reflect on the concept
Four fixed questions to reinforce the reasoning. Answers are not saved or scored.
