Replication copies data to standby servers for read scaling and high availability. Postgres streaming replication ships WAL records to replicas.
Primary and standby concept
Primary accepts writes; standby replays WAL for read-only queries (hot standby) or failover promotion. Managed services automate failover.
Replication lag check
SELECT client_addr, state, sent_lsn, replay_lsn,
pg_wal_lsn_diff(sent_lsn, replay_lsn) AS lag_bytes
FROM pg_stat_replication;Run on primary when replication is configured—empty on single-node sandboxes.
Practice: Run on a local Postgres instance you own. Avoid changing production cluster settings.
Read replicas in apps
Route analytics to replicas; keep writes on primary. ORMs need explicit read replica routing—eventual consistency applies.
Important interview questions and answers
- Q: WAL role in replication?
A: Standbys replay WAL to stay consistent with primary. - Q: Logical vs physical replication?
A: Physical streams byte-level changes; logical decodes row changes for selective subscriptions.
Self-check
- What does pg_stat_replication show?
- Why route heavy reports to replicas?
Tip: Route heavy reads to replicas; keep writes on the primary.
Interview prep
- WAL in replication?
Standbys replay WAL from primary.
- Read replica use?
Offload read-heavy reporting from primary.