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.
Builds on Leader and follower replication, Load balancing.
- 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.
- A sends a contact or progress message every second.
- The coordinator records when it last received one.
- After three seconds without contact, the coordinator suspects a problem.
- 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:
- Heartbeats arrive every second, with no scheduling or network delay in this simplified example.
- A crash just after contact is suspected roughly three seconds later.
- 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.
- Real scheduling, network, and receiver delays change those times.
- 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:
- A receives permission number
41from the chosen ownership protocol. - After a valid takeover, B receives the newer number
42. A timer by itself must not invent legitimate ownership. - B submits its manifest with
42. The protected resource checks the permission number and saves both42and the accepted result together. - A resumes and submits its old manifest with
41. - The resource sees that it has already accepted
42and rejects41. 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:
- Have workers write separate output objects instead of overwriting one shared object.
- Publish the chosen object through a protected database record that names the accepted manifest.
- Update that record only while checking and saving the permission number together.
- Readers use the object named by the accepted record. Old bytes may exist, but cannot become the chosen result without accepted publication.
- 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
Follow the numbered arrows
- Worker A → Heartbeat detectorperiodic progress stops
- Heartbeat detector → Ownership protocolrequest takeover, not proof of death
- Ownership protocol → Worker Bgrant newer permission number
- Worker B → Protected manifestsave 42 and result together
- A resumes → Protected manifestreject 41 after 42
Trace the diagram in words
- Let 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.
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.
- Explain what the record saves and checks for both attempts.
- Explain what readers use.
- Explain how A’s abandoned output can eventually be removed.
Compare the worked solution
- On B’s accepted publication, save permission number
8and the chosen manifest reference together in the protected record. - On A’s later attempt, compare its
7with the saved8as part of the same guarded update. Reject7; do not replace B’s reference. The saved permission number must survive the covered failures. - Readers use only the output named by the accepted record. A’s separate bytes may remain stored without becoming the published result.
- Cleanup checks that no accepted record references A’s output and that retention rules permit removal, then deletes it safely.
- 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?
- Expected contact did not arrive before the threshold.
- A crash, pause, delay, overload, or network partition could explain it.
- 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?
- The takeover protocol grants B a newer permission number.
- Storage saves that number with B’s accepted result.
- A later write carrying A’s older number is rejected.
This enforced check is fencing. A number in an unchecked header provides no protection.
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.
CLOSE THE LOOP
Explain it without the page.
Summarise the mechanism, one trade-off, and one failure mode. Then mark this chapter complete.