Try
↑ ↓ move↵ openesc close

Interview prep / Locks, leases and fencing

Senior · Locks, leases and fencing · System design interview question

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

Learn it by running it

The locks, leases and fencing lesson explains the mechanism behind this question, and its lab lets you change the parameters and watch the effect.

Your progress is saved in this browser. Sign in to keep it on every device. Privacy · Terms