Notesdistributed🌱 seedling

Fencing Token

Also: Fencing · Epoch Number · Sequencer · Generation Number · Zombie Fencing

A number that strictly increases with every grant of a lock or lease, sent with each write so the resource can reject writes from any holder older than the newest it has seen.

Why it exists

A lease holder can pause, or be cut off, past its expiry and then act as if it still holds the lease (Process Pause). The holder can't be trusted to know; the resource can be made to check.

How it works

  • The lock service returns a token with each grant: 33, then 34 to the next holder.
  • Every write carries the token. The resource remembers the highest token seen and rejects anything lower.
  • The zombie (token 33) is refused even if it believes its lease is valid.
  • Tokens must come from a linearizable source (Consensus), or two holders could get the same number.

Trade-offs

Gains Costs
safe under any pause or delay length the resource must understand and check tokens
simple integer comparison needs a consensus-backed lock service to issue tokens
also stops stale leaders after failover a resource that can't check (plain files, old APIs) gets no protection

In practice

  • Chubby calls them sequencers; ZooKeeper offers the zxid or a znode version; etcd revisions serve the same role.
  • Apache Kafka fences zombie controllers, partition leaders and transactional producers with epochs.
  • Amazon S3 (since November 2024): conditional writes with If-Match on the ETag reject a writer working from a stale version.
  • Redlock debate (2016): Kleppmann argued Redlock is unsafe for correctness because it issues no fencing token and assumes bounded pauses and drift; Sanfilippo replied that it checks elapsed time after acquiring and that a resource able to compare-and-set barely needs the lock.

Pitfalls

  • Using a lock for correctness without fencing. Fine for efficiency (avoid duplicate work), unsafe for exclusive writes.
  • Generating tokens from wall-clock time: skew breaks monotonicity.

Recall

These cards are also in your review deck, scheduled with spaced repetition.