Insights · Data
PostgreSQL high availability: replication, failover and backups that restore
By the Cystem X Engineering Team - Published - 5 min read
In this article
PostgreSQL is trusted for critical systems, but availability does not come by default. A single server is a single point of failure, and a backup that has never been restored is a hope. A resilient design combines replication, automated failover, sensible client connection handling and backups that are regularly proven.
Start with requirements
Decide acceptable downtime and data loss for each database, as recovery time and recovery point objectives. They determine whether you need synchronous replication, how many replicas and which failover mechanism.
Not every database needs the most expensive design.
Use streaming replication
Physical streaming replication ships write-ahead log records to standby servers that can take over. Asynchronous replication adds minimal latency but can lose recent transactions on failover. Synchronous replication confirms commits on a standby, reducing loss at the price of latency and dependency on that standby.
Use replicas also for read scaling, with attention to replication lag.
A backup is real only when you have restored it.
Automate failover
Manual failover is slow and error-prone. Tools such as Patroni use a distributed consensus store to elect a leader, promote a standby and reconfigure replicas, avoiding split-brain. Run the consensus cluster on separate nodes with an odd number of members.
Test failover regularly, including network partitions and node failures.
Handle client connections
Applications must find the new primary quickly. Use a virtual IP, DNS with short timeouts or a proxy layer, and connection pooling with a tool such as PgBouncer to control connection counts and absorb reconnect storms.
Make applications retry safely and handle transient errors.
Back up properly
Combine base backups with continuous WAL archiving to allow point-in-time recovery. Tools such as pgBackRest provide compression, encryption, retention and parallel restore. Store copies in a separate location, including an immutable copy.
Replication is not backup, because it copies mistakes and deletions instantly.
Restore, rehearse and verify
Regularly restore backups to a test environment, check data integrity and measure how long recovery takes. Compare the result to your objectives and fix gaps.
Automate restore tests so they happen without relying on memory.
Operate and monitor
Monitor replication lag, WAL growth, disk space, connection counts, long transactions, vacuum activity and backup success. Plan upgrades with minimal downtime using replicas or logical replication, and keep security patches current.
Document runbooks for common failures.
Key takeaways
- Set recovery objectives before choosing the design.
- Use streaming replication and decide on sync versus async deliberately.
- Automate failover with a consensus-based manager such as Patroni.
- Use pooling and a stable connection endpoint.
- Combine base backups and WAL archiving, and test restores routinely.
Sources and further reading
This article reflects general practice and publicly available guidance. Teams should consult primary sources, including the official PostgreSQL documentation on high availability and backup, and the Patroni and pgBackRest documentation, and adapt the guidance to their own environment, risk appetite and legal obligations.
Keep reading
More engineering guidance from the Cystem X team.
Data
Data lakehouse architecture: open table formats and when to adopt them
Open table formats bring transactions and governance to lake storage. Architecture, layers and trade-offs.
Read articleData
Dimensional modeling or data vault: choosing a warehouse design
Star schemas for consumption, Data Vault for integration: how to choose and combine them.
Read articleData
Data loss prevention in practice: classify first, then control
DLP fails without classification. Define classes, find the data, then apply proportionate controls.
Read articleData & Databases
Make PostgreSQL resilient and recoverable.
We design, build and operate highly available PostgreSQL platforms with tested recovery.
Explore Data & Databases