- Category: A. Data-Intensive Design
- English term: Fencing Token and Generation Number
- Related: Lease (Leased Lock) · Compare-and-Swap (CAS) and Optimistic Concurrency Control · Split-Brain Avoidance and Leader Election · Uncertain State / In-Doubt State · Clock Skew and Monotonic Clocks
General Meaning
In distributed systems utilizing distributed locks or time-limited leases, guaranteeing that exactly one client is actively executing at any moment is notoriously difficult. A client holding a lease might experience a prolonged stop-the-world garbage collection pause, thread starvation, or network delay, causing its lease to expire quietly while another worker acquires a new lease.
When the paused client eventually awakens, unaware that its ownership elapsed, it attempts to write its stale results to storage. A fencing token resolves this hazard by issuing a monotonically increasing generation number with each lease grant. The receiving storage engine verifies incoming tokens against its highest acknowledged generation, instantly rejecting any mutation carrying an obsolete token.
Why It Matters
- Reliably rejecting delayed out-of-order writes: Even if an expired worker wakes up after a timeout and attempts to commit state, the storage layer immediately rejects the write due to its stale generation number.
- Decoupling correctness from client clocks and pauses: Safety does not rely on synchronized client wall clocks or unbroken process execution, as validation is enforced atomically at the shared storage layer.
- Mitigating split-brain hazards: During transient network partitions where two nodes might briefly consider themselves active, only the node possessing the highest generation token can successfully commit.
How the Site Factory Uses It
The Site Factory enforces generation numbers and fencing tokens across both service leases and agent-to-agent (A2A) execution workflows. In dispatch/src/service-lease.js, acquiring a service lease increments the generation (generation=generation+1) and issues a unique token, causing any renewal or release attempts with stale generations to fail with HTTP 409. Furthermore, dispatch/src/producer-fence.js inspects the X-Honbu-Service-Lease header sent by patrol runners, rejecting writes to Cloudflare D1 unless backed by the active generation. In dispatch/src/a2a-claim.js and a2a-evidence.js, job claims increment job generation, rejecting delayed task completions as stale_or_foreign_lease.
Rather than evaluating timestamps in application memory upon HTTP receipt, producer-fence.js embeds SQL guards utilizing D1 transaction statement time (strftime('%Y-%m-%dT%H:%M:%fZ','now')) alongside active generation and token checks (EXISTS (...)). This guarantees that delayed HTTP packets arriving over congested networks cannot perform unauthorized database writes under lapsed credentials.
Caveats & Operational Rules
- Client-side self-checks cannot prevent time-of-check to time-of-use (TOCTOU) races: A client verifying its lease before issuing a write might pause right after the check; fencing tokens must be validated atomically inside the datastore engine at the exact moment of mutation.
- Pair generation numbers with unpredictable secret tokens: Because incrementing integers alone could be vulnerable to spoofing or misconfiguration, the Site Factory pairs the generation counter with a cryptographically unique UUID token.
- Audit-log all rejected stale operations: Requests rejected with HTTP 409 due to stale tokens should not be silently swallowed; recording structured entries in journals is vital for identifying latency spikes and network anomalies.
Related Terms
- Lease (Leased Lock)
- Compare-and-Swap (CAS) and Optimistic Concurrency Control
- Split-Brain Avoidance and Leader Election
- Uncertain State / In-Doubt State
- Clock Skew and Monotonic Clocks
References
- Book: Martin Kleppmann, *Designing Data-Intensive Applications*, Chapter 8 (The Trouble with Distributed Systems: Fencing Tokens)
Verified: 2026-11-08