---
title: "Discussion on \"Your Distributed Lock Expired. Your Code Didn't.\" | Hashnode"
description: "Discussion on \"Your Distributed Lock Expired. Your Code Didn't.\". A distributed lock can expire while the code that acquired it is still running. Another instance finishes its work and saves the result."
url: "https://hashnode.com/posts/your-distributed-lock-expired-your-code-didn-t/6acde157ae82c4dc9a01de56"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [Your Distributed Lock Expired. Your Code Didn't.](https://fullstackcity.com/your-distributed-lock-expired-your-code-didn-t?utm_source=hashnode&utm_medium=feed)

A distributed lock can expire while the code that acquired it is still running. Another instance finishes its work and saves the result.

Oct 13 · 14 min read · fullstackcity.com  
Read the article

-   [#programming-blogs](https://hashnode.com/tag/programming-blogs)
-   [#software-engineering](https://hashnode.com/tag/software-engineering)
-   [#software-development](https://hashnode.com/tag/software-development)
-   [#distributed-system](https://hashnode.com/tag/distributed-system)
-   [#cloud](https://hashnode.com/tag/cloud)
-   [#cloud-computing](https://hashnode.com/tag/cloud-computing)
-   [#microsoft](https://hashnode.com/tag/microsoft)
-   [#csharp](https://hashnode.com/tag/csharp)
-   [#dotnet](https://hashnode.com/tag/dotnet)

## Written by

[Patrick Kearns](https://hashnode.com/@fullstackcity)

@fullstackcity · Dublin

  
[View profile](https://hashnode.com/@fullstackcity)

Senior .Net Engineer

Senior Software Engineer specialising in cloud architecture, distributed systems, and modern .NET development, with over two decades of experience designing and delivering enterprise platforms in financial, insurance, and high-scale commercial environments. My focus is on building systems that are reliable, scalable, and maintainable over the long term. I’ve led modernisation initiatives moving legacy platforms to cloud-native Azure architectures, designed high-throughput streaming solutions to eliminate performance bottlenecks, and implemented secure microservices environments using container-based deployment models and event-driven integration patterns. From an architecture perspective, I have strong practical experience applying approaches such as Vertical Slice Architecture, Domain-Driven Design, Clean Architecture, and Hexagonal Architecture. I’m particularly interested in modular system design that balances delivery speed with long-term sustainability, and I enjoy solving complex problems involving distributed workflows, performance optimisation, and system reliability. I enjoy mentoring engineers, contributing to architectural decisions, and helping teams simplify complex systems into clear, maintainable designs. I’m always open to connecting with other engineers, architects, and technology leaders working on modern cloud and distributed system challenges.

## Responses 11

Markdown supported · ⌘↵ to post

[Darius Tran](https://hashnode.com/@dariustran)

[Oct 15](https://hashnode.com/posts/your-distributed-lock-expired-your-code-didn-t/6acde157ae82c4dc9a01de56/comment/6ad07e155ba71273f3ad89e6)

The point that a longer expiry only delays the failure is the part people skip. The stale writer has no idea it lost the lease, so only the destination can refuse it.

The same thing shows up with agents that retry tool calls. A paused or timed-out run wakes up and writes after a newer run already did.

For side effects the database can't fence, like an email or a payment call, do you lean on idempotency keys with the provider, or an outbox so only the current owner ever sends?

[Patrick Kearns](https://hashnode.com/@fullstackcity)

[Oct 15](https://hashnode.com/posts/your-distributed-lock-expired-your-code-didn-t/6acde157ae82c4dc9a01de56/comment/6ad07e155ba71273f3ad89e6#reply-6ad07f16e8a7ddf6b9bdc6f8)

Id prob combine an outbox with provider side idempotency. The fenced database transaction records the accepted result and its outbound intent together, so a superseded worker cant enqueue new work. The dispatcher then retries that intent using a stable idempotency key for the logical operation. An outbox doesnt guarantee "only the current owner ever sends". An intent accepted before ownership changed can still be delivered afterwards, and a dispatcher can itself pause with a request in flight. If that previously accepted action must now be rejected as obsolete, the destination needs a version or conditional operation check too. Idempotency alone wont establish that. Where the provider offers no check, you have to decide whether accepted intents remain valid or whether the workflow needs cancellation, reconciliation or compensation.

[BomToBe](https://hashnode.com/@bomtobe-orke)

[Oct 14](https://hashnode.com/posts/your-distributed-lock-expired-your-code-didn-t/6acde157ae82c4dc9a01de56/comment/6acfaf6b0fc54e188ed42cf9)

Good distinction between fencing for safety and lease expiry for liveness. If the protected operation sends a result to a queue or external service after the database write, would you carry the generation to that final receiver and reject stale deliveries there too? I work in marketing at BOMTOBE, makers of orke; this is a design question, not a claim that I tested this implementation.

[Patrick Kearns](https://hashnode.com/@fullstackcity)

[Oct 15](https://hashnode.com/posts/your-distributed-lock-expired-your-code-didn-t/6acde157ae82c4dc9a01de56/comment/6acfaf6b0fc54e188ed42cf9#reply-6ad07f7eb7972db3e87ff0ba)

Yeah, f the downstream operation needs protection against stale owners, Id carry the report ID and generation through and have the receiver enforce the check atomically with its own state change. The database fence only protects the database write, it wont automatically protect a later queue message or API call.

[mno tao](https://hashnode.com/@dancermno)

[Oct 14](https://hashnode.com/posts/your-distributed-lock-expired-your-code-didn-t/6acde157ae82c4dc9a01de56/comment/6aced9ba79a9aae105350829)

One thing worth adding for anyone adopting this: count rejected publications by cause. Zero rows because Generation moved means another worker took over, which is the design working. Zero rows with Generation unchanged means only the lease time ran out, so builds are outliving the lease and its length or renewal needs attention. The UPDATE alone cannot tell the two apart, so a follow-up read of the row is needed to log which one happened. Did you consider returning the reason from the adapter instead of a bool?

[BomToBe](https://hashnode.com/@bomtobe-orke)

[Oct 14](https://hashnode.com/posts/your-distributed-lock-expired-your-code-didn-t/6acde157ae82c4dc9a01de56/comment/6aced9ba79a9aae105350829#reply-6acff6f11be7c4b932d8d0bd)

Agreed—the reason should be visible, not collapsed into one ‘rejected’ count. For a downstream queue, the generation token only prevents a stale publish if the receiver checks it; otherwise an idempotency key or reconciliation step is still needed. Does the sample report track these rejection causes separately?

[Patrick Kearns](https://hashnode.com/@fullstackcity)

[Oct 15](https://hashnode.com/posts/your-distributed-lock-expired-your-code-didn-t/6acde157ae82c4dc9a01de56/comment/6aced9ba79a9aae105350829#reply-6ad07c9351210ccf0ec3459a)

No, the sample currently logs a generic publication refusal with the report ID and generation, it doesnt track causes separately. A structured result would be a useful extension, provided it preserves the distinction between a reason and a later diagnostic observation. The thing to remember with a downstream receiver is an idempotency key prevents duplicate execution of the same operation, but doesnt necessarily reject a distinct operation from a stale owner. The receiver needs to enforce the generation check atomically with the protected change. Otherwise it wont provide that same prevention guarantee.

[mno tao](https://hashnode.com/@dancermno)

[Oct 16](https://hashnode.com/posts/your-distributed-lock-expired-your-code-didn-t/6acde157ae82c4dc9a01de56/comment/6aced9ba79a9aae105350829#reply-6ad1815833dcd04d662b19ae)

Fair point about another acquisition landing between the UPDATE and the read. Logging it as diagnostic evidence with an unknown outcome allowed seems like the honest version, and it keeps the adapter contract to what it can actually guarantee. Thanks for the detailed answer.

[Mike Dabydeen](https://hashnode.com/@firelinks)

[Oct 13](https://hashnode.com/posts/your-distributed-lock-expired-your-code-didn-t/6acde157ae82c4dc9a01de56/comment/6ace13d7a868b35039393eb6)

I think the publish statement can drop `AND LeaseUntilUtc > SYSUTCDATETIME()` without losing safety, because the generation already fences the write. If nobody acquired after the lease ran out, Generation still matches and LeaseOwner is still the same worker, so the late write can't overwrite anyone. If someone did acquire, Generation has moved and the update matches zero rows either way. As written, a build that takes 31 seconds with no competing worker throws its result away, which is exactly the case the missing renewal loop would have to cover.

Kleppmann's 2016 locking post draws the same line: safety shouldn't rest on timing, and timeouts belong to liveness. Here the lease decides when another worker may take over, and the generation decides whose write counts. If you want a cap on how old a published report can be, I'd keep that as a separate freshness rule rather than part of the fence.

[Patrick Kearns](https://hashnode.com/@fullstackcity)

[Oct 14](https://hashnode.com/posts/your-distributed-lock-expired-your-code-didn-t/6acde157ae82c4dc9a01de56/comment/6ace13d7a868b35039393eb6#reply-6acf26b61175aa9e8a360b36)

The post deliberately applies the stricter rule that publication requires an unexpired lease, but your 31-second example highlights the cost of that choice, especially without renewal. For this report example, allowing publication until the acquisition is superseded is a better fit. Id remove that predicate and update the explanation and expiry test accordingly, keeping any source freshness requirement separate.
