Opens in a new tab
Guide

The old leader came back. Can it still write?

In brief

An expired lease makes room for a new leader. A fencing token gives the system receiving a write a way to turn the old leader away.

3 min read

Before you read Familiarity with distributed workers, leases and atomic database writes.

Is this the right starting point?
What you will take away
Test whether a destination rejects a stale worker after a leader handoff.
Sources
Two colored markers flank a dark gate on a shared write path.
Original conceptual illustration for AIOpsSRE.
On this page3 sections

Imagine two workers that can close the same billing period. At 9:00, worker A acquires a short lease and starts preparing the final balance. Its process pauses. While it sleeps, the lease expires, worker B takes over, and the billing store records B’s result. A wakes up holding the balance it calculated earlier. From A’s point of view, it was halfway through a write. From the store’s point of view, the billing period is already closed.

The drill starts there, after failover appears to have succeeded. If the old worker can still change the record, the team has shown it can replace a leader. It has yet to show it can protect the work.

Give the store a way to tell the workers apart

A lease grants authority for a limited time. Its expiry makes room for another worker. A fencing token is an increasing identifier issued when a worker acquires authority through a suitable coordination service. Each write carries that identifier all the way to the system that can change the data.

Give A token 41 and B token 42. Once the destination registers 42, it must reject a write carrying 41. Put the token check and state change in the same transaction or guarded operation. If the worker checks the token in one call and writes in another, the gap reintroduces the problem. The worker may pause between those two calls.

There is a smaller race that matters in a real handoff. If the destination has only seen 41, it cannot infer from the number that 42 exists somewhere else. For a workflow that needs the destination to exclude A before B attempts a business update, make B register the new token at the destination first. Record that point explicitly in the test. Persist the high-water token with the protected resource so restarting the destination cannot revive an older writer.

Pause one worker and inspect the record

In a disposable test environment, establish a known billing record. Pause A after the lease check and before its write. Let B acquire the next token, register it at the destination, and complete a visible update. Resume A and verify that the store specifically rejected its stale token. A timeout alone cannot establish that. Read the destination through a path that can see B’s committed update. B’s value should still be there. Repeat after a destination restart to check that the old token stays fenced.

Run a control without a handoff. A legitimate write with the current token should still work. Retry the same logical operation with that token and confirm the record reflects one logical change. Fencing distinguishes owners. An idempotency key or equivalent state check handles duplicate attempts by the current owner.

A destination that cannot enforce the token changes the design. If an external payment API accepts an old request on its own, a token checked in your database cannot retract that request. Route the action through a component that enforces authority and use the downstream provider’s idempotency controls where available. Where the outcome is still uncertain, reconcile the external state before anyone retries.

The handoff can look healthy from every dashboard. The record you read after the old worker wakes up settles the question.

Three steps show worker A pausing with token 41, worker B registering token 42 and the store rejecting worker A’s late write.
The store must reject the stale token and preserve the current worker’s committed value.

Sources

Sources & context

Sources linked in this article. Read alongside the author’s analysis; a citation does not independently verify a publisher’s claims.

Report an error or outdated detail

Keep reading

Where to go next