Split-Brain Avoidance and Leader Election

Mechanisms that safely elect a single coordinator ('leader') among multiple machines and prevent 'split-brain' disasters where network partitions cause two nodes to act as leader simultaneously.

General Meaning

In redundant multi-machine systems, a single active coordinator must often be chosen to govern shared state—a process called Leader Election.

However, network partitions or process pauses create a dangerous hazard: an isolated former leader may believe it is still in charge while the remaining nodes elect a new leader. Having two active leaders simultaneously—known as Split-Brain—leads to conflicting commands, duplicate job execution, and corrupted databases.

Why It Matters

  • Preventing conflicting writes and data corruption: Stops two leaders from simultaneously mutating the same database or task ledger.
  • Preventing contradictory commands: Avoids race conditions where two machines commit to the same Git branch or dispatch the same ticket twice.
  • Ensuring safe failover: Allows a previously paused leader to detect that it has been superseded and step down quietly without disrupting the new leader.

How the Site Factory Uses It

The Site Factory deploys management daemons across all three Macs (Mac mini, Studio 1, Studio 2) and prevents split-brain through five mechanisms: (1) Centralized D1 leader election (/v2/services/<service>), where only the single Mac that acquires the service lease runs patrol or macgakari; (2) Monotonically increasing integer generations (fencing tokens) incremented on every lease acquisition so requests from a stale generation are rejected server-side as stale_or_foreign; and (3) Immediate halt on HTTP 409 Conflict, raising HONBU_SERVICE_LEASE_STOP so a delayed former leader aborts work immediately upon seeing another holder.

Furthermore, the Site Factory enforces (4) Monotonic clock lease deadlines, adding server TTL to local time.monotonic() at request start minus a safety margin to avoid wall-clock skew; and (5) Fault-injection verification via tools/service_lease_fault_trial.py to test generation-8 holder checks, lease release, and peer takeover on live hardware.

Caveats & Operational Rules

  • Lease expiration time alone cannot prevent split-brain: If an old leader is mid-write when its timer expires, its request can still collide with the new leader unless the server validates generation numbers (fencing tokens).
  • Do not rely on LAN-only peer voting across 3 home Macs: Mixed Ethernet/Wi-Fi or switch glitches can partition the LAN; the Site Factory uses neutral Cloudflare D1 as the single arbiter.
  • Avoid rapid leader flapping: Hair-trigger lease takeovers waste cycles on handoff overhead; the Site Factory stabilizes leadership with a 40-minute lease TTL renewed every 5 minutes.

Related Terms

References

  • Book: Martin Kleppmann, *Designing Data-Intensive Applications*, Chapters 8 & 9 (The Truth Is Defined by the Majority; Leader Election and Fencing)

Verified: 2026-11-08