Fencing Tokens: Preventing Stale Operations in Distributed Systems
The coordination layer for autonomous agents. When your AI systems need to elect a leader, vote on decisions, or prevent conflicts — Arbiter provides the infrastructure for machines to reach agreement.
January 10, 2025
Distributed locking is a fundamental pattern for coordinating access to shared resources. But traditional locks have a dangerous flaw: they don't prevent stale operations from agents that have lost their locks.
The Problem: Stale Operations
Consider this scenario:
-
Agent A acquires a lock on a shared database.
-
Agent A experiences a long garbage collection pause or network partition.
-
The lock expires, and Agent B acquires it.
-
Agent A "wakes up" and tries to continue operating, corrupting shared state.
The Solution: Fencing Tokens
Fencing tokens solve this problem. When an agent acquires a lock, it receives a fencing token—a number that only ever increases globally. Before performing any protected operation, the agent must present this token.
Downstream services (databases, APIs) track the highest token they've seen and reject operations from stale tokens. Even if a crashed agent "wakes up," it can't corrupt data because its token is stale.
How Arbiter Implements Fencing Tokens
Arbiter issues fencing tokens on-chain, ensuring they're globally monotonic. Each lock acquisition increments the global token counter, and the token is recorded on-chain along with the lock holder. This provides cryptographic proof that can't be forged or replayed.
When an agent performs a protected operation, it includes its fencing token. The downstream service checks the token against the highest it has seen. If the token is lower, the operation is rejected—preventing stale operations from corrupting state.
Best Practices
- Always present fencing tokens to downstream services
- Track the highest token seen in your service
- Reject operations with stale tokens
- Verify tokens on-chain for critical operations
Conclusion
Fencing tokens are essential for reliable distributed locking. They prevent one of the most dangerous failure modes in distributed systems: stale operations from agents that have lost their locks. Arbiter makes fencing tokens easy to use and cryptographically verifiable.