# 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.

- Author: Zeeshan Adil (https://www.internalswithzee.com/about)
- Published: 2026-11-12
- Source: https://www.internalswithzee.com/concepts/fencing-token

Also called: fencing epoch.

## 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.

```sql
-- 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](/challenges/distributed-systems/fencing-a-zombie-worker), 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.

## Related

- Challenge to run: https://www.internalswithzee.com/challenges/distributed-systems/fencing-a-zombie-worker
- Article: https://www.internalswithzee.com/blog/choosing-the-right-lock
- Related concept: https://www.internalswithzee.com/concepts/idempotency-key
- Related concept: https://www.internalswithzee.com/concepts/at-least-once-delivery

## Sources

- [Martin Kleppmann, How to do distributed locking (2016)](https://martin.kleppmann.com/2016/02/08/how-to-do-distributed-locking.html)

