From 1017e2382902cbf63ffc55c2460af986ee9bb40a Mon Sep 17 00:00:00 2001 From: Konstantin Renner Date: Sat, 13 Jun 2026 12:09:36 +0200 Subject: [PATCH] cnpg: enable preferred synchronous replication on 2-instance clusters Today's kubelet-restart-induced switchovers on ak-pg and pprl-pg each took ~5 minutes from probe-failure to authentik/paperless reconnecting, dominated by ~4 minutes of postgres-internal promote work. Most of that time is the new primary chasing missing WAL via barman-cloud (the old primary terminated mid-stream, queued WAL had to be replayed from the archive) and the standby waiting through wal_retrieve_retry_interval cycles before noticing the promote trigger. With one synchronous standby (`synchronous.number: 1`), the standby is always at the same LSN as the primary at commit time, so on promote there is no missing WAL to chase and the standby is immediately ready to accept connections. CNPG switchovers in this mode complete in single digits of seconds. `dataDurability: preferred` (CNPG v1.20+ modern API, replaces the legacy minSyncReplicas/maxSyncReplicas fields) makes sync best-effort: the primary stops waiting for the standby if it becomes unavailable, so a standby outage does not block writes. The trade-off versus async is ~1-5 ms of added commit latency on healthy LAN+Ceph, which is well under the noise floor for authentik and paperless workloads. Single-instance clusters (im-pg, gf-pg) are not touched - sync replication requires >= 2 instances. Co-Authored-By: Claude Opus 4.7 (1M context) --- kubernetes/seed/authentik/cluster.yaml | 10 ++++++++++ kubernetes/seed/paperless/cluster.yaml | 5 +++++ 2 files changed, 15 insertions(+) diff --git a/kubernetes/seed/authentik/cluster.yaml b/kubernetes/seed/authentik/cluster.yaml index d957a840..a8c58a97 100644 --- a/kubernetes/seed/authentik/cluster.yaml +++ b/kubernetes/seed/authentik/cluster.yaml @@ -23,6 +23,16 @@ spec: parameters: shared_buffers: "256MB" max_connections: "400" + # Async replication makes CNPG switchover slow because the standby has + # to chase missing WAL via barman-cloud and replay queued records + # before it can promote. With one synchronous standby, the standby is + # always at the same LSN as the primary at commit time, so promote is + # near-instant. `dataDurability: preferred` falls back to async if the + # standby is down so writes don't block on standby unavailability. + synchronous: + method: any + number: 1 + dataDurability: preferred resources: requests: cpu: 200m diff --git a/kubernetes/seed/paperless/cluster.yaml b/kubernetes/seed/paperless/cluster.yaml index 0248f2ed..cb253ed5 100644 --- a/kubernetes/seed/paperless/cluster.yaml +++ b/kubernetes/seed/paperless/cluster.yaml @@ -23,6 +23,11 @@ spec: parameters: shared_buffers: "256MB" max_connections: "200" + # See ak-pg cluster.yaml for the rationale on synchronous replication. + synchronous: + method: any + number: 1 + dataDurability: preferred resources: requests: cpu: 200m -- 2.54.0