Appearance
Postgres primary and standby
This recipe runs a Postgres primary on one Server and a streaming standby on another, with Roles. It is a recipe, not a Yoho feature. Yoho does not fail over. You promote the standby when you decide to, with yoho db promote. CI runs this exact example and every step on this page (test/integration/pg-replication.sh).
The files are in examples/postgres-replication.
How it works
db(Servera) anddb-standby(Serverb) are two Stateful Services. Each has one Server, as ADR 0005 requires. They use the same image, entrypoint andpg_hba.conf.The Connection
primarydecides which one is the primary:yamlconnections: primary: {service: db, port: 5432, consumers: [web, db-standby]}A consumer of
primarywith an empty data directory clones the primary withpg_basebackupand becomes a standby. A Service that is not a consumer and has an empty data directory runsinitdband becomes the primary.webconnects toYOHO_CONNECTION_PRIMARY_HOSTandYOHO_CONNECTION_PRIMARY_PORT, not to the namedb. After a promotion the same variables point at the new primary.Replication is asynchronous. A write the primary acknowledged can be lost if the primary's Server dies before the standby received it. For synchronous replication, set
synchronous_standby_nameson the primary; then writes stop when the standby is down.
Set it up
Copy the example directory. In
yoho.yml, set each Server'ssshandprivate_address(or use the WireGuard mesh). Incompose.yaml, set the Proxy host ofweb.Create
.yoho/secretsfrom.yoho/secrets.examplewith long random values:shPOSTGRES_PASSWORD=... REPLICATOR_PASSWORD=... DEMO_WRITE_TOKEN=...These are user secrets, not
x-yoho.generate. A generated secret has one owning Service (ADR 0019). After a promotion that owner would be a read-only standby, and rotation would fail. Both database Services receive both secrets as files.Run
yoho deploy. Yoho startsdbfirst, thendb-standby(provider before consumer). The standby clones the primary and starts streaming.
Check replication from the primary:
sh
yoho app exec db -- psql -U postgres -c "select application_name, state from pg_stat_replication"db-standby should show streaming.
The web Service is a demo. It reads a row count at /cgi-bin/read. Its write endpoint accepts only a POST whose body is DEMO_WRITE_TOKEN:
sh
curl --data-binary @token-file https://pg.example.com/cgi-bin/write?helloReplace it with your App, and give your App its own least-privilege role instead of postgres.
What protects the database
- The Connection firewall admits only consumer Servers to port 5432 on the primary's private address. Other hosts are refused, even when another rule accepts all forwarded traffic.
pg_hba.confrequiresscram-sha-256for every network login. It allows replication from any address, because the firewall is the network control. The same file then works on both nodes after a promotion.- The data directory is
pgdatainside the volume. A standby clone keeps a marker next to it until the clone is complete and configured. If the container stops mid-clone, the next start wipes the partial data and clones again. - The replication password is in
/var/lib/postgresql/.pgpass, outside the data directory. The entrypoint rewrites it from the secret files on every start.primary_conninfonames thepassfile; it never holds a password.
Promote the standby
yoho db promote does a planned switchover, with both Servers up. It is config first: you edit the Connection, and it does the rest.
Switch the Connection. In
yoho.yml, make the standby the provider and the old primary a consumer:yamlprimary: {service: db-standby, port: 5432, consumers: [web, db]}Do not run
yoho applyyet:yoho plannamesyoho db promotefor this change.Promote.
shyoho db promoteIt reads the old primary (
db) from the current Release and checks, read-only, thatdb-standbyis streaming from the same cluster (samesystem_identifier) ondb's current timeline and at most--max-lag(16MiB) behind. It repeats these checks with the locks held, right before the fence. It shows the plan and asksyes(--auto-approveskips the question,--planstops there). Then it:- fences
dbwithyoho app stop db: its container is stopped and removed, and it stays stopped through every deploy untilyoho app start db. Between stop and removal it runspg_controldataondb's data directory, which must sayshut down; the latest checkpoint location (the shutdown checkpoint, the end ofdb's WAL) is the target; - waits until
db-standbyhas replayed the target; - runs
SELECT pg_promote(true, 60)ondb-standby; - runs
yoho deploy.webnow connects todb-standby, and the firewall now protects port 5432 on Serverb.
If it stops before the promotion, nothing was promoted and
dbis stopped:yoho app start dbrestores the old primary, or rerunyoho db promoteto continue. Every step is journaled on Serverb, and a rerun resumes. A rerun checksdb's Server again: if you starteddb, it is fenced again; if you started and stopped it again, its shutdown can no longer be verified and the rerun refuses as below. If the promotion worked but the deploy failed, a rerun resumes at the deploy. The journal is deleted once the deploy succeeds.- fences
Rebuild the old primary as a standby. Yoho never deletes volumes, so remove the old data by hand on Server
a(yoho db promoteprints the volume name; check it withdocker volume ls):shdocker volume rm yoho-pg-production_pgdataThen run
yoho app start db.dbstarts with an empty data directory as a consumer, and clonesdb-standby.
The roles are now reversed. To switch back later, repeat the steps with the names swapped.
Replication is asynchronous. The fence is a clean shutdown of db, which sends its last WAL to the standby before it exits; a container that does not exit 0, or a data directory not shut down, stops the promotion, because its last writes may not have reached the standby. The journal remembers it: a rerun refuses until you run yoho app start db (and promote again later), or pass --old-primary-down to promote at the position db-standby received and accept that writes it did not receive are lost. psql runs in the containers over the local socket as POSTGRES_USER (from the Service's tag environment, Compose environment or secrets) or --user, so pg_hba.conf needs a local rule for that role, as the example's has.
By hand
Without yoho db promote (CI tests the command, not these steps):
- Fence:
yoho app stop db. For a planned switchover, first compareselect pg_current_wal_lsn()ondbwithselect pg_last_wal_replay_lsn()ondb-standby. - Promote:
yoho app exec db-standby -- psql -U postgres -c "SELECT pg_promote()". Do not runpg_ctl promote: it cannot run as root. - Switch the Connection as above and run
yoho apply.dbstays stopped. - Rebuild: remove the volume and
yoho app start db, as above.
Server a is lost
A Server you cannot reach over SSH is not fenced. It may still run and accept writes from clients you cannot see. Before you promote, make sure the old primary cannot accept writes: power Server a off in your provider's console and confirm that it stays off. Detaching only its external network is not enough, because web and the old primary may still write to each other on Server a.
Then move everything to Server b:
- In
yoho.yml, removeafrom the top-levelservers(this also removes it from the mesh) and from the Destination'sservers. - Drop the old primary's Role, place
webonb, makedb-standbythe Connection's provider, and leavewebas the only consumer.dbcannot be a consumer: it has no Server any more.
yaml
servers:
b: {ssh: root@203.0.113.11, private_address: 10.0.0.11}
destinations:
production:
servers: [b]
roles:
app: {services: [web], servers: [b]}
standby: {services: [db-standby], servers: [b]}
connections:
primary: {service: db-standby, port: 5432, consumers: [web]}Delete the
dbService fromcompose.yaml.Run:
shyoho db promote --old-primary-downAdd
--versionif the App directory is not a Git checkout. It reads the old primary from the Release on Serverb. There is no fence:db-standbyis promoted once its replay is stable and matches what it received (or its WAL receiver stopped), and writes it did not receive are lost. Then it deploys the edited Yoho file to Serverb.
Without --old-primary-down, a promotion whose old primary is not on a Server of the Destination is refused; with it, a promotion whose old primary's Server is still in the Destination is refused. If DNS, a Cloudflare Tunnel or a load balancer still sends traffic to Server a, point it at Server b. The App now runs on one Server with no standby. To add a standby again, add a new Server, a Service for it, and make it a consumer of primary. Never start the old Server a with its data again; wipe its disk first.
Rotate the passwords
REPLICATOR_PASSWORD (tested in CI):
Put the new value in
.yoho/secrets.Run
yoho deploy. Both database Services restart with the new secret files, and the entrypoint rewrites.pgpass.Set the new password on the primary:
shyoho app exec db -- sh /etc/yoho-pg/entrypoint.sh set-passwordspsql reads the values from the secret files. They never appear in a command line. The standby retries and reconnects with the new password.
Between steps 2 and 3, the standby cannot log in and replication waits. Run step 3 right after the deploy. After a promotion, run it on db-standby.
POSTGRES_PASSWORD (web uses it): change it in the database first, then deploy, so web never holds a password the database rejects. With the new value in the shell variable NEW (hex characters only):
sh
printf "ALTER ROLE postgres PASSWORD '%s';\n" "$NEW" | yoho app exec -i db -- psql -U postgres -v ON_ERROR_STOP=1printf is a shell builtin and the statement travels over stdin, so the value is not in a command line. If psql reports an error, stop: do not change .yoho/secrets. Then put the value in .yoho/secrets and run yoho deploy. Connections that web opens before the deploy fail until it finishes, so do this in a quiet moment. This order is not covered by CI.
Backups
Replication copies mistakes too: a dropped table is dropped on the standby. Keep off-Server Backups of the primary. After a promotion, move the Backup job to the Service that is now the primary.
Limits
- Yoho does not detect a failed primary and does not promote anything. Automatic failover needs a resident leader election, which ADR 0003 rules out.
- One standby. More standbys work the same way: add a Service, a Role and a consumer.
yoho db promotesupports Postgres only, and rebuilds the old primary with a full base backup (no pg_rewind).yoho db promotechecks the standby'ssystem_identifierand timeline, but not its upstream host or full WAL history. A standby that follows a different branch with the same timeline ID (two copies promoted independently) is not detected; with this recipe's single Connection, that takes a manual misconfiguration.