Highlights

Contents

Part IV
04

Heartbeats, leases and fencing

We distinguish missing contact from a stopped process. A healthy replacement can overlap with an old worker that resumes. We will build safe takeover using suspicion, leases, and resource checks.

CHAPTER ATLAS / FOLLOW THE READING PATHCHAPTER ATLAS / FOLLOW THE READING PATH1CAP theorem and networkpartitions2Consistency models3Leader, multi-leader,leaderless4Heartbeats, leases andfencing5Consensus6Raft7ZooKeeper, etcd and splitbrain8Case study: onereplicated write

#Section 13Heartbeats measure recent contact

Heron’s publisher sends a heartbeat and then pauses during garbage collection. The coordinator receives nothing while the publisher still exists. A second publisher can start before the first resumes. The challenge is not merely finding a replacement. It is ensuring that the old process cannot corrupt work after the replacement becomes active.

A heartbeat is a periodic signal of recent contact. A failure detector converts missed signals into suspicion according to a rule. It can suspect a crashed process, but it can also suspect one delayed by scheduling, network congestion, disk stalls, or an overloaded receiver. In an asynchronous model, a finite silence cannot establish which explanation is true.

Assume an illustrative heartbeat period of 1 second and a suspicion threshold of 3 seconds since the last receipt. A signal received at time 0 gives a suspicion deadline at time 3. A process paused until time 4 is suspected despite resuming later. Lowering the threshold shortens detection but makes shorter innocent delays trigger the same action.

The threshold must be tied to observations and the consequences of suspicion. Suspecting a read-only worker can remove it from routing. Suspecting a writer can trigger a takeover protocol with a new authority epoch. Neither action should assume the old process has ceased execution. The protected resource still needs a way to distinguish old and new authority.

Monitor both detection delay and false takeovers. Repeated leadership churn can make a cluster unavailable even while most machines remain alive. A detector is useful input to a protocol, not the safety proof of that protocol. We will make this distinction visible in the ownership record and resource-side checks in the following sections.

HEARTBEAT SILENCE IS EVIDENCE OF LOST CONTACT, NOT PROOF OF DEATHHEARTBEAT SILENCE IS EVIDENCE OF LOST CONTACT, NOT PROOF OF DEATH01234suspectoldresumesa paused old worker can return after replacement is proposedownership needs enforcement at the effect boundary
Figure 5.16Illustrative heartbeat timeline. Recent contact, silence, suspicion and resumed execution are different observations; suspicion does not stop an old worker from issuing requests.

#Section 14A lease expires while its holder is paused

Publisher A acquires a lock, checks it, and pauses just before writing. Its permission expires while it is paused. Publisher B acquires the role and writes a newer score. A then resumes at the instruction after its earlier check. That old check cannot know what happened during the pause.

A distributed lock coordinates competing processes through shared state. A lease grants ownership for a limited interval and requires renewal. The service can expire a lease and reassign ownership, but it cannot reach into a paused client and remove instructions already queued there. Time-limited permission therefore needs an enforcement story at the resource receiving the effect.

Assume A receives a 3-second lease at time 0 and pauses at time 2. The coordinator expires it at time 3 and grants B ownership. A resumes at time 4, which is 4-3=1 second beyond the illustrative expiry. Even if A checks its clock on resumption, another pause can occur between that check and the external write.

Some lease protocols rely on bounded clock drift, bounded communication delays, or a carefully chosen conservative client deadline. Those are assumptions to state and test, not facts created by adding TTL to a key. A lease held through a coordination service also depends on that service’s session and quorum behavior. Different APIs expose different boundaries.

Heron therefore treats a lease as the way to propose who should work, while the storage resource verifies an authority value with every protected mutation. A client should stop on lost ownership, but safety must survive a client that resumes late. This requirement is the reason the next section installs authority where the write will happen.

A'S LEASE EXPIRES AT TIME 3 WHILE A IS PAUSED; AT TIME 4 IT RESUMES WITHOUT AUTHORITYA'S LEASE EXPIRES AT TIME 3 WHILE A IS PAUSED; AT TIME 4 IT RESUMES WITHOUT AUTHORITY01234A holds lease (expires at 3)A paused (GC, VM stall)B acquiresA writesA checked the lease at time 2; nothing re-checks it at time 4
Figure 5.17The lease trace expires A’s permission at time 3 while A is paused; orange marks its resumed request, which lacks authority after B takes over.

#Section 15Install fencing authority at the resource

A holds token 7 and pauses. B obtains token 8 from the coordinator. It is tempting to say that all of A’s future writes are now rejected. That skips a step. If the storage resource has not received or validated token 8, a rule comparing against the highest token it has seen still knows only 7.

A fencing token is an increasing authority value carried to a resource, which atomically checks it with the protected effect. Under the highest-seen scheme, the resource stores the greatest processed token. A write carrying a smaller token fails. An equal token can permit more work by the current owner, with command identity providing a separate duplicate check.

Heron’s trace starts with stored token 7. The coordinator issues 8 to B. B sends an authority-install operation to storage; storage atomically persists highest token 8 and acknowledges it. Only after that acknowledgement does the takeover claim storage protection. A’s delayed write carrying 7 now compares 7<8 and fails before changing the score.

If A’s token-7 write arrives before storage processes token 8, highest-seen fencing alone can accept it. This is not a contradiction; the resource’s authority transition has not happened yet. A stronger immediate-revocation contract needs synchronous validation against current authority, or a coordinated cutover that waits for resource installation before exposing the new owner as active.

The comparison and effect must share one atomic boundary. Checking in the application, then making an unchecked external write recreates a race. Every protected path must enforce the rule, including retries and background workers. The Chubby paper discusses related resource validation with lock sequencers; Heron’s increasing-number scheme is an illustrative design.

B INSTALLS TOKEN 8 AT STORAGE BEFORE ACTING; A'S TOKEN 7 BOUNCESB INSTALLS TOKEN 8 AT STORAGE BEFORE ACTING; A'S TOKEN 7 BOUNCES1storage highest 72coordinator issues 83B installs 8, persisted4A writes with 7storagehighest 7highest 8877 < 8: reject before any effect
Figure 5.18The fencing trace installs token 8 at storage before takeover; orange marks the delayed token 7 write rejected before it changes the score.

#Section 16Token order and command identity solve different problems

B legitimately owns token 8 and sends the same correction twice because its first reply disappears. Both requests carry current authority. Fencing therefore cannot decide whether the second request is a duplicate. Authority and operation identity answer separate questions, and Heron needs both on the same protected write path.

An authority epoch identifies an ownership generation rather than a specific command. Heron accepts token 8 for commands c9 and c10, but records each command independently. Reusing c9 returns its existing outcome. Reusing c9 with a different payload fails validation, because otherwise one identity would refer to incompatible intended effects.

Start score 10 with highest token 8 and no c9. The first c9 adds 1 and atomically records score 11 plus the result. A second c9 returns 11 without another increment. A different c10 can add 1 and produce 12. An old token-7 request is rejected regardless of whether its command is new.

The protected transaction can first reject a token below stored authority, then compare or insert the unique command record, then apply the effect. The precise ordering of duplicate-result lookup and stale-authority rejection is an API choice, but no stale owner may create a new effect. Keep retries distinguishable from fresh work and document whether old owners can retrieve prior results.

Deduplication records require retention. Deleting one while an old request can still return turns a replay into apparently new work. A recovery design can bound the retry lifetime, retain a tombstone, or use durable source identities. None of these questions is answered by the lock TTL. At a whiteboard, label the authority field and command field separately.

THE TOKEN SAYS WHO MAY WRITE; THE COMMAND ID SAYS WHETHER IT ALREADY HAPPENEDTHE TOKEN SAYS WHO MAY WRITE; THE COMMAND ID SAYS WHETHER IT ALREADY HAPPENEDcommandtokenscorec9810 → 11appliedc9 again8stored 11recorded resultc10811 → 12appliedc117no effectold owner rejected
Figure 5.19The rows separate token order from command identity; orange marks the old token 7 owner, which cannot create a new effect.
THE STORAGE CHECKS THE TOKEN, SO A STALE OWNER IS STOPPED AT THE DOORTHE STORAGE CHECKS THE TOKEN, SO A STALE OWNER IS STOPPED AT THE DOORcoordinatorissues 8A (paused)Bhighest = 8install 8write, token 777 < 8: rejected before it changes the score
Figure 5.20The state history shows coordinator issuance, resource installation, and the stale write check; orange marks the storage-side rejection that makes takeover real.