LearnastraTHE DESIGN COURSE

Understand the building blocks / CHAPTER 21

Heartbeats and failure suspicion

Use periodic signals to detect missing progress while keeping failure detection separate from safe authority.

Advanced1h of deliberate study10 questions

Builds on Leader and follower replication, Load balancing.

By the end, you can
  • Explain why a missed heartbeat is suspicion rather than proof.
  • Choose a detection threshold using delay and false-positive costs.
  • Explain how storage rejects an old worker’s write after a legitimate takeover.

01Silence is an observation, not a death certificate

Worker A builds an export made of several output files. A manifest lists the completed files that readers should use. A coordinator watches A’s progress and may start a replacement if work stops.

  1. A sends a contact or progress message every second.
  2. The coordinator records when it last received one.
  3. After three seconds without contact, the coordinator suspects a problem.
  4. It does not yet know whether A crashed, paused, lost its network path, or is waiting behind an overloaded receiver.

That periodic signal is a heartbeat. Its contents define what it tells the receiver:

  • An “alive” signal says the signal-sending part of the process responded.
  • A readiness signal can report whether required requests can be served.
  • A progress signal can report the last completed export chunk or applied log position.

A control thread can keep sending “alive” while the export is stuck. Monitor useful progress as well as contact: how long since the last completed chunk, for example?

Without a known bound on message delivery, silence cannot identify the cause with certainty. Recovery must stay safe even when the old worker is still running and later resumes.

02Trade detection speed against unnecessary takeover

Choose the suspicion threshold from measured delays and the cost of a mistaken takeover.

  • A shorter threshold finds some real failures sooner, but treats more ordinary pauses or delays as failures.
  • A longer threshold avoids some unnecessary takeovers, but leaves genuine failures waiting longer.

Work the idealized timing separately from total recovery:

  1. Heartbeats arrive every second, with no scheduling or network delay in this simplified example.
  2. A crash just after contact is suspected roughly three seconds later.
  3. A crash just before the next expected contact is suspected roughly two seconds after the crash, because almost one second has already elapsed since the last contact.
  4. Real scheduling, network, and receiver delays change those times.
  5. After suspicion, selecting an authorized replacement, recovering saved state, preventing old writes, and redirecting work take additional time.

Other detector improvements have limited purposes:

  • Adapting a threshold to observed arrival times can reduce unnecessary suspicions under its assumptions.
  • Randomizing election timers can reduce repeated simultaneous election attempts.
  • Measuring elapsed waiting with an appropriate local timer avoids treating a wall-clock adjustment as elapsed time.

None makes silence proof of death. Test long process pauses and one-way message loss as well as killing a process. Different participants can receive different signals and hold different beliefs while the original worker still acts.

03Prevent a resumed old owner from making stale changes

A dangerous sequence is: A pauses, the coordinator starts B, then A resumes and overwrites B’s manifest. Missing contact did not stop A. Protect publication at the storage operation itself.

Use a saved permission number that increases with legitimate takeovers:

  1. A receives permission number 41 from the chosen ownership protocol.
  2. After a valid takeover, B receives the newer number 42. A timer by itself must not invent legitimate ownership.
  3. B submits its manifest with 42. The protected resource checks the permission number and saves both 42 and the accepted result together.
  4. A resumes and submits its old manifest with 41.
  5. The resource sees that it has already accepted 42 and rejects 41. A cannot overwrite the newer accepted result just by arriving later.

The increasing permission number is a fencing token. The check and result update must be indivisible at the resource, and the saved number must survive the failures covered by the contract. A client-side check can pause before writing, so it does not provide the same protection. A random request identifier does not order ownership changes.

If the file store cannot perform this check:

  1. Have workers write separate output objects instead of overwriting one shared object.
  2. Publish the chosen object through a protected database record that names the accepted manifest.
  3. Update that record only while checking and saving the permission number together.
  4. Readers use the object named by the accepted record. Old bytes may exist, but cannot become the chosen result without accepted publication.
  5. Clean abandoned objects only after checking references and the retention policy.

Keep four tasks distinct: detect missing progress, authorize a replacement, reject obsolete writes, and recover unfinished work. Heartbeats mainly help with the first. Test A resuming after B publishes; safety must survive a wrong suspicion.

A wrong suspicion must not permit a stale overwrite

A wrong suspicion must not permit a stale overwriteLet A remain alive while its heartbeat messages stop. The ownership protocol legitimately gives B permission number 42. The resource saves 42 together with B’s accepted manifest. Resume A with 41 and verify that the resource rejects its publication. Numbered arrows correspond to the connection key below. All connections are directional.Worker APermission 41; thenpausesHeartbeatdetectorMissing contactraises suspicionOwnershipprotocolGives B newerpermission 42Worker BPublishes with 42ProtectedmanifestSaves result withpermission 42A resumesAttempts write with4112345A wrong suspicion must not permit a stale overwriteLet A remain alive while its heartbeat messages stop. The ownership protocol legitimately gives B permission number 42. The resource saves 42 together with B’s accepted manifest. Resume A with 41 and verify that the resource rejects its publication. Numbered arrows correspond to the connection key below. All connections are directional.Worker APermission 41; thenpausesHeartbeatdetectorMissing contactraises suspicionOwnershipprotocolGives B newerpermission 42Worker BPublishes with 42ProtectedmanifestSaves result withpermission 42A resumesAttempts write with4112345A wrong suspicion must not permit a stale overwriteLet A remain alive while its heartbeat messages stop. The ownership protocol legitimately gives B permission number 42. The resource saves 42 together with B’s accepted manifest. Resume A with 41 and verify that the resource rejects its publication. Numbered arrows correspond to the connection key below. All connections are directional.Worker APermission 41; thenpausesHeartbeatdetectorMissing contactraises suspicionOwnershipprotocolGives B newerpermission 42Worker BPublishes with 42ProtectedmanifestSaves result withpermission 42A resumesAttempts write with4112345

Follow the numbered arrows

  1. Worker A → Heartbeat detectorperiodic progress stops
  2. Heartbeat detector → Ownership protocolrequest takeover, not proof of death
  3. Ownership protocol → Worker Bgrant newer permission number
  4. Worker B → Protected manifestsave 42 and result together
  5. A resumes → Protected manifestreject 41 after 42
A may be paused rather than dead. B’s accepted publication saves permission number 42 with its result. When A resumes with 41, the protected resource rejects that older permission so A cannot replace B’s manifest.
Trace the diagram in words
  1. Let A remain alive while its heartbeat messages stop.
  2. The ownership protocol legitimately gives B permission number 42.
  3. The resource saves 42 together with B’s accepted manifest.
  4. Resume A with 41 and verify that the resource rejects its publication.

DRAW. CALCULATE. EXPLAIN.

Resume an old export owner

20 minutes · guided practice

Worker A has permission number 7 and pauses. A valid takeover gives B number 8. B publishes manifest M through a protected database record that names the accepted output. A resumes with a different output object and tries to publish.

  1. Explain what the record saves and checks for both attempts.
  2. Explain what readers use.
  3. Explain how A’s abandoned output can eventually be removed.
Your self-check
Compare the worked solution
  1. On B’s accepted publication, save permission number 8 and the chosen manifest reference together in the protected record.
  2. On A’s later attempt, compare its 7 with the saved 8 as part of the same guarded update. Reject 7; do not replace B’s reference. The saved permission number must survive the covered failures.
  3. Readers use only the output named by the accepted record. A’s separate bytes may remain stored without becoming the published result.
  4. Cleanup checks that no accepted record references A’s output and that retention rules permit removal, then deletes it safely.
  5. The heartbeat triggered suspicion. Legitimate takeover and storage-enforced publication checks preserve the result when A returns.

The checkboxes record your own assessment. The worked solution gives you a way to check the reasoning.

RETRIEVE BEFORE YOU REVEAL

Keep the important ideas.

Say the answer aloud, then turn the card over.

CARD 01What does missing contact prove?
  1. Expected contact did not arrive before the threshold.
  2. A crash, pause, delay, overload, or network partition could explain it.
  3. The old worker may still resume.

A safe takeover cannot depend on silence proving death.

CARD 02How do I select a threshold?
  • Shorter thresholds can find real failures sooner but cause more mistaken takeovers.
  • Longer thresholds can avoid some mistakes but delay recovery.

Measure contact delays and pauses. Include replacement selection, old-write rejection, and state recovery in the total recovery time.

CARD 03What makes fencing real?
  1. The takeover protocol grants B a newer permission number.
  2. Storage saves that number with B’s accepted result.
  3. A later write carrying A’s older number is rejected.

This enforced check is fencing. A number in an unchecked header provides no protection.

Practise this topic in the revision deck ↗

CHECK THE REASONING

Can you explain the choice?

Answer every question, then finish for an immediate score and an explanation for each answer. Aim for 80%, then revisit any gaps.

1 / 10A misses heartbeats while paused but its process remains live. What did the detector establish?
2 / 10B’s token 42 was accepted; A later sends token 41. What should the protected resource do?
3 / 10The coordinator grants B token 42, but the resource ignores tokens. What protection exists against A’s stale write?
4 / 10A 3-second suspicion threshold is met. What can you promise about total recovery time?
5 / 10What is a cost of reducing the suspicion threshold?
6 / 10The control thread responds but the export makes no progress. What should monitoring add?
7 / 10What distinguishes a fencing token from a random request ID?
8 / 10Why test a one-way partition rather than only killing A?
9 / 10Which event legitimately starts a replacement’s ownership?
10 / 10An object store cannot enforce a token condition on the final side effect. What is appropriate?
0 of 10 answered

CLOSE THE LOOP

Explain it without the page.

Summarise the mechanism, one trade-off, and one failure mode. Then mark this chapter complete.

Go to the source