Skip to content

Lesson 10 of 13 · 15m

A Lock Without a Fence Is a Suggestion

Leases expire while their holder is frozen mid-write; only a monotonically checked fencing token stops the deposed leader from writing anyway.

A process acquires your distributed lock and starts writing to the shared resource. Mid-write, a stop-the-world GC pause freezes it for sixty seconds. The lease expires. A second process acquires the lock and begins its own writes. Then the first process wakes — it has no way to know it was deposed — and finishes the write it started. Two writers, one lock, and the lock did exactly what it promised: the lease expired honestly. The corruption is real anyway.

That scenario is not hypothetical flavor; it is the concrete example Martin Kleppmann used in "How to do distributed locking" (February 2016), the post that started the Redlock fight. antirez replied defending Redlock's clock assumptions; Kleppmann's follow-up stood on the mechanism: pauses are unbounded, clocks are skewed, and a client that holds no valid lease can still issue a perfectly formed write to a resource that does not know the leadership changed.

The token is the point

The fix is a fencing token: a number issued by the lock service that increases with every grant — ZooKeeper's zxid or the znode's version, etcd's CreateRevision on the lock key, a Postgres sequence if your lock is a table. The holder presents the token with every write, and the storage layer — the thing being protected — rejects any write carrying a token older than the newest it has seen. The lock itself cannot provide this guarantee. Only the resource can refuse the stale writer, which is why "we have a distributed lock" is a sentence about the beginning of the story, not the end.

Note where the check must live. Validating the token inside the lock client is worthless — the deposed leader's client library still thinks it holds the lock. The validation has to happen in the datastore, the file system, the upstream service: the component that would otherwise execute the stale write. If the resource cannot check a fence, no locking protocol on top of it is safe. This is why Pacemaker's STONITH exists — "shoot the other node in the head" — physically power-cycling a maybe-alive peer before taking over its disks, because when you cannot fence by token you fence by making the old writer actually dead.

Elections give you a leader, not a guarantee

Kubernetes' leaderelection package grants a Lease object in coordination.k8s.io/v1 (defaults: LeaseDurationSeconds 15, RenewDeadline 10, RetryPeriod 2). ZooKeeper recipes use ephemeral znodes — the session dies, the node vanishes, a new leader is elected. etcd leases work the same way via KeepAlive. All of these provide at most one leader among participants able to reach quorum. None of them retract the old leader's ability to write — the frozen process cannot be reached to be told it lost. Election and fencing are two different mechanisms and production systems need both.

Meanwhile flock() on NFS is not the per-server stub it is assumed to be — since Linux 2.6.12 the client emulates it via an NLM byte-range lock, so it does coordinate across hosts, though it stays advisory and fragile through crash recovery (true per-server locking is the local_lock mount option) — and generations of juniors have deployed it as a "distributed lock" anyway; a Postgres advisory lock (pg_advisory_lock) is session-scoped with no fencing token at all — fine as a mutex, not a leadership claim.

What to actually do

Whatever accepts writes must check a fence: store the epoch with the row (WHERE fence < :token), CompareAndSwap on the lock revision, or have the storage verify a token generation. Prefer ephemeral primitives — ZK ephemeral znodes, etcd leases — over TTL'd plain keys, because a lease tied to a session dies with the session instead of lingering. If the downstream resource genuinely cannot fence, make its operations idempotent and commutative so a stale writer is a replay, not corruption. And assume every lock holder can wake up late — the pause is always longer than you budgeted for, because the pause is by definition the part you did not budget for.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.