Fencing tokens assume the resource can compare tokens. What do you do when the protected resource cannot, for example a third-party API or a file system with no version check?
Sketch your own answer first, then compare it with the one below.
A strong answer
Then a lock alone cannot make the operation safe, and the honest answer is to say so first. The usual escape is to find a version somewhere. Many stores offer compare-and-set or conditional writes on a version or an ETag, which is a fencing token under another name. Writing to a new object or file named by the token, then atomically swapping a pointer, turns an unconditional write into a conditional one.
When the operation is not idempotent and has no version, such as sending a payment to an external API, move the token into the request as an idempotency key and make the external side reject duplicates. If it cannot, the operation has to be made safe to repeat or safe to lose, or it has to go through a single serialising component that is itself replicated with consensus.
Lamport's view of ordering explains why this works. A token is a logical timestamp for the lock, and the resource needs enough state to compare timestamps. Without that comparison somewhere on the write path, the system is relying on timing, which an asynchronous network does not promise.
Sources
- Leslie Lamport (1978). Time, clocks, and the ordering of events in a distributed system.
- Martin Kleppmann (2016). How to do distributed locking.