Wiki Cluster
Failure detection, leases and failover
How Mirexa notices a dead node, why a cut-off primary stops serving before it can be replaced, and how its partitions move to in-sync copies automatically.
When a node stops answering, three mechanisms work in sequence. The failure detector notices
its silence and fences it: the cluster stops believing it. The primary lease guarantees the
node itself has stopped serving as a primary before that happens, even if it is merely cut off
rather than dead. Then failover hands each of its partitions to an in-sync copy. All three
are automatic and are timed off one cluster-wide value, the liveness timeout (T,
cluster.liveness_timeout, default 30 s, agreed once and immutable).
| Timing | Value | At the default (T = 30 s) |
|---|---|---|
| Heartbeat interval | T / 3 | 10 s |
| Fence sweep interval | T / 3 | 10 s |
| A silent node is fenced after | between T − one heartbeat interval and T + one sweep | 20 – 40 s |
| Primary lease | ¾ T from the heartbeat's send time | 22.5 s |
| Failover delay after the fence is first seen | T | 30 s |
| In-sync staleness bound | 2T + 2 s | 62 s |
A failover therefore takes about two liveness timeouts end to end — one to fence, one to wait out any lease — by construction. At the default that is about a minute; it is a trade made so that a partition is never failed over while its old primary could still be serving it.
Fencing
Every node heartbeats the control-plane leader every T/3. The leader keeps the last time it
heard from each node in memory — a heartbeat is evidence, not a decision, and never becomes a
log entry. A leader-local sweep, every T/3, proposes a fence for any node silent longer than
T. A fence moves ONLINE to OFFLINE and LEAVING to OFFLINE_LEAVING (a drain in progress
survives the fence).[1]
- Incarnations. Each registration is stamped with the log index of the entry that created it. A fence names the incarnation the detector saw and is refused if the node has re-registered since, so a node that restarted inside the window between "silence observed" and "fence applied" is not marked dead the moment after it proved it was alive.
- A new leader starts every clock at its election, so an election never fences the whole cluster for silence the new leader simply did not observe.
- A fence does nothing to the node and reassigns nothing. It stops the cluster routing to the
node: other nodes now refuse requests for its partitions with
PartitionUnavailable(0x0009) instead of redirecting clients to an address the cluster has stopped believing. - Coming back. A fenced node's heartbeats are answered
accepted=false; it re-registers with a new incarnation and isONLINEagain, keeping any partition that has not yet been failed over.
The primary lease
Fencing alone cannot stop a primary that is alive but cut off from the control plane: it never hears about the fence, and writes carrying the old epoch still match its own. A Mirexa primary therefore serves only while it holds a lease:[2]
- Each acknowledged heartbeat grants a lease of
¾ T, counted from the moment the heartbeat was sent. The leader measures silence from when it received the heartbeat — later — and fences no earlier thanTafter that, so the lease ends at least¼ Tbefore the earliest possible fence. That margin absorbs clock-rate drift between machines and a write admitted just before expiry finishing just after it. - A grant takes effect only once the node's own control-plane state has applied the position the acknowledging leader named, so a node restarted on an old log cannot serve on a stale directory.
- One missed heartbeat does not lapse a lease (they go every
T/3). - Without a lease in effect, the node refuses every partition it is primary of — reads and
writes — with
PartitionUnavailable, logs a warning once when the lease lapses and once when it returns, and exportsmirexa_cluster_primary_lease_remaining_seconds. - A copy's stale reads are lease-gated too: a node the control plane cannot vouch for answers nothing from its copies.
There is no autonomous mode. If a node loses the control plane — or the whole control plane loses quorum — its primaries stop serving within their lease. This is deliberate: a node that cannot be acknowledged cannot rule out having been replaced. The data path itself does not run through consensus, but serving as a primary requires the control plane's acknowledgement.
Measured 2026-10-08 with T = 3 s, three runs, in-process on one development laptop
(TestLease_APrimaryCutOffFromTheControlPlaneStopsBeforeItCanBeFenced): a primary cut off from
the control plane refused requests 1.71–1.77 s after the cut and was fenced 3.45–3.49 s
after it. The shipped binary is checked too: with the whole control plane killed, a live primary
refuses within its lease (TestServerBinary_APrimaryStopsServingWhenItsControlPlaneIsGone).
Not claimed: a process paused (by a GC, a VM stall, a stopped container) for longer than the
¼ T margin after checking its lease could still write after the lease ended — the caveat of
every lease. And a lease prevents split brain; it does not prevent the loss of writes acknowledged
after a copy's last pull.
Automatic failover
A leader-local loop, sweeping every T/3, plans and proposes failovers. It is on by default
and has no off switch: it moves no data (the copy already holds it) and acts only on partitions
nobody is serving.[3]
- When. A fenced node's partitions move only
Tafter this leader first saw the fence at that incarnation. The last lease the node could hold came from a heartbeat sent before that sighting, so it has ended by then with¼ Tto spare. The clock is leader-local; a new leader starts it again, which only ever waits longer. - Where. Each partition goes to a failover candidate: an in-sync copy on a node the
cluster believes. Among candidates:
ONLINEbeforeLEAVING; then the node primary of the fewest partitions, counting this plan's own moves, so a dead node's partitions spread over the survivors; then the lowest id. - Stranded partitions. A partition with no candidate is not moved — promoting a copy that was not caught up would discard everything it missed. It is logged once and stays unavailable until its node returns.
- The entry. One
FAIL_OVERentry covers all the node's movable partitions, all or nothing. It names the incarnation it was planned at and is refused if the node is no longer fenced at that incarnation — a node that came back meanwhile either kept its partitions or lost them, never both. Each partition's new epoch is the entry's log index. - Every other copy becomes divergent. After a failover, each surviving copy pulled from the
dead primary independently and may hold artifacts the new primary never received. The old
primary and every other copy are recorded as divergent: not replicas, never read, never
handed the partition again until their data is set aside. The partition reads
UNDER_REPLICATEDuntil they return. - Promotion. The new primary replays the replica log it kept as a copy — the writes the old
primary acknowledged that never reached a chunk — and refuses the partition with a retryable
Overloaded(250 ms hint) until it has. Only then does it serve.
Measured 2026-10-08 in the shipped binary with T = 3 s, three runs, one development laptop
(TestServerBinary_ADeadPrimaryIsFailedOverToItsCopy): fenced 2.9 s after a SIGKILL, failed over
3.97–4.97 s after the fence, every acknowledged candle the copy held served, and the old
primary — restarted on its old data directory — back ONLINE and redirecting the partition to
its successor.
What a failover can lose
Writes the dead primary acknowledged after its copy's last pull. A copy is a candidate only while
in sync (level within 2T + 2 s), which bounds the loss; in practice it measured at milliseconds'
worth of writes. See Replication for the numbers.
When the old node returns
A node that comes back after its partitions failed over holds data from an abandoned history. Its replica supervisor — acting only once the node holds a lease, i.e. once its view of the directory has caught up — handles it in two steps:[4]
- Quarantine. For each partition the directory lists it as divergent for, the node stops the
partition's streams, records a floor so WAL recovery never replays the abandoned records, and
moves the whole partition directory to
quarantine/partition_<id>-epoch_<e>/in one durable rename. Nothing is deleted; a quarantine is evidence. - Re-admission. Only after the quarantine has landed does the node propose
READMIT_DIVERGENTabout itself, becoming aSYNCINGcopy of the new primary, and the ordinary catch-up re-seeds it from nothing toIN_SYNC. A draining node does not ask back.
A node's files for a partition in which it no longer holds any role are likewise moved aside as
partition_<id>-epoch_<e>-released. See Reconciliation.
Partition health
What a partition can still do is projected from the directory and the registry on every read, never stored:[5]
| State | Meaning | Gauge |
|---|---|---|
serving |
Primary and every named copy on nodes the cluster believes | — |
under_ |
Primary serving; at least one copy fenced or divergent | mirexa_ |
primary_ |
Primary fenced; an in-sync copy survives on a believed node — failover can restore it | mirexa_ |
unavailable |
Primary fenced and no caught-up copy survives — back only with its node | mirexa_ |
The gauges read NaN, not zero, when a node cannot read its control-plane state.
ListPartitionHealth (a linearizable read, with an impaired_only filter) lists each impaired
partition with its failover candidates.
References
Source
cluster.Liveness,State.Fence,State.Registercluster.Lease,LeaseDuration,FailoverDelay,InSyncStaleAfterState.FailOver,State.PlanFailover,State.PartitionHealth- Fence loop, failover loop
partitionResolver,newPrimaryLease, replica supervisor, replica-log gateErrorCodePartitionUnavailable
Tests
TestState_FenceRefusesAStaleIncarnation,TestState_ADrainSurvivesTheNodeBeingFenced— /internal/cluster/state_test.goTestLiveness_ANewLeaderDoesNotFenceTheWholeCluster— /internal/cluster/liveness_test.goTestFenceLoop_ASilentNodeIsFencedWithinItsTimeoutAndNoSooner,TestDefaultFenceSweep_IsAThirdOfTheTimeout— /internal/control/fence_test.goTestLeaseDuration_EndsBeforeTheEarliestFence,TestFailoverDelay_OutlastsAnyLeaseAFencedNodeCanHold— /internal/cluster/lease_test.goTestLease_APrimaryCutOffFromTheControlPlaneStopsBeforeItCanBeFenced,TestPartitionResolver_APrimaryServesOnlyUnderALease,TestNewPrimaryLease_OnlyWhereSomethingGrantsIt— /cmd/server/lease_test.goTestServerBinary_APrimaryStopsServingWhenItsControlPlaneIsGone— /tests/server_binary_lease_test.goTestFailOver_PromotesTheCopyAndCarriesEveryOtherCopyOut,TestFailOver_RefusesUnlessThePrimaryIsStillFencedAtTheIncarnationSeen,TestDivergentCopy_IsACopyThePartitionIsMissing— /internal/cluster/failover_test.goTestFailoverSweep_WaitsOutAnyLeaseThenFailsOver— /internal/control/failover_test.goTestServerBinary_ADeadPrimaryIsFailedOverToItsCopy— /tests/server_binary_failover_test.goTestReplicaSupervisor_AFailedOverCopyPromotesWithoutWaitingOutItsCatchup— /cmd/server/failover_test.goTestReplicaSupervisor_ADivergentCopyIsQuarantinedAndNothingOfItIsDeleted,TestReplicaSupervisor_AQuarantinedCopyIsReadmittedOnlyAfterItsQuarantine,TestReplicaSupervisor_NothingIsQuarantinedOnAStateThatHasNotCaughtUp,TestDivergentCopy_ReturnsAsAnInSyncCopyOfTheNewPrimary— /cmd/server/quarantine_test.goTestListPartitionHealth_ReportsWhatAFailedNodeLeaves— /internal/control/health_test.goTestUnavailable_APartitionWithADownPrimaryIsRefusedNotRedirected— /internal/network/unavailable_test.go
Further reading
External
- Cary G. Gray and David R. Cheriton, "Leases: An Efficient Fault-Tolerant Mechanism for Distributed File Cache Consistency", SOSP 1989 — https://dl.acm.org/doi/10.1145/74850.74870
- Martin Kleppmann, "How to do distributed locking" (fencing tokens and process pauses) — https://martin.kleppmann.com/2016/02/08/how-to-do-distributed-locking.html
- KIP-631, "The Quorum-based Kafka Controller" (broker fencing by the controller) — https://cwiki.apache.org/confluence/display/KAFKA/KIP-631%3A+The+Quorum-based+Kafka+Controller
internal/control/fence.go;State.FenceandNodeStatusininternal/cluster/state.go; heartbeat interval ininternal/control/join.go. ↩︎internal/cluster/lease.go;partitionResolver.leaseInEffectandcopyReadincmd/server/resolver.go. ↩︎internal/control/failover.go;State.PlanFailoverandState.FailOverininternal/cluster/failover.go. ↩︎The replica supervisor in
cmd/server/replica.go;Layout.QuarantinePartitionininternal/storage/layout.go. ↩︎internal/cluster/health.go;registerPartitionHealthincmd/server/resolver.go. ↩︎