Master in Oracle DBA | Join us for the demo session on 19th November 2025 at 7:00 PM IST

High Availability with Patroni and etcd 

Breadcrumb Abstract Shape
Breadcrumb Abstract Shape
Breadcrumb Abstract Shape
Breadcrumb Abstract Shape
Breadcrumb Abstract Shape
Breadcrumb Abstract Shape
postgresql dba online training

High Availability with Patroni and etcd 

PostgreSQL High Availability with Patroni and etcd 

High availability (HA) in PostgreSQL refers to the ability of a database system to remain accessible with minimal downtime, even in the presence of hardware failures, network issues, or system crashes. Organizations adopt HA architectures to ensure continuous service delivery, safeguard data, and maintain application reliability. As part of postgresql dba online training and core skills required in postgresql for dba roles, understanding Patroni and etcd is fundamental.

1. Overview of High Availability in PostgreSQL

PostgreSQL relies on replication to create multiple copies of a database. In an HA setup, one node functions as the primary server, while the remaining nodes operate as standby servers. If the primary fails, a standby can be promoted to maintain service continuity. Traditional manual failover introduces delays and requires human intervention, which is why automated failover solutions are preferred.

2. Introduction to Patroni

Patroni is an open-source cluster management framework designed specifically for PostgreSQL. It automates the main components of a PostgreSQL HA cluster. Patroni is responsible for:

  • Monitoring the health of each PostgreSQL instance

  • Managing leader (primary) election

  • Executing automatic failover

  • Managing replication settings

  • Updating configuration dynamically

Patroni runs as a lightweight process on each database node and continuously checks the state of the PostgreSQL instance it manages.

3. Role of etcd in Cluster Coordination

etcd is a distributed, consistent, key-value store used for shared configuration and coordination in distributed systems. Patroni uses etcd as its Distributed Configuration Store (DCS). etcd stores:

  • Cluster-wide configuration

  • Leadership information

  • Heartbeat and health signals

  • Status of each node

Because etcd supports strong consistency and synchronous replication across its members, it ensures that the cluster state remains accurate and up to date.

4. HAProxy in a Patroni–etcd PostgreSQL High Availability Setup

HAProxy is an open-source, widely adopted load balancer used to distribute incoming requests across multiple backend servers. In PostgreSQL environments, HAProxy plays a crucial role by routing client connections to the correct primary node. It continuously monitors the state of all database nodes and ensures that applications always connect to the active primary, even when failover occurs.

How HAProxy Fits into the Patroni Architecture

In a Patroni-managed PostgreSQL cluster, each database node runs a Patroni agent. This agent is responsible for managing PostgreSQL instances and interacting with the Distributed Configuration Store (DCS), which in this architecture is etcd. Patroni updates the DCS with the current health, role, and availability of each node.

etcd stores this information in a consistent, distributed key-value structure. A typical HA-ready etcd setup requires at least three nodes to ensure majority-based decision-making using the Raft consensus algorithm. However, it is still possible—although less fault-tolerant—to operate with a single etcd node in constrained environments.

Leader Election Process

Leader election in this ecosystem is managed through etcd. Patroni agents attempt to acquire a special key in etcd; the first node that successfully writes this key becomes the primary. etcd ensures that only one node can claim leadership at a time by relying on Raft-based consensus, which prevents conflicts and split-brain scenarios.

Once the primary node is determined:

  • The Patroni agent configures the winning PostgreSQL instance as the primary.

  • All other nodes detect this status in etcd and reconfigure themselves as replicas.

  • Replication begins automatically.

This dynamic adjustment ensures that the cluster always maintains a clearly defined primary and consistent standby nodes.

How HAProxy Determines the Primary Node

HAProxy does not directly participate in leader election; instead, it queries Patroni. Each Patroni node exposes a REST API (default port 8008) that provides information about the node’s state. HAProxy routinely checks these endpoints to identify which PostgreSQL node is currently acting as the primary.

Based on the API responses:

  • HAProxy forwards client connections to the active primary.

  • If failover occurs, HAProxy automatically reroutes traffic to the newly elected primary without requiring manual changes.

This setup ensures smooth continuity of service and allows applications to remain connected even when the primary node changes.

Resilience and Failover

The combination of Patroni, etcd, and HAProxy forms a highly resilient high-availability architecture for PostgreSQL:

  • Patroni automates monitoring, role assignment, and failover.

  • etcd maintains accurate cluster state through strong data consistency.

  • HAProxy ensures clients always connect to the correct primary node.

Although failover is automated, a brief interruption may still occur while a new leader is selected and replication roles are updated. The duration depends on cluster health, network latency, and replication lag.

Conlusion

PostgreSQL HA using Patroni and etcd is the industry-standard approach for building resilient database clusters. As organizations adopt cloud-native architectures, DBAs who understand this stack stand out with advanced and modern PostgreSQL skills.

If you want to accelerate your career, consider adding these to your PostgreSQL learning roadmap—especially if you are enrolled in PostgreSQL DBA online training or preparing for real-world production DBA roles.

At Learnomate Technologies, we make sure you not only understand such cutting-edge features but also know how to implement them in real-world projects. Whether you’re a beginner looking to break into the database world or an experienced professional upgrading your skillset—we’ve got your back with the most practical, hands-on training in Oracle technologies.

📺 Want to see how we teach? Head over to our YouTube channel for insights, tutorials, and tech breakdowns: 👉 www.youtube.com/@learnomate

🌐 To know more about our courses, offerings, and team: Visit our official website: 👉 www.learnomate.org

💼 Let’s connect and talk tech! Follow me on LinkedIn for more updates, thoughts, and learning resources: 👉 https://www.linkedin.com/in/ankushthavali/

📝 If you want to read more about different technologies, Check out our detailed blog posts here: 👉 https://learnomate.org/blogs/

Let’s keep learning, exploring, and growing together. Because staying curious is the first step to staying ahead.

Happy learning!

ANKUSH😎

Let's Talk

Find your desired career path with us!