Mental model
PostgreSQL high availability decouples durability and read scale by continuously shipping committed Write-Ahead Log (WAL) streams off the Primary writer down onto Standby nodes. Physical streaming replication mirrors exact byte-level disk block changes onto identical major-version read replicas, while Logical replication decodes WAL transactions cleanly into SQL row-level change events (INSERT, UPDATE, DELETE) for selective table sync or zero-downtime major upgrades—orchestrated safely via distributed consensus (Patroni + etcd/Raft) preventing dual-primary split-brain.
Learning outcomes
- Contrast Physical Streaming Replication (exact whole-cluster block clone) cleanly against Logical Replication (
PUBLICATION / SUBSCRIPTION) (cross-version partial table replication,requires Primary Key / REPLICA IDENTITY). - Compare Asynchronous (
RPO > 0, minimal write latency) cleanly againstsynchronous_commit = remote_write / on / remote_apply(RPO = 0zero data loss vs cross-AZ RTT write tax!). - Prevent split-brain data divergence during network partitions cleanly using Raft/etcd DCS leader leases (Patroni) alongside read-after-write replica lag hygiene cleanly.
Theory
Teams routinely treat two unmanaged Postgres servers plus an automated Bash ping-promote cron script cleanly as "High Availability"—virtually guaranteeing catastrophic split-brain divergence when a transient switch glitch causes both nodes simultaneously cleanly to accept customer writes! Reliable failover architecture relies strictly cleanly on quorum consensus (3+ etcd/Consul nodes across failure zones) holding an exclusive TTL leader lock: if an isolated old Primary loses contact cleanly with consensus quorum, Patroni immediately demotes it cleanly to read-only before promoting the most WAL-current standby cleanly!
| Replication / HA Mode | Replication Granularity & Version Coupling | Read Scalability & Schema Upgrades | Primary Commit Latency & RPO |
|---|---|---|---|
| Async Physical Streaming | Byte-for-byte identical cluster (same PG major version & CPU arch) | Instant read scaling across standbys (note replication lag!) | Zero standby RTT wait (fast commits); slight WAL byte RPO window cleanly on crash |
Sync Physical (synchronous_commit = on + standby_names) | Same whole-cluster mirror; primary waits cleanly for >= 1 standby WAL fsync | Zero committed transaction loss (RPO = 0 across AZ!) | Adds 1x standby AZ network RTT (~0.5–2ms intra-region) per write commit cleanly |
Logical Replication (pgoutput) | Selective tables (INSERT/UPDATE/DELETE), cross-major PG versions (v15 -> v17), separate indexes! | Enables zero-downtime major upgrades + analytics CDC sync cleanly | Zero DDL replication (schema migrations don't replicate cleanly automatically!); requires PK |
Watch closely for replication conflict cancels on Hot Standby replicas AND READ-YOUR-OWN-WRITES lag surprises: long SELECT reporting queries running on physical replicas block incoming primary VACUUM WAL cleanup unless max_standby_streaming_delay cancels slow queries cleanly (or hot_standby_feedback = on asks the Primary cleanly to hold old dead tuples temporarily cleanly!).
sql-- 1. Monitor streaming byte lag cleanly across connected replicas on PrimarySELECT application_name, client_addr, state, sync_state,pg_size_pretty(pg_wal_lsn_diff(pg_current_wal_lsn(), sent_lsn)) AS send_lag,pg_size_pretty(pg_wal_lsn_diff(pg_current_wal_lsn(), replay_lsn)) AS replay_lagFROM pg_stat_replication;-- 2. Verify standby replication role & last replayed timestamp cleanly on StandbySELECT pg_is_in_recovery() AS is_standby,pg_last_wal_replay_lsn() AS last_replayed_lsn,now() - pg_last_xact_replay_timestamp() AS replay_delay;-- 3. Logical replication setup (note: destination tables MUST have matching schemas ahead of sync)-- CREATE PUBLICATION ai_audit_pub FOR TABLE usage_ledger, prompt_versions;-- CREATE SUBSCRIPTION ai_audit_sub CONNECTION 'host=primary...' PUBLICATION ai_audit_pub;
Trade-offs
Using Patroni + 3-node etcd + Physical Streaming Replication delivers proven automated sub-30-second failover (with Watchdog kernel fencing stopping split-brain completely), transparent read-replica offloading, and zero DDL headache cleanly across Postgres upgrades using identical minor releases. Turning on cross-AZ synchronous_commit = on achieves zero data loss cleanly (RPO = 0) at a small 1–2 ms per-commit network penalty cleanly (while needing 2+ standbys ANY 1 (...) cleanly so one dead replica never halts primary writes forever!!). Meanwhile, Logical Replication earns its higher CPU decoding cost and lack of automatic DDL sync strictly when performing zero-downtime major-version upgrades or feeding selective operational tables cleanly into warehouses cleanly.
Failure modes and misconceptions
- Two-Node HA Without External Quorum Witness (
Split-Brain): Running Primary + Standby cleanly without an odd-numbered DCS (3x etcd/Consul) makes clean partition recovery impossible without human guesswork or conflicting writes cleanly! - Setting Single Standby
synchronous_standby_names = 'replica_1'Without Backup: If that single required sync replica reboots cleanly, every write cleanly on Primary hangs indefinitely cleanly (always configure ANY 1 (rep1, rep2) cleanly across >= 2 replicas!). - Assuming Logical Replication Syncs
CREATE/ALTER TABLEDDL or Sequences: Logical slots replicate solely table DML rows (requires external schema rollout first AND separate sequence sync cleanly during cutovers!). - Routing User Post-Write Redirects Straight to Async Read Replicas: Saving a user's profile on Primary then immediately fetching
/settingscleanly from a 150ms-lagging standby renders stale data cleanly (stick user writes briefly to Primary cleanly or verify LSN!!).
Decision scenario
A 2-AZ PostgreSQL database handles user updates alongside multi-second analytics reads on a replica; last month's standby reboot stalled all writes globally, while naive write failover cron scripts caused stale primary updates cleanly after a brief network flap! Deploy Patroni + 3-node etcd DCS across 3 AZs (with Watchdog / quorum fencing), expand synchronous standby pools cleanly via ANY 1 (sb1, sb2) (keeping RPO=0 without single-standby freeze!), cap max_slot_wal_keep_size safely, and route interactive read-after-write sessions cleanly back onto Primary for 2 seconds post-mutation cleanly!