Skip to content

Fencing

Published 4 October 2026

What fencing is

The term comes from distributed locking. The classic problem:

  1. Worker A holds a lock or lease and starts writing.
  2. A freezes for 30 seconds, from a GC pause, a network stall, or a slow disk.
  3. The lease expires, so worker B gets the lock and writes newer data.
  4. A wakes up, still believes it holds the lock, and overwrites B's newer data.

A can't detect this itself, since from its point of view no time passed. So the storage has to reject A. Every lock grant comes with a monotonically increasing number, called a fencing token. Every write carries the token, and the storage refuses any write whose token is lower than the highest it has already seen. A's write with token 33 arrives after B's write with token 34, so it's rejected. The stale writer is "fenced off."