AlgoViz

When yesterday’s leader wakes up

medium 6 min

A compaction worker pauses while holding a lease.

Video lesson · Chapter 1 of 5

The projector has two directors

0:00 / 3:47

Yesterday’s leader writes again

1:13Reel

Theory

Read or listen first. About 1 min.

What it is

A lease grants temporary authority. Expiry changes what the coordinator knows, but a process paused by the operating system cannot observe that change until it resumes. Worse, a request sent before expiry can spend longer than the lease in a network queue. Even perfect handling of the local expiry event cannot recall that request.

The storage service checks the fencing token

Old workerStorageNew workerWrite · 8Accepted · 8Highest accepted token becomes 8.
After the lease changes hands, the new worker writes with token 8. Storage records 8 as the highest accepted generation.

Why it exists

Imagine a print shop where each editor can publish a new cover. Replacing the editor at the front desk will do little if the printer still accepts jobs from the old editor. The printer must recognize which generation a job belongs to.

View diagram · Publication authority
Editor A → Fence check (token 7); Editor B → Fence check (token 8); Fence check → Generation (compare); Fence check → Manifest (publish if current)
The stored fence rejects seven after eight.
Full size

Words to know

Lease
A time-limited grant of authority.
Fencing token
An ordered generation a resource can compare against prior owners.

How it works

Step through the idea, or press Play.

Step 1 of 3

Issue generations. Give each new owner an increasing token from a coordinator that orders ownership changes. A timestamp from a worker’s local clock cannot establish that order.

Understand it

Start here

Two editors share a press

A printing press accepts covers from whichever editor owns the current edition. Follow the envelope that editor A sends late, after editor B has already delivered a replacement.

Two editors share a press
Two editors, one printing pressEditor Aedition 7Pressedition 7Editor BwaitingA prepares an old cover

Compare and write at the destination.

Structure

Publication authority

Publication authority

Editor A → Fence check (token 7); Editor B → Fence check (token 8); Fence check → Generation (compare); Fence check → Manifest (publish if current)
The stored fence rejects seven after eight.

The paused publisher

Issue token 7 to A and suspend it just before publication. Let B publish a new manifest with token 8. Resume A. Read the manifest after A’s write attempt: it must still contain B’s contents and token 8. A health check on A would miss this failure.

Where a stale write stops

Two publishers, one manifest

EventToken at storageVisible contents
A prepares an update7First draft
B acquires a newer lease7First draft
B publishes8B’s revision
A’s delayed write arrives8B’s revision survives

Make the check and write indivisible

If storage reads the latest token, releases a lock, and later writes the contents, an older writer can slip between those operations. Put the comparison and mutation in one conditional write. The fence must survive restart with the contents it protects. If a restored store forgets its accepted generation, queued requests from old owners become dangerous again.

A fence has a precise boundary

An increasing token protects a resource once it has observed the newer generation. Merely issuing token eight at the coordinator does not tell a separate store about it. A system requiring immediate revocation needs a stronger coordination path. For ordinary handoff, make installation of the new generation part of the takeover before relying on its effects.

Guard the publication

  • Compare and write at the destination.

Check before sending

  • A packet can be delayed after the check.

Summary

The whole topic in one minute.

Ownership changes at the coordinator. Protection takes effect at the resource that checks and persists the generation.

Sources