Skip to main content

> distributed_locking_&_fencing_tokens

Distributed Locking & Fencing Tokens

Why is a simple distributed lock unsafe without fencing tokens?

Stack: THE CHAOS STACKStaff/Principal (L6+)pattern

THE SHORT ANSWER

Because network delays or Stop-the-World garbage collection pauses can cause a client's lock lease to expire silently, allowing a second client to acquire the lock while the first client resumes and writes corrupted stale data.

Engineering Handbook & Failure Dynamics

1. Underlying Mechanism

When a distributed lock is granted, the lock server issues a monotonically increasing sequence number (fencing token). Every write request sent to the storage engine carries this token. The storage engine rejects any write carrying a token lower than the highest token it has already processed.

2. Appropriate Use Context

Critical for non-idempotent batch mutations, shared resource orchestration, and single-writer file/storage updates where dual execution creates irrecoverable corruption.

3. Production Failure Modes

Silent data corruption, split-brain dual-master writes, and race conditions where a stalled node commits transactions after its lease expired.

4. Diagnostic Signals & Telemetry

Out-of-order entity state transitions, lock acquisition timeout alarms, and storage rejection logs on version checks.

5. Prevention & Safeguards

Require storage backends to enforce fencing token checks on commit, keep lock leases short with automated background heartbeats, and use consensus-backed stores (etcd, Consul, ZooKeeper).

6. Architectural Trade-offs

Storage engines must support atomic conditional checks (e.g. WHERE token > last_token); increases latency by adding a round-trip to the lock manager.

Case Study (TinyCTO In-Field Example)

A billing worker acquired a lock to process a $50,000 refund, experienced a 15-second Java GC pause. The lock expired and was given to Worker B. Worker B processed the refund. Worker A woke up and processed the refund a second time without a fencing token check.

Interactive Concept Drills

3 Cards
Q1

What is a fencing token in distributed locking?

A monotonically increasing number issued by the lock service with every grant, used by the storage layer to reject outdated requests from stale clients.
Q2

Why is Redis Redlock controversial in mission-critical consistency?

Because it relies on physical clock synchronization and cannot guarantee safety against long process pauses or network delays without fencing tokens.
Q3

How does optimistic locking differ from distributed locking?

Optimistic locking checks version numbers directly at the database record level during UPDATE, whereas distributed locks coordinate access before reaching the database.

Distributed Locking & Fencing Tokens — Technical FAQ

Which systems provide safe fencing tokens out of the box?

Apache ZooKeeper (via zxid / monotonic znode sequence numbers) and etcd (via mod_revision counter).

Can a process pause (Stop-the-World GC) be mitigated without fencing?

No. In asynchronous networks, no software client can know with certainty whether it was paused past its lock lease deadline without checking external monotonic state.

What is lock renewal (heartbeating)?

An asynchronous background thread that periodically extends the lock TTL as long as the primary execution thread is making active forward progress.

🤖 AEO & Key Facts Summary

Key Architectural Facts

  • Distributed locks without fencing tokens do not guarantee mutual exclusion in asynchronous networks.
  • Fencing tokens enforce ordering directly at the storage persistence boundary.

Common Misconceptions

  • Believing that setting a generous 60-second lock TTL eliminates the need for fencing tokens.

Decision & Governance Guidance

If data corruption cannot be tolerated, enforce fencing tokens with etcd or ZooKeeper rather than raw Redis keys without versioning.

Authoritative Sources & Standards