This article was automatically translated from the original Turkish version.
"PostgreSQL is an excellent database... but what happens when there's an outage? That’s when Patroni steps in."
PostgreSQL is highly reliable and performant on its own. However, as systems grow—when you need to use the database across multiple servers in a distributed environment with high availability—certain challenges arise:
This is precisely where Patroni saves the day.
Patroni is a High Availability (HA) manager for PostgreSQL that automates failover, leader election, health checks, and cluster management. In essence, Patroni is an intelligent orchestrator that brings HA capabilities to PostgreSQL. Simply put, Patroni:
In this article, we will not discuss intermediary layers such as HAProxy or PgBouncer. Dedicated articles on these tools will be published soon—stay tuned!
The CAP theorem states that in a distributed system, you can only choose two out of three properties:
1. Consistency
2. Availability
3. Partition Tolerance
In distributed systems, network partitions are inevitable. Therefore, the choice narrows down to balancing consistency and availability. Patroni chooses the "Consistency + Partition Tolerance" side. Why?
Patroni requires a key-value (KV) store to centrally track which server is the leader and the overall health of the cluster. This is typically done via:
These systems:
Patroni performs leader election through Etcd. The process works as follows:
1. Each PostgreSQL instance, via its associated Patroni bot, checks whether a specific key in Etcd has expired.
2. If the leader key is vacant, the bot requests Etcd to acquire the key for a limited duration.
3. Etcd, using the RAFT protocol in the background, ensures that only one instance can acquire the key, preventing race conditions.
4. The first instance to acquire the key is recognized as the leader, and Patroni configures that PostgreSQL instance as the primary.
5. Other instances detect this leadership change and automatically switch to replica mode.
Thus, the leader is selected in a distributed and consistent manner without requiring a central control point.
For a simple example, we can use three PostgreSQL + Patroni containers and three Etcd containers:
First, obtain the project files from the developer’s GitHub repository. Build the Dockerfile in the project to create the Patroni Docker image we will use.
Example related to this topic (created by the author)
Patroni is a highly effective and flexible solution for transforming PostgreSQL into a high-availability system.
In our next article, we will explain how to route traffic to a Patroni cluster using HAProxy and create a connection pool with PgBouncer. Don’t miss it!
Stay Tuned—Keep Your Data Alive!
“Consul Documentation.” HashiCorp. Accessed May 10, 2025. https://developer.hashicorp.com/consul/docs.
“Etcd Documentation.” *etcd.io.* Accessed May 10, 2025. https://etcd.io/docs/.
“Patroni Documentation.” Read the Docs. Accessed May 10, 2025. https://patroni.readthedocs.io/.
“Patroni Repository.” GitHub. Accessed May 10, 2025. https://github.com/patroni/patroni.
“PostgreSQL Documentation: Streaming Replication.” PostgreSQL Global Development Group. Accessed May 10, 2025. https://www.postgresql.org/docs/current/warm-standby.html.
“Raft Consensus Algorithm Explained.” The Secret Lives of Data. Accessed May 10, 2025. https://thesecretlivesofdata.com/raft/.
Introduction: PostgreSQL’s Challenge in Distributed Systems
What Is Patroni, and What Does It Do?
CAP Theorem and Patroni
Etcd and Consul: The Foundation of Patroni
How Is Leader Election Performed?
Constraints and Limitations
Patroni Installation with Docker (Minimal Example)
Conclusion and Final Thoughts