Skip to main content

High availability

The backend keeps no state of its own. Every replica reads and writes the same PostgreSQL primary, so a second replica covers the loss of a pod or a node, and the database decides how available the platform is. This page covers the database side.

What the engine needs from PostgreSQL​

  • One primary. Every replica reads and writes the primary. The backend sends nothing to standbys.
  • Synchronous replication. A committed step, a started instance or a completed job must survive the loss of the primary. With asynchronous replication a failover can lose the last transactions the old primary committed. Quorum replication with one synchronous standby out of two (ANY 1) lets either standby go without stalling writes.
  • One address for the primary. Each replica connects to an address that always leads to the current primary: a Kubernetes service, a proxy, a virtual IP, or a URL that lists every node.

Each shard database is a PostgreSQL cluster of its own, set up the same way.

Kubernetes: CloudNativePG​

apiVersion: postgresql.cnpg.io/v1
kind: Cluster
metadata:
name: bpm
spec:
instances: 3
postgresql:
parameters:
tcp_user_timeout: "15000"
tcp_keepalives_idle: "10"
tcp_keepalives_interval: "5"
tcp_keepalives_count: "3"
synchronous:
method: any
number: 1
dataDurability: required

Point config.database.host at the cluster's read-write service, bpm-rw. CloudNativePG moves the service to the new primary on a switchover or a failover and closes the connections to the old one. The backend needs no connection pooler in front of the cluster.

Virtual machines: Patroni and HAProxy​

Patroni manages three PostgreSQL nodes and keeps its leader lock in etcd. The cluster settings for quorum replication:

bootstrap:
dcs:
ttl: 30
loop_wait: 10
retry_timeout: 10
maximum_lag_on_failover: 1048576
synchronous_mode: quorum
synchronous_mode_strict: true
synchronous_node_count: 1
postgresql:
use_pg_rewind: true
parameters:
tcp_user_timeout: 15000
tcp_keepalives_idle: 10
tcp_keepalives_interval: 5
tcp_keepalives_count: 3

synchronous_mode_strict keeps writes waiting while no standby confirms them, instead of going on unreplicated. A switchover moves the primary within seconds. A failover after the primary's host is lost waits for the leader lock to expire, up to ttl seconds.

HAProxy routes to the node whose Patroni REST API answers 200 on /primary:

defaults
mode tcp
timeout connect 4s
timeout client 30m
timeout server 30m
timeout check 3s

listen primary
bind *:5432 tcp-ut 15s
option clitcpka
clitcpka-idle 10s
clitcpka-intvl 5s
clitcpka-cnt 3
option httpchk OPTIONS /primary
http-check expect status 200
default-server inter 1s fall 2 rise 3 init-state down on-marked-down shutdown-sessions
server pg1 pg1.example.internal:5432 check port 8008
server pg2 pg2.example.internal:5432 check port 8008
server pg3 pg3.example.internal:5432 check port 8008
  • inter 1s fall 2 rise 3 marks a demoted primary down within 2 seconds, while a promoted one needs 3 passing checks to come up, so HAProxy never routes to both. With inter 3s fall 3 rise 2, as in Patroni's sample configuration, writes reach the demoted node for several seconds after a switchover and fail as read-only.
  • init-state down routes nothing to a node before its first check passes.
  • on-marked-down shutdown-sessions closes the connections to a node marked down, so they reconnect to the new primary.
  • tcp-ut 15s and the clitcpka settings close the session of a backend replica that stopped answering, and with it the session to PostgreSQL, see Dead connections.
  • timeout client and timeout server end a session that has been silent that long. Keep them above the longest statement you run.

Run two HAProxy instances, with keepalived moving a virtual IP between them, or list both in the connection URL:

DB_URL=postgres://bpm:secret@haproxy1:5432,haproxy2:5432/bpm?sslmode=require&target_session_attrs=read-write

The URL can also list the PostgreSQL nodes themselves, with no HAProxy, and target_session_attrs=read-write connects to the one that takes writes.

With the Helm chart, put DB_URL in a Secret and add it to the backend's environment. DB_URL takes precedence over the DB_* variables the chart renders from config.database:

backend:
extraEnvFrom:
- secretRef:
name: bpm-db-url

Dead connections​

A connection to a primary that went away, or through a proxy that stopped forwarding, fails within about 15 seconds instead of the kernel's retransmit timeout of about 15 minutes. The connection settings set the limits, and the libpq keys of the same name in DB_URL or a shard URL change them.

PostgreSQL needs the same limits for a client that went away. It keeps the session open until it notices, and a transaction left open holds its row locks and its connection slot. A job report cut off mid-transaction then blocks every retry of that report until the job's lock runs out. The tcp_user_timeout and tcp_keepalives_* parameters in the examples above end such a session within about 25 seconds. Behind HAProxy, PostgreSQL sees the proxy, which still answers, so the HAProxy settings above detect a backend replica that went away.

During a failover​

  • Readiness. /readyz answers 503 while the database does not answer or takes no writes, so the load balancer stops sending API calls to the replica.
  • Steps. A step that fails because the database does not answer, or answers read-only, commits nothing and runs again. Each step worker waits between such attempts, from 100 ms doubling up to 2 seconds, and goes back to full speed with the first step that does not fail that way. A decision, a call activity's process version or a migration's target version that cannot be read for the same reason raises no incident.
  • API calls and workers. Calls fail while the database is away. Clients and workers retry them.
  • Job locks. A worker that cannot heartbeat or report while the database is away keeps its job only until lockDuration runs out, see When a lock runs out. A poll that fails while the database is away cannot unlock its jobs either, and they stay locked until lockDuration runs out. Give service tasks at least one retry, so a lock lost in a failover offers the job again instead of raising a JobLockLost incident.

When no standby confirms​

With ANY 1 on three nodes, one standby can be lost and writes go on. With both standbys lost, every write waits until one returns. A statement cancelled during that wait, for example by a client timeout, stays committed on the primary without the standby, so a call that failed during the wait may still have taken effect.