High Availability¶
Active/passive HA (available, opt-in)¶
Karakuri ships an optional active/passive mode. Default is still a single
instance (replicaCount: 1, no coordination). To run standbys:
replicaCount: 2
config:
ha:
enabled: true # also grants the SA a Lease Role + adds a PodDisruptionBudget
How it works: every replica races for one Kubernetes Lease (karakuri-leader).
Only the holder runs the watchers, consumers, and Slack connection; standbys idle
with their liveness heartbeat warm. If the leader dies or loses the lease, it stops
and exits (Kubernetes restarts it as a fresh standby) and a standby acquires the
lease within ~lease_duration_seconds (default 15s). State persists via the
SQLite/Postgres state store, so the new leader restores in-flight cards on takeover.
This avoids double-posting (only one active replica) while removing the single point
of failure for scheduling — a dead node no longer blinds detection until reschedule.
For zero-data-loss across handover, run agent.backend: postgres.
Soak this with a real multi-replica + Postgres deployment before relying on it — distributed coordination should be observed failing over a few times first.
Active/active — Design (not implemented)¶
The notes below specify true active/active (all replicas processing), which Karakuri does not do — active/passive above is the supported HA mode.
Why it's hard¶
All cross-event state lives in the agent process memory:
lookup(diagnosis_id → card meta) — read by button handlers, the regression dedup, and the resolution loop- controller dedup — which event becomes a card
- mute store and per-user rate-limit buckets
With ≥2 replicas, each opens its own Slack Socket Mode connection and runs its own pod watcher, so without coordination every restart is diagnosed and posted N times, and a button click (delivered to a random replica) can't find a card that lives only on another replica's heap.
Required changes¶
1. Shared store (Postgres)¶
HA requires agent.backend: postgres. Add a Postgres-backed store (mirror
PostgresOutcomeStore, psycopg, autocommit) with these tables:
-- Exactly-once alert claim per active workload
CREATE TABLE alert_claims (
dedup_key TEXT PRIMARY KEY,
expires_at TIMESTAMPTZ NOT NULL
);
-- Shared card lifecycle (replaces the in-memory lookup)
CREATE TABLE cards (
diagnosis_id TEXT PRIMARY KEY,
dedup_key TEXT NOT NULL,
meta JSONB NOT NULL, -- cluster, ns, pod, controller, fix_cmd, status, ts…
status TEXT NOT NULL,
updated_at TIMESTAMPTZ NOT NULL
);
CREATE INDEX ON cards (dedup_key);
CREATE TABLE mutes (
mute_key TEXT PRIMARY KEY, -- cluster|ns|pod
expires_at TIMESTAMPTZ NOT NULL
);
2. Exactly-once claim (the crux)¶
Before diagnosing, a replica must win an atomic claim — only the winner runs the LLM and posts:
INSERT INTO alert_claims (dedup_key, expires_at)
VALUES (%s, now() + %s::interval)
ON CONFLICT (dedup_key) DO UPDATE
SET expires_at = excluded.expires_at
WHERE alert_claims.expires_at < now() -- only steal an expired claim
RETURNING dedup_key;
A returned row ⇒ this replica won (new claim, or it stole an expired one). No row ⇒
another replica owns the active card; skip. Postgres' per-row lock on the conflict
makes this safe under concurrent inserts. Test: N concurrent try_claim for the
same key ⇒ exactly one returns true (run against a real Postgres).
3. Wire the shared store in¶
_consume_restarts: resolve controller →try_claim(dedup_key); if lost, skip (no analyze, no post). If won, analyze + post + upsert thecardsrow. Regression becomes: claim already held by an active card → update it.button_handler(apply/skip/investigate/overflow): read/update the card bydiagnosis_idfrom thecardstable instead of the in-memory dict._resolution_loop: iteratecardswhere status is active; a single lease-guarded replica (or any, idempotently viaUPDATE … WHERE status=…) runs resolution so the chat_update happens once.mute/rate_limit: back with themutestable / a shared bucket (or accept per-replica rate-limit approximation).
4. Relax the replica guard¶
The replicaCount > 1 guard in templates/deployment.yaml becomes conditional:
allow >1 only when agent.backend == postgres and sharedState.enabled.
Keep Recreate/RollingUpdate choice configurable.
Rollout¶
- Implement the store + claim with a real-Postgres concurrency test.
- Wire consumer → buttons → resolution behind
sharedState.enabled(default off). - Soak: 2–3 replicas + Postgres in staging; induce crashes; assert exactly one card per incident and that buttons work regardless of which replica handles them.
- Only then document
replicaCount > 1as supported.
Until then: single active replica + Recreate + the KarakuriDown alert is the
supported, safe configuration.