PostgreSQL
High Availability
Database Management
PostgreSQL 9.4
Database Topology

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.

Practice system design

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 like Patroni can 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:

  1. Configure Primary Server:
    • Edit the postgresql.conf to include:
plaintext
1        wal_level = logical
2        max_wal_senders = 10
3        wal_keep_segments = 64
4        hot_standby = on
  • Set up a replication user in pg_hba.conf:
plaintext
        host replication replicator 192.168.1.1/32 md5
  • Create the replication role:
sql
        CREATE ROLE replicator WITH REPLICATION PASSWORD 'your_password' LOGIN;
  1. Configure Standby Server:
    • Start by cloning the data directory from the primary server using pg_basebackup:
bash
        pg_basebackup -h primary_host -D /var/lib/postgresql/9.4/main -U replicator -v -P
  • Create a recovery.conf file in the standby data directory:
plaintext
        standby_mode = 'on'
        primary_conninfo = 'host=primary_host port=5432 user=replicator password=your_password'
        trigger_file = '/tmp/promote_trigger'
  1. 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

ComponentDescription
Primary NodeMain server that hosts the read/write copy of the database.
Standby NodeServers that replicate the primary node's data. Can be used for read-only queries.
Replication SlotEnsures WAL retention to support standby nodes staying synchronized.
pgpool-II/PgBouncerMiddleware tools that help in load balancing and facilitating automatic failovers.
pg_stat_replicationView providing data about how the replication interfaces operate.
Trigger FileFile 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
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track 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.

Practice system design

All Rights Reserved.