GebSecure Docs v0.1.0

Architecture · All ADRs

ADR 0016 — Reliability & scale: stateless replicas, PostgreSQL as the single source of truth, explicit degradation

Architecture decision record — status: Accepted.

Status: Accepted

Context#

EPIC 26 targets 99.99 % gateway availability and P95 < 20 ms for simple authorization decisions under defined production load, with correctness preserved while any replica, the cache or the primary database fails. Before this EPIC the services were stateless and the queues used SKIP LOCKED (ADR 0008), but several behaviours were undefined or unsafe under failure: a Redis outage silently dropped risk/taint signals (fail-open for sensitive actions), unreadable limit counters read as "0 used", a database failover crashed processes (unhandled error on checked-out pool clients), credential revocation took up to the 3 s principal-cache TTL to reach other replicas, SIGTERM closed listeners immediately (in-flight requests and LB health checks raced), and webhook delivery could duplicate under load because a hot endpoint-row update could roll back a delivery that had already been sent.

Decision#

  1. Replicas are stateless and interchangeable. API, gateway and worker scale horizontally behind a health-checked load balancer (Kubernetes Service + readiness probe; HAProxy in the local reliability stack). Every cross-request invariant is enforced in PostgreSQL (approvals single-use by row state, outbox/job claiming with FOR UPDATE SKIP LOCKED, unique delivery rows) or atomically in Redis (Lua limit reservation, fixed-window rate-limit counters, revocation marks) — never in process memory. In-process caches are bounded by version stamps and short TTLs.
  2. Graceful drain. On SIGTERM readiness fails first (503 draining), the listener closes after SHUTDOWN_DRAIN_DELAY_MS (> LB check interval × fall count), in-flight requests finish, workers finish their current transaction; everything is bounded by SHUTDOWN_TIMEOUT_MS. The Helm chart aligns preStop and terminationGracePeriodSeconds.
  3. Readiness gates schema compatibility. Each build carries the newest migration it was compiled against; /readyz stays 503 until gs_schema_version() reaches it. Migrations are expand-only on upgrade (the migrator refuses drops/renames/type changes/NOT NULL additions unless a file is marked -- gebsecure:contract), run under an advisory lock with a per-statement lock_timeout, so N-1 and N run side by side during a rolling upgrade and a rollback needs no schema change.
  4. PostgreSQL HA and reads. Production uses a managed Multi-AZ primary (automatic failover, RPO 0 in-region); lag-tolerant reads (analytics, audit search) may use a streaming replica (DATABASE_REPLICA_URL). Replica staleness is measured end to end from a heartbeat row the worker writes every second; reads fall back to the primary when lag exceeds DATABASE_REPLICA_MAX_LAG_MS or the replica is unreachable. Permission checks for replica reads run on the primary. RLS is identical on the replica (physical copy).
  5. Redis is rebuildable state; degradation is explicit. Redis (Sentinel or managed failover) holds only data that can be lost: rate-limit windows, limit reservations, version stamps, risk history, revocation marks (also persisted in PostgreSQL). Commands are bounded by REDIS_COMMAND_TIMEOUT_MS. When Redis is unreachable:
    • rate limiting fails open (availability; documented, metric gebsecure_dependency_up{dependency="redis"}),
    • token/credential revocation falls back to PostgreSQL (fail closed, ≤ 3 s principal-cache staleness),
    • usage limits fail closed (unknown usage counts as exhausted for the permits that carry a limit),
    • policy caches revalidate from PostgreSQL within 10 s,
    • sensitive actions (write/delete/admin) fail closed to human approval (DEGRADED_SENSITIVE_ACTIONS=approval, or deny; allow is an explicit opt-in); reads continue on policy alone. Every such decision carries the risk_signals_unavailable risk signal and increments gebsecure_degraded_decisions_total.
  6. Delivery semantics. Outbox dispatch and webhook/notification delivery are at-least-once with idempotent consumers (unique (endpoint, event) delivery rows, event ids in every payload); retries never re-create deliveries. Delivery fan-out is bounded by the worker's system pool.
  7. Observability as the contract. SLOs are Prometheus recording/alerting rules (multi-window burn rates for 99.99 % gateway availability and the 20 ms authorize latency objective; queue backlog/age, replica lag, dependency and readiness alerts), tested with promtool test rules; each alert links a runbook.

Consequences#

  • (+) Any single API/gateway/worker replica, the Redis master or the PostgreSQL primary can be killed under load without a security fail-open, lost audit records or duplicate side effects; the measured outage windows and recovery times are in docs/operations/reliability-evidence.md.
  • (+) Rolling upgrades are safe by construction (schema gate + expand-only guard + drain).
  • (−) During a Redis outage sensitive actions need human approval: a deliberate availability cost.
  • (−) Every decision is a durable commit on the primary; the primary's commit throughput and CPU are the scaling ceiling for decisions per second (measured in the capacity test). Beyond it: larger primary, partitioned audit/outbox tables, or a log-based transport behind the dispatcher interface (ADR 0008).
  • (−) Revocation of machine credentials relies on a Redis mark for immediacy; with Redis down it converges within the 3 s principal-cache TTL from PostgreSQL.