HAProxy
HAProxy is the load balancer the Patroni docs
recommend putting in front of a cluster: clients connect to one stable address,
HAProxy health-checks each member’s REST API, and routes writes to the current
leader (and, in read/write-split mode, reads to the replicas). pgcli runs it as
a standalone, top-level addon — shared routing infrastructure, not a
per-instance sidecar — pinned to the official haproxy:3.2.23-alpine image.
Platform support: the HAProxy addon is Linux-only for now. It reaches Patroni members over the host network, which the macOS
podman machinedoes not expose to containers. On macOSNewHAProxyManagerfails fast with a clear message;pg addon liststill shows installed instances without live status.
How It Works
Routing follows the official Patroni haproxy.cfg
pattern. Each backend is a Patroni member; two ports matter per member:
- the PostgreSQL port — the TCP port HAProxy actually forwards connections to;
- the REST API port — where HAProxy sends its
httpchkhealth check.
Patroni’s REST API answers these GETs (unauthenticated by design):
| Endpoint | Returns 200 on |
|---|---|
GET / |
the leader only |
GET /replica (optionally ?lag=<max>) |
replicas only, and only if their lag is within the limit |
So the leader is the sole UP server for a write listener, and the replicas are
the sole UP servers for a read listener — failover is picked up automatically
as the health checks flip. pgcli renders this into
<base-dir>/addon/haproxy/<name>/haproxy.cfg and bind-mounts it read-only into
the container.
Two routing modes
The mode is chosen with --mode:
unified(default) — a single listener sends all traffic to the current leader. Simple; every connection can read and write. On leader change the write listener useson-marked-down shutdown-sessionsto drop stale connections so clients reconnect to the new leader.split— read/write separation: a_<name>_rwlistener routes writes to the leader, and a separate_<name>_rolistener spreads reads across the replicas (round-robin, lag-filtered). Two client ports instead of one.
Two configuration modes
Above those two routing modes sits a choice of who writes haproxy.cfg:
- Rendered (default) — pgcli generates the config from
--node/--haplus--mode, ports and--max-lag, and rewrites it on every install. This is the Patroni read/write proxy described above. - Custom (
--conf-file <path>) — you provide the fullhaproxy.cfgand pgcli runs it verbatim, for any native HAProxy proxying (plain TCP relays, HTTP frontends, extra listeners — anything HAProxy does), not just a Patroni cluster. pgcli copies the file into its config directory, mounts that copy read-only, validates it withhaproxy -cbefore (re)starting the container, and never rewrites or deletes your original. After an edit,pg addon haproxy reloadre-validates the file and hot-reloads the running instance without dropping connections (conf-file instances only — see Hot reload). All listeners come from the file;--mode,--rw-port/--ro-port/--stats-port,--max-lag,--listenand--node/--hahave no meaning there and are rejected or ignored — see Custom config file.
Stats page
Every instance also gets an HTTP stats listener so you can watch server
up/down state in a browser: http://<listen>:<stats-port>/.
Install
Backend members are supplied one of two ways (mutually exclusive), or bypassed
entirely with --conf-file (see Custom config file):
--node NAME=HOST:PGPORT:RESTPORT(repeatable) — list them explicitly;--ha <scope>— auto-derive every local member of a Patroni scope thatpg hamanages, using each member’s PostgreSQL and REST API ports.
What each --node field means — for node1=10.0.0.11:35532:8008:
| Field | Meaning |
|---|---|
node1 |
member name — becomes the server name in haproxy.cfg and the label in the stats page |
10.0.0.11 |
address where this member is reachable from this host (the member’s advertise-host, or 127.0.0.1 on the same host) |
35532 |
the member’s PostgreSQL port that HAProxy forwards connections to |
8008 |
the member’s Patroni REST API port, used for the health check |
With --ha, all four come from pg.yaml: the Patroni member name, its
advertise-host (default 127.0.0.1), and its host_port / restapi_port.
The output reports the assigned ports and a ready-to-use connect URL:
Adding or removing members later
The backend list follows the scope’s local members recorded in pg.yaml, so
any topology change — or removal — is picked up by re-running the same
install command: it re-derives the full backend list and recreates the
container.
Added with pg ha create <scope> --member <m>, the new member is not in the
running HAProxy config yet:
Symmetrically, after pg ha remove <scope> --member <m> the removed member
would otherwise linger in the config as a stale backend — re-running install
drops it:
For an explicit --node install there is no auto-derivation: edit the --node
list itself (add or drop a spec) and re-run — the target list is replaced
wholesale each time, so the command always reflects the full set you want.
Cross-host clusters
--ha derives backends from this host’s pg.yaml, which lists only the
members this host’s pg ha create manages. A member living on another host was
never a backend here — adding or removing one there requires no re-sync on
this host (and leaves nothing stale). To front the whole cluster through one
HAProxy, use an explicit --node list, with each member’s advertised host:
Caveat: an instance installed with --ha on one host routes writes only to a
local leader — if failover promotes a remote member, the rw listener has no
UP server until the leader moves back. For cross-host clusters where the leader
can move, prefer the explicit all-members --node list (or run one HAProxy per
host and front it with a higher-level VIP/DNS).
Custom config file
The rendered mode above is pgcli’s opinion about Patroni routing. With
--conf-file you skip the render entirely and run your own haproxy.cfg —
arbitrary native HAProxy: plain TCP relays, HTTP frontends, any number of
frontend/backend/listen sections, anything the upstream software
supports. pgcli keeps only the container lifecycle (pull, create, start, stop,
remove, autostart) and the config gate:
--conf-file is mutually exclusive with --node/--ha (the install is
rejected with an error), and the file must exist and be non-empty. A typical
file mixes a raw TCP relay with an unrelated HTTP listener:
The summary then reports the custom mode instead of ports (there are none to report — they live in your file):
How the mode behaves, point by point (all of this is what an install on a real host does):
- Your file is the source of truth; pgcli’s copy is what runs. Install
copies it to
<base-dir>/addon/haproxy/<name>/haproxy.cfgand bind-mounts that copy read-only. Your original is never rewritten, andpg addon removedeletes only pgcli’s copy — your file stays where you put it. - Edits apply via
reload(seamless) or re-install (lossy). Change the file, then eitherpg addon haproxy reloadto hot-apply it to the running container without dropping connections, or re-run the same install to recreate the container. Both first re-copy your file and validate it withhaproxy -c. Editing the rendered-mode knobs does nothing here —--mode,--rw-port/--ro-port/--stats-port,--max-lagand--listendescribe the generator, not your file. See Hot reload. - Everything is validated before the container is touched. Each install
runs the mounted copy through
haproxy -c(check-only) in a throwaway container first. A bad file fails the install with HAProxy’s own diagnostics, exits non-zero, and restores the previous good copy — a running container and the on-disk config are left exactly as they were, so a later reboot orpg addon startcannot resurrect a rejected config. (Warnings — missing timeouts and the like — pass; only real errors are refused.) - Ports are yours to choose. The pool allocator skips conf-file instances;
it neither assigns nor learns the ports in your file. Pick listener ports
yourself and keep them clear of rendered-mode instances’ auto-assigned ports
and everything else on the host — HAProxy binds fail at start with
cannot bind socketif you collide. - The container runs with host networking, like the rendered mode. A
frontend bound to
127.0.0.1is reachable from the host itself; to let other containers/hosts in, bind the host’s address (aspg_relayabove does). - Health of the proxies is your config’s business.
pg logs <name>shows HAProxy’s own stdout (if your file logs there, as the sample does), and any stats page is whateverstatssection you wrote — pgcli does not add one. - Switching back is just an install without
--conf-file: pass--node/--haagain and the instance returns to rendered mode (the conf-file copy on disk is overwritten by the generated config).
Example: HTTP load balancer with health checks
A conf-file instance is a full HAProxy, so the Patroni pattern is not the only
thing it can do — here is an HTTP round-robin balancer over several app servers
with active health checks (this exact config was run and the DOWN/UP behaviour
observed in pg logs):
What each piece does, and what was observed live:
- Multiple
serverlines — HAProxy round-robins new connections across theUPservers (add a line per backend; the list order is the starting rotation). option httpchk GET /+http-check expect status 200— the health probe is an HTTPGET /and only 200 counts as UP. Match the method and the expected code to what the app actually answers: a server that returns405toHEADbut200toGETneeds theGETform, not theHEAD /default.default-server inter 3s fall 3 rise 2— probe every 3s, mark DOWN after 3 consecutive failures, return to service after 2 successes.checkon each server turns the probe on for that server.
When a backend is unreachable its probe fails and, after fall 3, HAProxy drops
it from rotation and logs it — pg logs <name> showed:
and all frontend traffic kept returning 200 by flowing only to the surviving
app1. Once app2’s port answers again it clears rise 2 and rejoins the
rotation — no reload needed. To add or remove a server line, edit the file
and run pg addon haproxy reload (see Hot reload): it
re-validates with haproxy -c and hot-swaps the worker without dropping
in-flight connections. Backend membership changes are cleanest still through the
health-check path above (a server you stop is pulled from rotation with no
reload at all); reload is for when you change the server list itself.
Tuning concurrency (threads)
HAProxy’s model is one process, many threads — there is no nginx-style
worker_processes. In conf-file mode you tune it from the global section of
your own file (the rendered mode does not set any of these):
nbthread N— the number of worker threads. Validated against the image:haproxy -cacceptsnbthread 4.- default when unset — HAProxy spreads threads across the machine’s CPUs
automatically on a multi-processor host (its own wording: “Threads will
automatically be used on multi-processor machines if available”), so you only
need
nbthreadto pin a specific count. cpu-map auto:1-N 0-M— optionally bind thread 1…N to CPU cores 0…M (validated).nbprocno longer exists. Since HAProxy 2.5 it is refused outright (haproxy -cfails with “nbproc is not supported any more since HAProxy 2.5”). Use threads, not processes.
Changing it means editing the file and running pg addon haproxy reload — the
master re-reads the config and starts a fresh worker with the new thread count
(verified: nbthread 2 → 4 applied with no dropped connections), so unlike a
re-install it doesn’t cut in-flight traffic. This is a config knob in your file,
not a pgcli flag.
Hot reload
--conf-file instances support seamless config reload — the HAProxy equivalent
of pg addon nginx reload:
The official image already runs HAProxy in master-worker mode (-W): pid 1
is the master, a child is the worker. reload sends the master SIGUSR2, so it
starts a new worker from the config on disk, hands the listening sockets over,
and lets the old worker drain its in-flight connections before exiting —
no dropped connections, unlike the lossy container recreate that install
performs. (This is verified behaviour, not just theory: a running instance’s
worker was swapped end-to-end while it kept serving 200s.)
Before it signals anything, reload does exactly what install’s gate does — it
is not a blind signal:
- re-copies your
--conf-fileto the mountedhaproxy.cfg(verbatim); - runs the copy through
haproxy -c(check-only) in a throwaway container; - only if it validates does it send
SIGUSR2. A file that fails the check aborts the reload with HAProxy’s own diagnostics (non-zero exit), restores the previous good copy on disk, and leaves the running container untouched — so a rejected edit can never be picked up by a later restart either.
Requirements and scope:
--conf-fileinstances only. A rendered instance rebuilds its config from--node/--haon every install and takes no hand-edits, so its update path stayspg addon install haproxy. Runningreloadon it is refused with that advice.- The container must be running.
reloadsignals the live master; if the instance is stopped, start it (pg addon start haproxy --name custom) — a freshinstallalso picks up the edited file. reloadis for changing the running config in place. Turning a backend off for maintenance still needs no reload at all — the health check pulls it from rotation; reload is for editing theserverlist / global knobs themselves.
The same reload is available on install via --reload: for a running
conf-file instance it validates and hot-reloads instead of recreating the
container, so one command both (re)configures and applies without dropped
connections:
Without --reload, install keeps its default recreate-the-container behaviour
(lossy). --reload on a stopped or rendered instance is a no-op warning — it
just installs normally.
Read/Write Split in Practice
After installing with --mode split, two client ports are exposed:
| Port | Traffic | Backend |
|---|---|---|
write_port (default 5000) |
Read + write | Leader only |
read_port (default 5001) |
Read only | Replicas (round-robin, lag ≤ max-lag) |
stats_port (default 5002) |
HTTP stats page | — |
After a failover, HAProxy picks up the new leader automatically via health
checks — no connection string change needed. A browser at
http://127.0.0.1:5002/ shows real-time backend state.
Ports
Ports are auto-assigned from a per-host pool, three consecutive free ports per
instance (rw, then ro if split, then stats), starting at haproxy_start_port
(default 5000). Override any of them explicitly:
Because the pool scans for free ports and tracks what every HAProxy instance
has already taken, you can run many instances on one machine without
collisions. A split instance consumes 3 ports (rw / ro / stats), a unified
instance 2 (rw / stats):
The bind address defaults to 127.0.0.1; set --listen 0.0.0.0 — or the
listen key in pg.yaml — to expose the listeners on the network. Keep it on
loopback unless the backends require otherwise; there is no authentication in
front of PostgreSQL here.
Configuration
Everything lives under the top-level addons.haproxy map in pg.yaml, keyed by
instance name:
targets is rendered from and re-derived by install; edit mode, ports,
replica_max_lag, or image_tag here for fine control, then
pg addon install haproxy --name lb ... to apply. The rendered haproxy.cfg
carries no secrets — only hosts, ports, and health-check URIs.
List
A conf-file instance shows its source instead of ports and backends:
Start and stop
After a host reboot, bring an instance back without re-rendering config:
install always recreates the container so config changes take effect;
start only starts an existing one (and self-heals an improper state by
recreating from the haproxy.cfg on disk). For a --conf-file instance,
pg addon haproxy reload applies an edited file to a running container
without that recreate — see Hot reload.
Auto-start on Boot
Containers carry a --restart unless-stopped policy (crashes, not reboots). To
bring an instance up after a host reboot:
This sets autostart: true and installs/refreshes the boot service (see
Auto-start on Boot). Boot is start-only: it starts the
existing container reading the haproxy.cfg already on disk, and never
re-renders config — install the instance first. The boot service starts
HAProxy after the Patroni members, so its backends are already coming up.
pg autostart status lists every target’s state.
Remove
Stops and removes the container and deletes its config directory.
Logs
The container logs to stdout (log stdout format raw local0 info), so health-check
transitions and connection events show up here.
Troubleshooting
- All backends DOWN in the stats page. The health check hits each member’s
REST API port. Confirm it is reachable —
curl http://<host>:<rest_port>/returns 200 on the leader, 503 elsewhere;/replicais the mirror image. A000means the REST API is down or the port is wrong. --hafinds no members.--haonly derives local members thatpg hamanages inpg.yaml. For remote members, or a scope pgcli doesn’t manage, list them with--node.- Port collision on manual
--rw-port. Pick a port outside the auto pool (haproxy_start_portand up) or the auto-assigner will treat it as taken. - A conf-file install fails
haproxy -cbut the old config keeps running. That is the designed behaviour: the rejected copy is rolled back and the container untouched. Fix the file (HAProxy’s error names the line) and re-install. To see why a started container died anyway (cannot bind socket, missing files your config references), checkpg logs <name>. reloadsays “container is not running”. Reload signals the live master, so the instance must be up first —pg addon start haproxy --name <name>(or a freshpg addon install, which also picks up the edited file).reloadrejects the file withhaproxy -cerrors. Same gate as install: the reload is aborted, the previous good copy is restored, and nothing was reloaded — fix the line HAProxy names and reload again.- macOS. Not supported yet — HAProxy needs host networking to reach Patroni members, which the podman machine VM does not provide to containers.