Overview
A read replica receives the primary’s committed changes and serves SELECT queries. Add one when read traffic, reports, or analytics compete with writes on the primary. Route read-after-write paths to the primary, because replicas lag (replication-lag).
Rules
- PostgreSQL offers physical streaming replication (a byte-level copy of the write-ahead-log, with hot standbys serving reads) and logical replication (row-level changes for selected tables). Replication is asynchronous by default, so replica reads can be stale.
- Do not send read-your-writes, checkout, or inventory reads to a replica unless the app handles lag: pin the user to the primary briefly after a write, or use
synchronous_commit = remote_apply. - A long query on a standby can be canceled by replay conflicts (
max_standby_streaming_delay, default 30 s).hot_standby_feedbackprevents that but delays cleanup and can bloat the primary. - Replicas scale reads only. If writes are the bottleneck, see sharding.
Example: an e-commerce site sends catalog searches to a replica (200 ms of staleness is fine) and checkout queries to the primary (stock must be current).
Related
- write-ahead-log: the stream replicas apply.
- replication-lag: why replica reads go stale.
- base: the consistency model replica reads exhibit.
- sharding: when replicas are not enough.
- postgres-replication: setup and monitoring.