Use it when offloading reads or when users see stale data.
Read Replicas and Replication Lag
Read replicas serve read queries from asynchronous copies; replication lag means they may return stale data.
1. Lag: PG now()-pg_last_xact_replay_timestamp() / MySQL Seconds_Behind_Source / AG secondary lag.
2. Cause: heavy writes, slow replica I/O, long queries.
3. Read routing respects lag?
4. Fix bottleneck.
5. Monitor.
write on primary → shipped → applied on replica after lag → read
Context & applicabilityCommon mistakes · platform/version boundaries
Operational reference
Fast context before you go deeper.
Verification & Troubleshooting
Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.
Use the operational verification above to establish the expected state.
Capture the concrete output, symptom, packet, log or counter before changing anything.
Step 1 How to verify Verify boundary
Test a write followed by an immediate read through the replica path. Monitor lag during batch jobs.
Step 2 Common failures Narrow the fault
Seconds_Behind_Source shows 0 when replication is stopped on some versions; check thread status too. Replica lag alarms that ignore stopped replication miss outages. Big batch deletes create long lag.
Route only lag-tolerant reads to replicas. Monitor lag and remove lagging replicas from the pool. Long queries on replicas can delay replay or be cancelled.
now()-pg_last_xact_replay_timestamp()— first check for this topicpsql -c "SELECT now(), version();"— PostgreSQL reachable and versionmysql -e "SELECT NOW(), VERSION();"— MariaDB/MySQL reachable and versionsqlcmd -S <server> -Q "SELECT @@VERSION"— SQL Server reachable and version
ReferenceWhat it is1 section · depth on demand
How it works
Replicas apply changes in commit order. Large transactions or heavy replica read load slow apply. Applications often route reads that must see recent writes to the primary.
ReferenceImportant terms & boundaries3 sections · depth on demand
Measuring and handling lag
| Database | Lag metric |
|---|---|
| PostgreSQL | replay_lag in pg_stat_replication, now() - pg_last_xact_replay_timestamp() |
| MySQL | Seconds_Behind_Source (approximate), heartbeat tables (pt-heartbeat) |
| SQL Server AG | secondary_lag_seconds in sys.dm_hadr_database_replica_states |
Applications reading their own writes must read from the primary or wait for replicas to catch up. Causes of lag: long transactions, single-threaded apply, slow replica disks, network.
In depth
Read-your-writes problem
A user saves a form (write to the primary) and the next page reads from a replica that has not applied the change yet; the user sees old data. Ways to handle it:
| Approach | How |
|---|---|
| Sticky primary | Route a user's reads to the primary for a few seconds after they write |
| Wait for position | Record the commit LSN/GTID; read from a replica only after it has applied it (WAIT_FOR_EXECUTED_GTID_SET in MySQL; WAIT FOR LSN or polling pg_last_wal_replay_lsn() in PostgreSQL) |
| Synchronous apply | synchronous_commit = remote_apply for that session/transaction |
| Accept staleness | Fine for reports and search pages; say so in the design |
Measuring lag correctly
-- PostgreSQL standby: time since last replayed transaction (misleading when the primary is idle)
SELECT now() - pg_last_xact_replay_timestamp() AS replay_delay;
-- PostgreSQL primary: per standby, in bytes and time
SELECT application_name, replay_lag,
pg_wal_lsn_diff(pg_current_wal_lsn(), replay_lsn) AS bytes FROM pg_stat_replication;
SHOW REPLICA STATUS\G -- Seconds_Behind_Source, and Replica_SQL_Running = Yes
Seconds_Behind_Source compares event timestamps; it is NULL when the applier is stopped and can read 0 for a replica that has stopped receiving. A heartbeat table (a row the primary updates every second, read on the replica) gives an end-to-end lag measurement that works the same on every engine.
Why lag grows
Single large transactions (a 10-million-row UPDATE replays as one unit), single-threaded apply, a replica with slower disks, long queries on the replica that block apply, or the replica serving more read load than it can handle while applying changes.
Common mistakes
- Reading from replicas without accounting for lag.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Terms & aliases
Hover a linked term for a quick definition; click it to open its glossary entry. Aliases are other names that find this Topic in search.