Q&A-2Explain read replicas: what they solve and what they break.
Answer
Core (first 90 seconds). A read replica is a second copy of the database that continuously applies the primary's write log and serves read-only queries. It solves read scale and workload isolation: N replicas give roughly N× read capacity, and moving reporting off the primary removes CPU contention and buffer-pool eviction. An analytical scan over a large table can push the hot pages the OLTP workload depends on out of cache. It does not solve write throughput: every write still goes to the primary and is shipped to every replica, so each replica adds log-shipping work and, under synchronous replication, commit latency. What replicas break is consistency. Reads can be stale, and the failures are user-visible correctness bugs, not slow queries.
Mechanism. The primary writes a log (PostgreSQL WAL, MySQL binlog) and ships it. Asynchronous replication returns the commit to the client before any replica has applied it, so lag is unbounded in principle. Synchronous replication bounds it: PostgreSQL synchronous_commit = on (the default) only flushes local WAL; remote_write waits for the standby to write, and remote_apply waits until it has replayed. That is what makes a read on that standby see the write. synchronous_standby_names = 'ANY 1 (a, b)' gives quorum. MySQL semi-sync waits for one replica's ACK, but rpl_semi_sync_source_timeout defaults to 10000 ms; after that the source silently reverts to async, so the durability guarantee disappears exactly when a replica is in trouble.
Measuring lag. On the primary:
SELECT application_name, state, sent_lsn, replay_lsn, replay_lag
FROM pg_stat_replication;
-- replica-1 | streaming | 0/1A2B3C40 | 0/1A2B3B00 | 00:00:00.041replay_lag (PostgreSQL 10+) is the time between the primary flushing WAL and the standby reporting it replayed; 41 ms here is a typical same-region cross-AZ figure, not a guarantee. On the standby, pg_last_wal_replay_lsn() and pg_last_xact_replay_timestamp() give the same picture, and pg_wal_lsn_diff('0/1A2B3C40','0/1A2B3B00') returns 320: the byte gap, meaningful only alongside a write rate. MySQL's Seconds_Behind_Source (in SHOW REPLICA STATUS, 8.0.22+) is a trap: it is NULL when the replica SQL thread is idle and is derived from event timestamps, so it can read 0 while a long transaction is still applying.
What breaks, and the fix.
| Anomaly | Symptom | Fix |
|---|---|---|
| Read-your-writes | User saves, next read hits a lagging replica, sees the old value, saves again | synchronous_commit = remote_apply; MySQL WAIT_FOR_EXECUTED_GTID_SET(); pin that session to the primary for a window; or return the written value from the write response |
| Monotonic reads | Two requests hit replicas at different positions; data appears to go backwards | Sticky routing per session, or track a per-session LSN/GTID and read only replicas that have reached it |
| Decision-informing read | Balance checked before a withdrawal, uniqueness checked before an insert | Must go to the primary; a stale read there is a correctness bug, not a display glitch |
| Failover | An async replica promoted is behind; the last commits are gone | Accept RPO > 0, or run synchronous replication with quorum |
The failure mode people miss. A long query on a standby conflicts with WAL replay. PostgreSQL cancels it after max_standby_streaming_delay (default 30s) with ERROR: canceling statement due to conflict with recovery; setting hot_standby_feedback = on avoids the cancellation but makes the primary retain dead rows, causing bloat and delayed vacuum. So a heavy report routed to a replica either fails or drives lag up. Alert on replay_lag, or ReplicaLag (seconds) in CloudWatch for RDS, AuroraReplicaLag (milliseconds) for Aurora. A replica silently minutes behind turns a correct routing decision into a wrong one with no code change.
Curated: · Written: · Reviewed:
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.