---
title: "Comment by Mike Dabydeen on \"I Kill My Workers on Purpose\" | Hashnode"
description: "When the idempotency keys go in, I'd build them from the job id and step, never from the attempt number. The attempt counter is the right fence for your own tab"
url: "https://hashnode.com/posts/crash-safe-workflow-engine-postgres/6afcf385577ca6d1f890dc51/comment/6afd96c74300b22e5f05a8fd"
---

[Discussion](https://hashnode.com/posts/crash-safe-workflow-engine-postgres/6afcf385577ca6d1f890dc51)

Comment [I Kill My Workers on Purpose](https://hashnode.com/posts/crash-safe-workflow-engine-postgres/6afcf385577ca6d1f890dc51)

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

[6h ago](https://hashnode.com/posts/crash-safe-workflow-engine-postgres/6afcf385577ca6d1f890dc51/comment/6afd96c74300b22e5f05a8fd)

When the idempotency keys go in, I'd build them from the job id and step, never from the attempt number. The attempt counter is the right fence for your own tables, but if it ends up in the key, a reclaimed job hands the other system a key it has never seen, and the duplicate ticket gets created anyway. The fence has to change on every claim; the key has to stay the same across them.

The heartbeats you're planning can carry the same token. If the heartbeat runs UPDATE jobs SET lease\_expires\_at = ... WHERE id = $1 AND attempts = $2 and gets zero rows back, the worker has already lost the job and should stop before its next external call, instead of learning that when its success write matches nothing. For a system that doesn't accept idempotency keys, writing the job id into the record it creates, in an external reference field or a label, gives the retry something to search for before it creates another one.

[Jason Collins](https://hashnode.com/@jdcollins)

[6h ago](https://hashnode.com/posts/crash-safe-workflow-engine-postgres/6afcf385577ca6d1f890dc51/comment/6afd96c74300b22e5f05a8fd#reply-6afda24d908e8268e51ce1ac)

This is really helpful. Thank you!

You're right. I was mentally carrying the attempt number around because it's already part of the claim lifecycle. Thinking about the idempotency key as the identity of the work itself, rather than a particular attempt, makes the distinction much clearer.

The plan already had fenced heartbeats, but not the part where losing the lease stops the handler before its next outside call. That’s a much better moment to find out than waiting for the final write to match nothing, so I’m adding that.

I also like the external reference idea for systems that don’t support idempotency keys. Search-before-create still leaves a small window if two attempts overlap, but it turns many duplicates from “create another record” into “find the one that’s already there.”

All three are going into the plan. You rock!
