The worker that kept writing after its lease expired
Failure story 18, in concurrency and ownership.
The challenge
Section titled “The challenge”Only the worker holding the lease may write the nightly report. Worker A’s lease expired and worker B took over. How did A’s write end up as the final report?
The setup
Section titled “The setup”| Assumption | Value |
|---|---|
| Lock service | Grants a lease: exclusive ownership of a job for 30 seconds, renewable |
| Workers | Each checks it holds the lease, then writes its result to object storage at reports/2026-11-21.json |
| Storage | Accepts any write from an authorized worker |
What happened
Section titled “What happened”| Time | Worker A | Worker B | Storage |
|---|---|---|---|
| 0 s | Acquires the lease and starts computing | ||
| 25 s | Checks: I hold the lease. Prepares to write | ||
| 26 s | Pauses: a 40-second garbage-collection pause (or a VM migration, or a slow disk) | ||
| 56 s | (paused) | The lease has expired. Acquires it and starts computing | |
| 60 s | (paused) | Writes the report, computed from fresh data | B’s report |
| 66 s | Resumes. Its check already passed, so it writes | A’s stale report |
Worker A wasn’t misbehaving. From its own point of view, no time passed between checking the lease and writing.
Why it went wrong
Section titled “Why it went wrong”A lease tells a worker that it was the owner when it checked. It can’t tell the worker that it still is at the moment its write lands, because any pause between the check and the write, which a process can’t detect, lets the lease expire in between. Shortening the lease or checking more often narrows the gap; it never closes it. The only place that can reliably reject a stale owner is the place the write lands.
Two designs
Section titled “Two designs”| Aspect | Design A: a lease alone | Design B: lease plus fencing token |
|---|---|---|
| What the lock service gives | A lease | A lease and a number that increases every time the lease is granted: A gets 33, B gets 34 |
| What a write carries | The data | The data and the token |
| What the destination checks | Nothing | Rejects any write whose token is lower than the highest it has accepted |
| A’s late write | Accepted, overwriting B’s | Rejected: token 33 is older than 34 |
storage.write(key, data, token): if token < highest_token_seen[key]: reject "stale owner: token 33, current 34" highest_token_seen[key] = token store(key, data)The check and the write must be atomic at the destination, which is why it lives there: a database can do it as a conditional update, UPDATE … WHERE fencing_token < :token.
The check
Section titled “The check”Run the scenario with a test hook that pauses worker A between its lease check and its write, longer than the lease.
- Expected: B’s write is accepted.
- Expected: the destination rejects A’s write, even though A still believes it owns the work.
- Expected: A receives the rejection, logs that it lost ownership, and stops, rather than retrying.
This check is in the checks catalog as test templates in Java and Python.
When the simpler design is enough
Section titled “When the simpler design is enough”- The write is idempotent and its content doesn’t depend on when it was computed, so a late duplicate is harmless.
- The lease is used only for efficiency, so two workers occasionally doing the same work wastes effort but can’t corrupt anything. Then a plain lease is fine, and saying so in the design is worthwhile.
For educators
Section titled “For educators”Discussion question. Why does a lease alone feel safe? What would you need to believe about pauses, clocks and networks for it to be safe?
Exercise. The destination is a third-party API that you can’t change, and it has no notion of tokens. What can you do?
Worked answer
You can’t fence a destination that won’t check, so look for something it will check. If it supports conditional writes, such as an If-Match header with a version or ETag, the worker reads the current version and writes only if it hasn’t changed: a stale worker’s write then fails because B’s write changed the version. If it doesn’t, put a component you own in front of it that checks tokens, and make it the only path to the API. If that’s impossible too, be honest about the guarantee: make the operation safe to repeat, detect conflicting writes afterwards, and document that two owners can briefly coexist. What doesn’t work is a shorter lease.
Further reading
Section titled “Further reading”- Martin Kleppmann, How to do distributed locking (opens in a new tab), on process pauses and fencing tokens.
- Unmesh Joshi’s Lease (opens in a new tab) pattern, in Patterns of Distributed Systems.
Related pages
Section titled “Related pages”- Two valid transactions left nobody on call
- Kubernetes, where leader election uses leases
- The cache that went stale right after invalidation, the same pause, in a cache
Written and edited by Abhinav Taduka, enterprise software architect.
New pages are added regularly. Follow Abhinav on LinkedIn to see them first.