postgresql 9.4 high availability topology
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
High availability (HA) in PostgreSQL refers to the ability of a system to continue functioning and provide services with minimal downtime, especially in the event of failures. PostgreSQL 9.4 offers several features and strategies that can be configured to achieve a high availability system. This article explores the architecture, components, and implementation of HA in PostgreSQL 9.4, providing technical insights and examples that can help in setting up an effective HA strategy.
High Availability Architecture in PostgreSQL 9.4
PostgreSQL 9.4's high availability is primarily achieved through replication and failover techniques. These techniques ensure that data is redundantly stored across multiple servers, allowing for quick recovery should one server fail. Below are the core components involved:
Streaming Replication
- Primary and Standby Nodes: The setup typically involves one primary node and one or more standby nodes. The primary node is the main server that handles all read and write operations, while the standby nodes keep a replicated copy of the database. Standby nodes can be set to accept read-only queries.
- Replication Slots: Introduced in PostgreSQL 9.4, replication slots ensure that the primary server retains enough WAL (Write Ahead Logging) data necessary for standby servers to keep up-to-date. This prevents WAL files from being removed before all connected standby nodes have had a chance to consume them.
Failover Mechanisms
- Automatic Failover: Tools like
PgBouncer,pgpool-II, or third-party solutions likePatronican manage automatic failover to switch operations from the primary to a standby node when the primary node becomes unavailable. These tools monitor the health of the primary node and initiate a failover process automatically when needed. - Manual Failover: In case automatic failover is not configured, or a controlled environment is preferred, a manual failover can be executed. This involves promoting a standby server to become the new primary server.
Implementing Streaming Replication
Here is a simple guide to set up streaming replication in PostgreSQL 9.4:
- Configure Primary Server:
- Edit the
postgresql.confto include:
- Set up a replication user in
pg_hba.conf:
- Create the replication role:
- Configure Standby Server:
- Start by cloning the data directory from the primary server using
pg_basebackup:
- Create a
recovery.conffile in the standby data directory:
- Start the PostgreSQL Service on the standby server to initiate replication.
Monitoring and Managing High Availability
Monitoring PostgreSQL high availability setups is crucial to ensure the system runs efficiently and to take action in case of failures. Tools like Nagios, Zabbix, and PostgreSQL's own logging facilities can be used.
Use replication information views:
pg_stat_replication: Provides details about the replication status from the primary server's perspective, including connection status and current WAL activity.pg_last_xact_replay_timestamp(): On standby servers, this function returns the last transaction replayed timestamp, providing insights into replication lag.
Summary Table: Key Components of PostgreSQL 9.4 HA
| Component | Description |
| Primary Node | Main server that hosts the read/write copy of the database. |
| Standby Node | Servers that replicate the primary node's data. Can be used for read-only queries. |
| Replication Slot | Ensures WAL retention to support standby nodes staying synchronized. |
| pgpool-II/PgBouncer | Middleware tools that help in load balancing and facilitating automatic failovers. |
| pg_stat_replication | View providing data about how the replication interfaces operate. |
| Trigger File | File which, when created, promotes a standby server to a primary role in case of failover. |
Conclusion
Implementing high availability with PostgreSQL 9.4 requires a mix of strategies involving streaming replication, monitoring, and both automatic and manual failover procedures. While PostgreSQL provides the tools necessary for these setups, effective configuration and robust monitoring are essential to achieve a resilient and responsive database system.
Related reading
- PostgreSQL asymmetric replication
- Postgresql replication in rails with data-fabric gem
- PostgreSQL restoration throwing error replication slot does not exist
- Postgresql slave for Mysql Master. Possible?
- PostgreSQL bitnami Helm Chart does not update the user password
- PostgreSQL error Fatal role username does not exist
- PostgreSQL Slony replication - scheduled sync
- postgresql streaming replication -- continuous archiving?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.