How does a load balancer such as HAProxy use Patroni's REST API health endpoints to send writes to the primary and reads to replicas?
answer
- HTTP status codes, not SQL
- 200 only where the leader lock is
- port 8008 in the examples
- lag and tag query parameters
basics
~20 sHAProxy health-checks each node's Patroni REST port instead of PostgreSQL: GET /primary returns 200 only on the leader, GET /replica only on a running replica without noloadbalance, so each pool sends traffic only to nodes answering 200.
solid answer
~40 sEach Patroni agent serves an HTTP API (port 8008 in the shipped examples) whose health endpoints answer 200 or 503 from Patroni's view of the cluster. Point the write pool at `/primary` (or `/` or `/read-write`): only the member holding the leader lock and running as primary returns 200, so after a failover HAProxy moves writes within a few checks. Point the read pool at `/replica`, which requires a running replica without the `noloadbalance` tag; add `?lag=16MB` to drop replicas that are too far behind, use `/read-only` to include the primary, or `/sync` and `/async` to split by replication role. `/health` only says PostgreSQL is running, and `/liveness` and `/readiness` are meant for Kubernetes probes. Add `on-marked-down shutdown-sessions` so clients still connected to a demoted node are cut off.
code
bash · 8 lines# Write-pool check: 200 only on the member holding the leader lock
curl -s -o /dev/null -w '%{http_code}\n' http://pay-dc1-a:8008/primary
# Read-pool check that rejects replicas more than 16 MB behind
curl -s -o /dev/null -w '%{http_code}\n' 'http://pay-dc2-a:8008/replica?lag=16MB'
# Full JSON status of one member (role, state, timeline, replication)
curl -s http://pay-dc1-a:8008/patronigo deeper
Remember that Patroni exposes HTTP health endpoints and that /primary answers 200 only on the current primary.
Map endpoints to pools: /primary for writes, /replica or /read-only for reads, the lag parameter, the noloadbalance tag, and HEAD checks against port 8008.
Explain detection timing, shutdown-sessions for demoted primaries, why lag filtering is approximate, and what every endpoint returns during a DCS outage.
Choose between proxy-based and client-side routing for the estate, and set how stale a read replica may be for each class of application.
## Why check Patroni and not PostgreSQL A load balancer in TCP mode can open a connection to port 5432, but that tells it nothing about **role**: a primary, a replica and an old primary that has not yet been demoted all accept connections. Even a SQL probe such as `SELECT pg_is_in_recovery()` only reports what PostgreSQL thinks, not whether that node still owns the **leader lock** in the DCS. Patroni's REST API answers from the agent's own view of the cluster, so the load balancer follows exactly the decisions Patroni makes. Each agent listens on the address in its `restapi` section; Patroni's sample configs use port **8008**. ## The health endpoints For every health check `GET` returns a JSON status document plus the status code; `HEAD` or `OPTIONS` return only the code, which is all a load balancer needs. | Endpoint | Returns 200 when | Typical use | |---|---|---| | `/primary`, `/`, `/read-write` | the node holds the leader lock and runs as primary | write pool | | `/leader` | the node holds the leader lock, as primary or standby leader | standby-cluster aware checks | | `/replica` | state is running, role is replica, no `noloadbalance` tag | read pool | | `/replica?lag=16MB` | as `/replica`, and lag is below the value | read pool that avoids stale replicas | | `/read-only` | as `/replica`, or the node is the primary | read pool that may use the primary | | `/sync`, `/async` | the node is a synchronous or asynchronous standby | split reads by durability | | `/quorum` | the node is a quorum standby on the primary | quorum mode | | `/health` | PostgreSQL is up and running, any role | basic monitoring only | | `/liveness` | the heartbeat loop ran within `ttl` (primary) or `2 * ttl` (replica) | Kubernetes livenessProbe | | `/readiness` | leader, or a replica replicating within the lag limit | Kubernetes readinessProbe | ## Wiring HAProxy Patroni's own template for HAProxy, in `extras/confd`, follows this shape: 1. One `mode tcp` listener per pool, for example port 5000 for writes and 5001 for reads. 2. `option httpchk HEAD /primary` on the write listener and `option httpchk HEAD /replica` on the read listener, each with `http-check expect status 200`. 3. Every `server` line points at PostgreSQL's port but checks Patroni's: `check port 8008`. 4. `default-server inter 3s fall 3 rise 2 on-marked-down shutdown-sessions`, so a node is marked down after three failed checks and its open client sessions are closed, which matters when the node is a demoted primary. ## Lag and tag filters - **`?lag=<max-lag>`** accepts bytes or units such as `1024kB`, `10MB`, `1GB`. Patroni compares the replica's replayed position with the leader's last WAL position recorded in the DCS, which is refreshed once per HA loop, so the filter is approximate rather than exact. - **`noloadbalance: true`** in a member's `tags` keeps it out of `/replica` while it still replicates and can still be a failover candidate — useful for a backup or reporting node. - **`?tag_<key>=<value>`** matches custom tags on replica checks; leader and primary checks ignore tags. ## Timing and failure behaviour - Detection time on the balancer is roughly `inter × fall`, about 9 seconds with the template values, on top of Patroni's own failover time. - When Patroni demotes a primary, `/primary` turns 503 on that node and 200 on the new leader; the balancer follows without any reconfiguration. - If the DCS is down and failsafe mode is off, Patroni demotes the primary, so `/primary` returns 503 everywhere and writes stop. That is intended: no endpoint should vouch for a primary that cannot prove it holds the lock. - Since 4.0 the JSON reports the role as `primary`, and since 4.1 `/readiness` only passes a replica that is actually replicating and close enough to the leader (by default within `maximum_lag_on_failover`). ## Where this sits A health-checking proxy is one way to route clients after a failover. Client-side routing with libpq multi-host connection strings is a core PostgreSQL topic, and a pooler such as PgBouncer is often placed behind the write listener. The same API serves monitoring, which is worth wiring up next to the balancer: - `GET /patroni` returns the member's full status (role, state, timeline, replication rows). - `GET /cluster` describes the whole topology, including receive and replay lag per replica since 4.1. - `GET /history` lists past switchovers and failovers by timeline. - `GET /metrics` exposes the status in Prometheus format, so alerts can fire on a cluster without a leader.
- Why should the write pool's check not be a plain TCP check on port 5432?Every node accepts TCP connections, replicas included, so the balancer would spread writes across read-only servers. Worse, an isolated old primary keeps accepting connections until Patroni demotes it. `/primary` answers 200 only where Patroni holds the leader lock, so the check follows Patroni's decision rather than PostgreSQL's reachability.
- What do /liveness and /readiness add for Patroni pods on Kubernetes?`/liveness` returns 503 when the heartbeat loop last ran more than `ttl` seconds ago on the primary or `2 * ttl` on a replica, so a hung agent gets restarted. `/readiness` returns 200 for the leader, or, since 4.1, for a replica that is replicating within the lag limit (default `maximum_lag_on_failover`), so a fresh pod serves reads only once it has caught up.
- A TCP check on port 5432 is enough to find the primary.
- GET /health returns 200 only on the primary.
- GET /replica also returns 200 on the primary.
- The noloadbalance tag stops a node from ever becoming leader.
- Patroni 4 still reports the leader's role as master in its API.
Assumes Patroni 4.1: since 4.0 the API reports the role as primary and rejects role=master, and 4.1's /readiness also requires a replica to be replicating within the lag limit.