Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Sovereign, high-performance data protection

PostgreSQL High-Availability (multi-host)

The KMS supports multi-host PostgreSQL connection strings for high-availability deployments (streaming replication, Patroni, pgBouncer clusters, etc.). The database-url value is treated as a raw string so that host1:port,host2:port syntax — which the standard URL parser cannot handle — is passed directly to the PostgreSQL driver.

KMS connects to the PostgreSQL primary; an HA manager handles failover

Use the target_session_attrs query parameter to control which node the driver connects to:

ValueBehaviour
read-writeConnect only to the primary (default for HA)
anyConnect to any available node (suitable for read replicas)
read-onlyConnect only to a standby

Host order

The driver tries hosts left to right in the URL. With target_session_attrs=read-write, standbys are automatically skipped (they return transaction_read_write=off), so the primary is always found regardless of its position. However, put the expected primary first to avoid an unnecessary round-trip to the standby on every new connection under normal conditions.

Example — two-node HA cluster (primary listed first):

[db]
database_type = "postgresql"
database_url  = "postgresql://kms_user:kms_password@primary:5432,standby:5432/kms?target_session_attrs=read-write"

The URL must start with postgresql:// or postgres://; any other scheme is rejected at startup.

Replication and standby writability

A standby is read-only

In standard PostgreSQL streaming replication, the standby node operates in hot standby mode: it accepts read queries but rejects all writes. The KMS requires a read-write connection for every operation, so target_session_attrs=read-write will skip a standby automatically — but this also means the KMS cannot use the standby as a fallback by itself. The standby only becomes usable by the KMS after it has been promoted to primary.

Replication is entirely managed by the PostgreSQL infrastructure — the KMS does not configure, trigger, or monitor it.

For automatic failover, deploy an HA manager such as Patroni, pg_auto_failover, or repmgr. These tools:

  1. Monitor the primary continuously.
  2. Promote the standby when the primary is unreachable.
  3. Demote the old primary back to standby automatically when it recovers (Patroni uses pg_rewind to resync it with the new primary's WAL timeline).

The KMS URL requires no change: with target_session_attrs=read-write the driver always connects to whichever node is currently the primary.

What happens when the primary goes down then comes back up?

PhaseStateKMS behaviour
Primary down, standby still read-onlyNo writable node existsKMS retries exhaust (≤ ~3.2 s), requests fail with a database error
HA manager promotes the standbyStandby becomes new primaryKMS reconnects on next retry/request, normal operation resumes
Old primary recoversHA manager demotes it to new standbyTransparent; KMS skips it (read-only) and connects to the new primary
Old primary recovers without an HA managerOld primary restarts as a standalone node — data divergence riskManual intervention required before reconnecting the KMS

Eliminate URL maintenance with a virtual IP or DNS endpoint

After a permanent failover without an HA manager, the roles of primary:5432 and standby:5432 are reversed. The KMS still works (it finds the read-write node), but it tries the old-primary address first on every connection, adding latency. To avoid this, front the cluster with a virtual IP (Keepalived, AWS RDS Multi-AZ endpoint, Azure Flexible Server read-write endpoint, GCP Cloud SQL HA endpoint) and use a single-host URL — failover then becomes completely transparent to the KMS.

PostgreSQL failover retry

When a PostgreSQL primary fails over, the driver may return transient connection errors before the new primary is ready. The KMS automatically retries failed queries with exponential backoff for the following PostgreSQL SQLSTATE codes:

SQLSTATEMeaning
08001sqlclient_unable_to_establish_sqlconnection
08004sqlserver_rejected_establishment_of_sqlconnection
57P02crash_shutdown
57P03cannot_connect_now

No additional configuration is required; the retry behaviour is enabled automatically for all PostgreSQL connections.

See also