Skip to content

Persistence: SQLite and PostgreSQL HA

Faber runs against two persistence backends behind one internal/store.Store interface, selected with --store-driver=sqlite (default) or --store-driver=postgres.

Capability SQLite (--store-driver=sqlite) PostgreSQL HA (--store-driver=postgres)
Driver Pure-Go modernc.org/sqlite (CGO_ENABLED=0) pgx/v5 (pgxpool)
Replicas 1 (strategy: Recreate, ReadWriteOnce PVC) 2+ (strategy: RollingUpdate, active/standby)
Leader Election Off by default (--leader-elect=false) Auto-enabled (--leader-elect=true) via Kubernetes Lease
Live SSE Fan-Out In-process event hub Cross-replica PostgreSQL LISTEN/NOTIFY (faber_run_events)
Concurrency WAL mode, SetMaxOpenConns(1) for non-blocking kopiur reads Pool concurrency with FOR UPDATE SKIP LOCKED and advisory-locked migrations
Maintenance Periodic PRAGMA wal_checkpoint(TRUNCATE) + terminal run pruning Periodic terminal run pruning (--retention-days, default 90)

SQLite is designed for low-footprint clusters and homelabs where an external database is unnecessary:

  • Opens /data/faber.db in WAL mode with SetMaxOpenConns(1) so external online backup tools (such as kopiur) can read consistently without write lock contention.
  • Stores fixed-width 9-digit UTC timestamps and supports Go-parsed comparisons for legacy RFC3339Nano rows.
  • Runs periodic retention pruning (--retention-days, default 90, --retention-interval, default 6h) deleting terminal runs only (via COALESCE(finished_at, started_at)) followed by PRAGMA wal_checkpoint(TRUNCATE).

When --store-driver=postgres is selected, Faber switches cleanly to multi-replica high availability:

At startup, store.Open acquires a fixed PostgreSQL session advisory lock (pg_advisory_lock) before running idempotent schema migrations and releases it on exit. Multiple replicas starting simultaneously serialize migrations without race errors.

2. Leader Election and Active HTTP/SSE Serving on All Replicas

Section titled “2. Leader Election and Active HTTP/SSE Serving on All Replicas”
  • When --store-driver=postgres is set without an explicit --leader-elect flag, the controller automatically enables Kubernetes Lease leader election (--leader-election-id=faber-controller.faberops.dev).
  • Only the elected leader runs CR reconcilers, Cron triggers, Kubernetes watches, approval expiry sweeps, PR feedback sync, and retention pruning.
  • ui.Server implements manager.LeaderElectionRunnable with NeedLeaderElection() == false, so every replica serves the Web GUI, REST API, /v1/invoke, /mcp, /webhooks/*, and /api/v1/events SSE streams even while on standby.

Background state transitions such as expiring overdue WaitingApproval checkpoints use FOR UPDATE SKIP LOCKED inside PostgreSQL transactions so concurrent sweeps never double-process a checkpoint.

4. Cross-Replica LISTEN/NOTIFY SSE Fan-Out

Section titled “4. Cross-Replica LISTEN/NOTIFY SSE Fan-Out”
  • Every replica establishes a synchronous LISTEN faber_run_events connection before reporting readiness and automatically reconnects if the database restarts.
  • When the leader publishes a run event, it inserts the row and sends a NOTIFY faber_run_events payload so standby replicas fan out the event immediately to connected Web GUI and faberctl runs watch clients.
  • Payloads exceeding 7.5 KiB (PostgreSQL’s 8 KiB NOTIFY limit) automatically fall back to an ID-only notification that listeners hydrate from the run_events table.
  • Startup logs redact credentials fail-closed via store.RedactDSN so database passwords never appear in container logs.

Plaintext PostgreSQL DSNs are intentionally disallowed in values.yaml (enforced by charts/faber/values.schema.json). Create a Kubernetes Secret holding the DSN and reference it with store.postgres.existingSecret:

Terminal window
kubectl -n faber create secret generic faber-postgres \
--from-literal=dsn='postgres://faber:[email protected]:5432/faber?sslmode=require'
helm upgrade --install faber oci://ghcr.io/faberops/charts/faber \
--version 2.0.0 \
--namespace faber \
--set replicaCount=2 \
--set store.driver=postgres \
--set store.postgres.existingSecret=faber-postgres \
--set store.postgres.secretKey=dsn

If networkPolicy.enabled is true, add a networkPolicy.extraEgress rule permitting TCP 5432 to your PostgreSQL namespace and pods.