|
| 1 | +# ADR 0006 — Recovery from a lost primary majority (Cluster topology) |
| 2 | + |
| 3 | +- Status: **Implemented** for **Cluster** topology. Automatic for the **Cache** |
| 4 | + profile; **opt-in for Durable** via the per-cluster annotation |
| 5 | + `valkey.wellcake.io/quorum-takeover: "true"` (a forced takeover can drop |
| 6 | + acknowledged writes, which the Durable profile promises to keep). Validated |
| 7 | + live on k3d: a 3-shard cluster, majority of primaries fenced, recovered to |
| 8 | + `cluster_state:ok` with **0 data loss** and the fenced primaries rejoining as |
| 9 | + replicas. See "Validation" below. |
| 10 | +- Date: 2026-08-04 |
| 11 | +- Context: ADR 0004 handles *planned* restarts and *single* primary loss (a |
| 12 | + replica is promoted, reactively or proactively). It does not cover the |
| 13 | + disaster where a Cluster loses the **majority of its primaries at once** — an |
| 14 | + AZ/node-group outage, a bad drain, a correlated crash. |
| 15 | + |
| 16 | +## Context |
| 17 | + |
| 18 | +Valkey Cluster fails a primary over by **vote**: a replica asking to take its |
| 19 | +dead primary's slots needs acknowledgement from a majority of the *masters*. |
| 20 | +That is exactly what a majority-primary loss removes. With `N` primaries and more |
| 21 | +than `floor(N/2)` of them down, the survivors are not a quorum, so **no replica |
| 22 | +can be voted in**. The cluster sits in `cluster_state:fail` with unserved slots |
| 23 | +**indefinitely** — the surviving replicas of the dead primaries are healthy and |
| 24 | +current, but gossip will never promote them. This is a genuine SPOF for the |
| 25 | +Cluster topology (the AR1 gap): the data is there, but nothing brings it back. |
| 26 | + |
| 27 | +The one Valkey primitive that breaks the deadlock is `CLUSTER FAILOVER TAKEOVER`: |
| 28 | +a replica promotes itself **unilaterally**, bumping its `configEpoch` above every |
| 29 | +peer and claiming its primary's slots **without a vote**. It is powerful and |
| 30 | +correspondingly dangerous — run it while the old primary is still serving and you |
| 31 | +get two owners for the same slots (split-brain). Gossip therefore never issues it |
| 32 | +on its own; a human (or an operator) must decide the old primary is really gone. |
| 33 | + |
| 34 | +An operator is well placed to make that call, because it sees something gossip |
| 35 | +cannot: **pod and node liveness through the k8s API**, plus a direct out-of-band |
| 36 | +connection to every node. |
| 37 | + |
| 38 | +## Decision |
| 39 | + |
| 40 | +Add an operator-driven recovery path (`maybeRecoverClusterQuorum`, ahead of the |
| 41 | +allReady-gated scale/survey steps so it runs *during* the outage). It intervenes |
| 42 | +only when **all** of the following hold: |
| 43 | + |
| 44 | +1. **Enabled for this cluster** — Cache by default; Durable only with the opt-in |
| 45 | + annotation. Availability-first vs durability-first is a per-workload choice. |
| 46 | +2. **Below a voting quorum** — from `CLUSTER NODES`, `healthyMasters*2 <= |
| 47 | + totalMasters`. With a quorum intact, gossip can (and should) heal on its own, |
| 48 | + or deliberately refuse under the Durable replica-validity factor; the operator |
| 49 | + must not race or overrule it. A minority of primaries down is left alone. |
| 50 | +3. **Stuck for the debounce** — `cluster_state != ok` continuously for |
| 51 | + `quorumRecoveryDownAfter` (45s). Gossip promotes a recoverable failure within |
| 52 | + seconds; only an unrecoverable one stays stuck this long. |
| 53 | +4. **Each dead primary is fenced from two independent perspectives:** |
| 54 | + - **k8s view** — the pod is missing, not Running, not Ready, or on a NotReady/ |
| 55 | + absent node; AND |
| 56 | + - **data-path view** — the operator cannot reach it, or reaches it and it |
| 57 | + reports `cluster_state:fail` (it has detected its own minority isolation). |
| 58 | + A primary reachable **and** reporting `cluster_state:ok` is genuinely |
| 59 | + serving and is **never** taken over. |
| 60 | + |
| 61 | +For each such shard the operator deletes the dead primary's pod (fencing it and |
| 62 | +letting the StatefulSet re-create it) and issues `CLUSTER FAILOVER TAKEOVER` on |
| 63 | +the shard's most up-to-date reachable replica. The re-created old primary rejoins |
| 64 | +through its retained data PVC, sees the higher `configEpoch`, and demotes itself |
| 65 | +to a replica — the cluster self-heals back to full redundancy. |
| 66 | + |
| 67 | +### Why this is not split-brain (the honest argument) |
| 68 | + |
| 69 | +Write safety does **not** rest on proving the old primary's process is dead — no |
| 70 | +k8s operator can do that without STONITH, and on a control-plane partition a |
| 71 | +pod on a NotReady node may still be running. It rests on a guarantee Valkey |
| 72 | +Cluster already makes: **a primary partitioned from the majority of masters for |
| 73 | +longer than `cluster-node-timeout` stops accepting writes** (it returns |
| 74 | +`CLUSTERDOWN`, because a replica on the majority side may have been promoted). |
| 75 | +The debounce (45s) is an order of magnitude above the operator's node-timeout |
| 76 | +(5s), so by the time a takeover fires, any still-running-but-isolated old primary |
| 77 | +has already stopped serving writes. The k8s fence and the data-path check are |
| 78 | +defence in depth on top of that: they decide *when* to intervene and refuse to |
| 79 | +overrule a primary that is demonstrably still serving. |
| 80 | + |
| 81 | +Residual, accepted limits: a minority primary may still answer **stale reads** |
| 82 | +until it rejoins (the Cache profile, the only one that recovers automatically, |
| 83 | +accepts this AP trade-off); and a pathological *asymmetric* partition is outside |
| 84 | +what any voteless takeover can fully rule out. Durable clusters opt in |
| 85 | +deliberately, or stay stuck for a human to resolve. |
| 86 | + |
| 87 | +## Consequences |
| 88 | + |
| 89 | +- The Cluster topology recovers automatically from a lost primary majority |
| 90 | + (Cache), closing the AR1 SPOF, with a clear, auditable trail (structured logs |
| 91 | + + the `failover_total{reason="cluster-takeover"}` metric + the |
| 92 | + `status.quorumDownSince` debounce marker). |
| 93 | +- Durable clusters keep their no-silent-loss contract by default; recovering |
| 94 | + them is a conscious opt-in. |
| 95 | +- A shard that lost **every** pod (primary and all replicas) is *not* recovered |
| 96 | + by takeover — there is nothing to promote. That is a restore, out of scope |
| 97 | + here, and is logged as such. |
| 98 | +- The feature is Cluster-only; Replication/Sentinel already fail over reactively |
| 99 | + (ADR 0004) and have no quorum to lose. |
| 100 | + |
| 101 | +## Validation |
| 102 | + |
| 103 | +Live on a dedicated k3d cluster (3 shards, 1 replica each, Cache profile, |
| 104 | +Valkey 8.0): |
| 105 | + |
| 106 | +1. Loaded 500 keys; cordoned all nodes; force-deleted 2 of 3 primary pods so they |
| 107 | + stayed Pending (fenced) while their replicas kept running — a true majority |
| 108 | + loss (`cluster_state:fail`, `healthy=1/3`). |
| 109 | +2. The operator armed the debounce (`quorum lost and recoverable; starting |
| 110 | + recovery debounce`), waited 45s, then issued `CLUSTER FAILOVER TAKEOVER` on |
| 111 | + both surviving replicas. |
| 112 | +3. Result: `cluster_state:ok`, all 16384 slots served, `cluster_size:3`, **500/500 |
| 113 | + keys intact**. The takeover replicas carried `configEpoch` bumped above the |
| 114 | + fenced primaries. |
| 115 | +4. Uncordoned: the two fenced primaries rescheduled (retained PVC) and rejoined |
| 116 | + as **replicas** of the new primaries — full redundancy restored, no operator |
| 117 | + action needed. `status.quorumDownSince` cleared on `cluster_state:ok`. |
0 commit comments