Learn Labs
Foundations

The Production Cluster

Three nodes, automatic failover, pooling, TLS and backups: what compose_prod.yaml runs and why

compose.yaml is one Postgres, enough to learn SQL on. Everything from the replication chapter onwards runs on compose_prod.yaml: the shape a real deployment has, on one laptop. This page is the map; the later chapters measure each part.

to the primaryWALWALWAL + backupsrenews leader keyapplicationTLS + SCRAMPgBouncer:6432, transaction poolHAProxy:5000 rw / :5001 ropg-2replicapg-1primarypg-3replicaSeaweedFS S3encrypted backupsetcd ×3leader key, TTL 30 severy node runs Postgres 18 + Patroni + pgBackRest; HAProxy asks each Patroni (GET /primary) which node is primary
PieceVersionJob
PostgreSQL18.6the database, on three nodes
Patroni4.1.5runs Postgres on each node, decides who is primary, fails over
etcd3.7.2three-member consensus store holding Patroni's leader key
HAProxy3.2.26port 5000 always reaches the primary, 5001 the replicas
PgBouncer1.26.1connection pooling; the only endpoint applications use
pgBackRest2.59.3backups and the continuous WAL archive, to SeaweedFS

PostgreSQL 19 was at release candidate 1 when this was built, so the module runs 18.6, the newest stable release. pgBackRest 2.60.0 had been out for five days but wasn't in the PostgreSQL apt repository yet, so the node image pins the packaged 2.59.3.

Bring it up

(cd ../seaweedfs && docker compose -f compose_prod.yaml up -d)   # backup target
docker compose -f compose_prod.yaml up -d --build
docker exec pg-1 patronictl -c /etc/patroni/patroni.yml list

| Member | Host | Role    | State     | TL | Receive LSN | Lag | Replay LSN | Lag |
| pg-1   | pg-1 | Replica | streaming |  1 |   0/4000060 |   0 |  0/4000060 |   0 |
| pg-2   | pg-2 | Replica | streaming |  1 |   0/4000060 |   0 |  0/4000060 |   0 |
| pg-3   | pg-3 | Leader  | running   |  1 |             |     |            |     |

From up to three nodes streaming took 34 seconds. Nobody chose pg-3 as the primary: all three Patroni processes raced to create the leader key in etcd, and pg-3 won. It ran initdb, the other two cloned it with pg_basebackup and started streaming.

One config for three nodes

Each node runs the same image (node/Dockerfile: the Postgres image plus Patroni and pgBackRest from the PostgreSQL apt repository) and the same config/patroni.yml. Only the name and addresses differ, passed as environment variables.

bootstrap:dcs:written to etcd ONCE, when the cluster is createdttl: 30the primary must renew its leader key within 30 sloop_wait: 10maximum_lag_on_failover: 1048576a replica >1 MB behind may not take overpostgresql:use_pg_rewind: truean old primary rejoins by rewinding, not re-cloninguse_slots: truethe primary keeps WAL each replica still needsparameters:archive_command: pgbackrest --stanza=main archive-push "%p"archive_timeout: 60bounds data loss to a minute on an idle primaryssl: "on"password_encryption: scram-sha-256post_bootstrap: /etc/patroni/post_bootstrap.shroles, database, schema

The bootstrap.dcs block is read once. After the cluster exists, the copy in etcd is the truth and you change it with patronictl edit-config. Editing the file and restarting changes nothing, which surprises everyone once.

How clients connect

Applications connect to PgBouncer (appdb for reads and writes, appdb_ro for read-only). PgBouncer connects to HAProxy, and HAProxy asks each node's Patroni REST API which one is primary:

listen primary
  bind :5000
  option httpchk
  http-check send meth GET uri /primary    # 200 only on the leader
  default-server inter 1s fall 2 rise 1 on-marked-down shutdown-sessions
  server pg-1 pg-1:5432 check port 8008
  server pg-2 pg-2:5432 check port 8008
  server pg-3 pg-3:5432 check port 8008

Nothing in the application knows which node is primary. After a failover, HAProxy's next health check finds the new one. The failover chapter shows that two of these settings (rise 1, and PgBouncer's server_login_retry) were not the first version: the drills found them.

Every connection must use TLS (pg_hba.conf rejects anything else), and the superuser can only log in over the local socket, through docker exec. The application gets a role that can read and write data but not create or drop tables. The security lab attacks each of these.

What went wrong building it

These are part of the lesson, because each one is a real production trap.

  • Replicas took up to five minutes to appear. pg_basebackup copies nothing until the primary's next checkpoint, and with checkpoint_timeout at 5 minutes that's the worst case. Patroni's basebackup: [{checkpoint: fast}] forces one. With it, 34 seconds.
  • pgBackRest refused to talk to SeaweedFS. pgBackRest only speaks HTTPS to S3, and SeaweedFS's S3 port is plain HTTP. A small HAProxy (s3-gateway) now terminates TLS in front of it, which is better anyway: backups shouldn't cross a network in plaintext.
  • Rebuilding the cluster broke WAL archiving. A new cluster gets a new system identifier, and pgBackRest refused to archive into the stanza the previous cluster created: system-id 7699567011657928738 do not match repo1 stanza ... 7699566270498205730. It's protecting you from mixing two databases' WAL. scripts/reset.sh deletes the repository together with the volumes.
  • Neither PgBouncer nor pgBackRest accepts comments at the end of a line. default_pool_size = 20 ; per pair is a fatal config error in PgBouncer, and start-fast=y # comment makes pgBackRest complain that start-fast must be y or n.

On this page