Skip to content

Resilience: 2 or 3 replicas of the API and worker per instance, with shared state in PostgreSQL, proven #225

Open

Description

@ephico2real2

The owner's goal (2026-11-14)

"make sure that we have an excellent and well designed system that is very resilient and that we can even spin 2 or 3 cmapi pods and it will work well (we have the postgresql key value pair)"

Each of the three instances (nonprod, staging, prod) must run 2 or 3 replicas of the API and of the worker, survive the loss of any pod, and behave exactly as with one.

Read at main (2026-11-14)

Already replica-safe:

  • Jobs: SELECT ... FOR UPDATE SKIP LOCKED with a lease (cmapi/src/cmapi/worker.py, core/db/repo.py:249).
  • Request IDs under pg_advisory_xact_lock (core/db/repo.py:140); migrations under pg_advisory_lock (core/db/migrations/env.py:30).
  • Idempotency keys and the per-namespace request lock are database constraints; the auto-approval switch is a database row.
  • The chart has replicas per component and a PodDisruptionBudget template (deploy/helm/cmapi/templates/api.yaml:95).

Not ready or not proven:

  • Every component defaults to 1 replica (deploy/helm/cmapi/values.yaml:176-189, deploy/manifests/components/cmapi/20-api.yaml, 30-worker.yaml).
  • core/ratelimit.py keeps token buckets in memory per replica ("the limits are set per replica count"), so N replicas give a caller about N times the limit.
  • Per-pod caches: JWKS, the watch cache of each cluster, the directory cache. Correct for reads, but N times the watch load on each cluster and on Keycloak.
  • No test runs more than one API or worker process at once.

What to build

  1. An audit of every piece of in-process state (caches, timers, the rate limiter, anything a single process assumes), each marked: safe per pod, moved to PostgreSQL, or leader-only. Record it in docs/02 ("Scaling").
  2. Shared state in PostgreSQL, not Redis or Valkey (the owner's rule; kvpg inkcasthq/kvpg contract v1 is the reference, or a vetted Python library, as OB1-PROMPT.md says). First user: an instance-wide rate limiter, so the configured limit holds whatever the replica count.
  3. Leader-only work where exactly one pod must act (for example the cluster watches or a periodic collector): a PostgreSQL advisory-lock leader with a lease and fast failover; the other pods stand by.
  4. Kubernetes: default replicas: 2 (or 3 for prod) for the API and the worker; PodDisruptionBudget minAvailable: 1; topology spread or anti-affinity across nodes; readiness that fails while draining; graceful SIGTERM (finish or release the job lease); database pool size × replicas within max_connections, with the numbers written down.
  5. Proof, in two steps:

Done when

The audit, the changes, the native multi-process test and the docs are merged; the twelve-factor row VIII (concurrency) is scored on that test; the cluster proof is filed for Wave 8.

Activity

  1. added
    feature
    status:open
    Backlog
    on Nov 14, 2026
  2. added a commit that references this issue on Nov 14, 2026
  3. ephico2real2 commented on Nov 14, 2026

    @ephico2real2
    OwnerAuthor

    From Fable's review of #238 (the front door), to handle here:

    • portal.replicas is 1, and every call to the API, the CLI's too, now passes that one pod. With 2 or 3 API pods, the front door needs 2 replicas and a spread over nodes too.
    • The API pod has no preStop. At a rolling restart, uvicorn closes its idle connections at SIGTERM. A POST that nginx sends on one of them at that moment gets a 502 and is not repeated, because a POST is never retried. A short preStop sleep on the API pod closes most of that window. Not measured.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Backlogfeaturestatus:open

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions