Zeeshan Adil
Book a call
← All concepts

CONCEPT

What is a fencing token?

A fencing token is a number that increases every time a lock or lease is granted. The protected resource remembers the highest one it has seen and rejects writes carrying a lower one, so a paused holder whose lease has expired cannot overwrite the new holder's work.

Also called: fencing epoch. By Zeeshan Adil··Distributed Systems·Databases

The problem it solves

A worker claims a job with a time-limited lease. Then it stalls: a long garbage collection pause, a hung call. The lease expires and another worker takes the job. The first worker wakes up and carries on, because from the inside no time has passed. It does not know it lost the lease. Checking "do I still hold the lock?" just before each write only narrows the window, because it can pause again right after the check.

So a lease alone cannot stop the old holder. Something at the receiving end has to.

How it works

Each time the lock is granted, a counter goes up by one, and the holder gets the new value. The holder sends that number with every write. The resource keeps the highest number it has accepted and refuses anything lower.

Code
-- the row stores the token of the last holder that wrote to it
UPDATE orders SET status = 'shipped', fence = 34
WHERE id = 7 AND fence < 34;
-- 0 rows updated: a newer holder got here first. Stop.

When the stalled worker wakes with token 33 after the new holder wrote with 34, its write matches no row.

Two things must be true. The token must come from something that issues strictly increasing numbers, such as a database counter, an etcd revision or a ZooKeeper sequence, and not a random id. And the resource must be able to compare it.

What it does not do

  • It needs the receiver to cooperate. An external service that ignores the number gets no protection. For those, send an idempotency key and check their state before retrying.
  • It stops the stale holder from winning, not from being heard. A receiver can only refuse a number older than one it has already seen. If the zombie reaches the receiver first, its call is accepted and later superseded.
  • It does not make clocks agree. It replaces trust in timing with a comparison of numbers.

Measured

In my zombie worker challenge, a real second OS process claimed a job at epoch 1 and blocked. Its lease expired and a healthy worker reclaimed the job at epoch 2. When the zombie woke up, its commit was rejected in both orderings tested: whether the new owner reached the receiver first (the zombie's call was rejected) or the zombie did (accepted, then superseded by epoch 2). The job row and the receiver both ended with the epoch 2 result and nothing from the zombie. Removing the epoch check from the commit, from the receiver, or from the claim made the test fail, so every guard was needed.

The sharding challenge uses the same idea for routing. A router with a stale map sent 200 writes to a database that no longer owned the data. With the epoch check, 1 write was refused and all 200 were readable afterwards. Without it, 0 of 200 were readable and 200 sat stranded.

RUN IT YOURSELF

Challenge · Distributed Systems ChallengesFencing a zombie worker

GO DEEPER

Choosing Between Optimistic, Pessimistic, Advisory, and Distributed LocksWhen to use SELECT FOR UPDATE, version checks, Postgres advisory locks or Redis locks, and which failures each one survives. Includes fencing tokens for expired leases.
What is an idempotency key?What is at-least-once delivery?

SOURCES

Get the next deep dive by email

New deep dives on how production systems really behave, from Kubernetes networking to PostgreSQL internals. About one email a month.

You will get one email to confirm. Unsubscribe from any email with one click. Your address is used for this newsletter and nothing else.

Written by Zeeshan Adil

I run systems like this for a living. If your team is fighting the same problem, or wants this as a talk, write to me.