A number that grows with every lock, so storage can reject a client that woke up too late.
A fencing token is a number that goes up every time a lock is given out. The client sends it with every write, and the storage rejects any write whose number is lower than one it has already seen.
A distributed lock tells a client "you are the only one allowed to do this now". With a lease, that promise has an end time. The trouble is that the client cannot tell for sure whether the end time has passed. A long garbage collection pause, a slow disk, or a network delay can make a client act after its lease is gone. This was described by Martin Kleppmann in his well-known analysis of Redis-based locks.
A fencing token solves this by moving the final check to the thing being protected. Every time the lock service grants the lock, it also hands out a monotonically increasing number: 33, then 34, then 35. The client attaches that number to each request. The storage remembers the highest number it has accepted, and refuses anything lower.
| Source | How it grows | Notes |
|---|---|---|
| Lock service counter | +1 on every grant | The simplest: ZooKeeper zxid or version, etcd revision |
| Consensus term or epoch | +1 on every election | Used by Raft, Paxos and primary-backup databases |
| Database sequence | Next value of a sequence | Fine if one database is the source of truth |
| Timestamp | Wall clock | Risky: clocks can move backwards or disagree. Prefer a counter. |
A simplified model with fixed numbers (tokens 33 and 34). A real lock service hands out a bigger number on every grant, and the storage remembers the highest one it has accepted.
UPDATE doc SET body = ?, token = ? WHERE id = ? AND token <= ?. If no row changes, the write was stale.| Term | Meaning in easy words |
|---|---|
| Fencing token | A growing number given with each lock grant, used to reject old owners |
| Lease | A lock that expires after a set time |
| Stale owner | A client that still thinks it holds a lock it has lost |
| Monotonic | Only ever goes up, never down |
| Epoch / term | A counter for each leadership period, the same idea as a fencing token |
| Compare-and-set | A write that only succeeds if a value is still what you expect |
| GC pause | A freeze while a runtime cleans up memory, often a cause of stale owners |
| Zombie | A process that was thought dead but is still running |
| Fencing (STONITH) | Cutting off a bad node, for example by powering it down, so it cannot do harm |
| Highest token seen | The number the storage remembers and compares against |
| Choice | We gain | We lose |
|---|---|---|
| Lease only | Simple | Unsafe if the owner pauses |
| Lease + fencing token | Safe even with pauses | Storage must check tokens |
| Counter token | Strict order, no clocks | Needs a reliable counter |
| Timestamp token | Easy to produce | Clock errors can break the order |
| Short lease | Fast failover | More false expiries during pauses |