Pcornel
Browse topics
Options
Recent
Favorites
Databases · Technical reference

Read Replicas and Replication Lag

Direct answer

Read replicas serve read queries from asynchronous copies; replication lag means they may return stale data.

When to use this

Use it when offloading reads or when users see stale data.

First 30 seconds

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.

Operational verification

write on primary → shipped → applied on replica after lag → read

Context & applicabilityCommon mistakes · platform/version boundaries
Platform / version applicability
MariaDB / MySQL / PostgreSQL / Microsoft SQL Server concepts
applies
Structured applicability is explicit Topic metadata; it does not infer compatibility beyond the stored rows.
At a glance

Operational reference

Fast context before you go deeper.

PlatformMariaDB / MySQL / PostgreSQL / Microsoft SQL Server concepts
Scope / prerequisiteUse it when offloading reads or when users see stale data.
What next?2 suggested next steps
Evidence0
On this pageJump to a specific section
Operational workflow

Verification & Troubleshooting

Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.

2 steps Observe → verify → isolate → change minimally → repeat the original test.
ObservedRecord what the original test actually shows

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.

Key points

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.

Key checks
  • now()-pg_last_xact_replay_timestamp() — first check for this topic
  • psql -c "SELECT now(), version();" — PostgreSQL reachable and version
  • mysql -e "SELECT NOW(), VERSION();" — MariaDB/MySQL reachable and version
  • sqlcmd -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

DatabaseLag metric
PostgreSQLreplay_lag in pg_stat_replication, now() - pg_last_xact_replay_timestamp()
MySQLSeconds_Behind_Source (approximate), heartbeat tables (pt-heartbeat)
SQL Server AGsecondary_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:

ApproachHow
Sticky primaryRoute a user's reads to the primary for a few seconds after they write
Wait for positionRecord 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 applysynchronous_commit = remote_apply for that session/transaction
Accept stalenessFine 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.

Source basisSource attribution states what this section is based on; it is not independent proof that the current environment matches it.

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
Terminology layer

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.

Aliases that resolve here
read replicas and replication lagread replica lagreplication lag monitoring
Confirmation

Confirm action

Continue?