Queues retry. That is their job. BullMQ hands a stalled job to another worker. An agent framework replays a tool call after a timeout. A process dies in the gap between sending the email and writing down that it sent it. Every one of those is a chance to charge a card twice, or to never charge it at all and never find out.
Most retry guards I see in Node code are one of two patterns. I wanted to know how they behave when a worker is actually killed, not when it politely throws an error. So I built a crash lab, and a small library called claim-once that is the last row in every scenario below.
The setup
The lab starts real Node processes against a real Redis and kills them with SIGKILL at chosen points. The side effect appends a line to a file, so a kill cannot undo an effect that already happened. Counting lines gives the true number of effects.
The run below used 20 operations per scenario, 8 concurrent runners, a 400 ms lease, Redis 8.10 on Linux and Node 24. In the crash scenarios each operation's first worker is killed at the crash point, the lease runs out, and a second worker gets every job again, the way a queue redelivers. 160 worker processes were SIGKILLed in that run.
Four strategies:
- no guard: just run it
- check, run, then set done: GET done, run, SET done
- SET NX first, then run: mark the key before the effect
- claim-once: reserve, begin, run, complete
Results
| Scenario | Strategy | Ops | Effects run | Duplicates | Lost | in_doubt |
|---|---|---|---|---|---|---|
| SIGKILL after the effect, before it is recorded | no guard | 20 | 40 | 20 | 0 | 0 |
| SIGKILL after the effect, before it is recorded | check, run, then set done | 20 | 40 | 20 | 0 | 0 |
| SIGKILL after the effect, before it is recorded | SET NX first, then run | 20 | 20 | 0 | 0 | 0 |
| SIGKILL after the effect, before it is recorded | claim-once | 20 | 20 | 0 | 0 | 20 |
| SIGKILL after the claim, before the effect | no guard | 20 | 20 | 0 | 0 | 0 |
| SIGKILL after the claim, before the effect | check, run, then set done | 20 | 20 | 0 | 0 | 0 |
| SIGKILL after the claim, before the effect | SET NX first, then run | 20 | 0 | 0 | 20 | 0 |
| SIGKILL after the claim, before the effect | claim-once | 20 | 20 | 0 | 0 | 0 |
| event loop blocked past the lease, job redelivered | no guard | 20 | 40 | 20 | 0 | 0 |
| event loop blocked past the lease, job redelivered | check, run, then set done | 20 | 40 | 20 | 0 | 0 |
| event loop blocked past the lease, job redelivered | SET NX first, then run | 20 | 20 | 0 | 0 | 0 |
| event loop blocked past the lease, job redelivered | claim-once | 20 | 20 | 0 | 0 | 0 |
| concurrent runners on the same keys | no guard | 20 | 160 | 140 | 0 | 0 |
| concurrent runners on the same keys | check, run, then set done | 20 | 160 | 140 | 0 | 0 |
| concurrent runners on the same keys | SET NX first, then run | 20 | 20 | 0 | 0 | 0 |
| concurrent runners on the same keys | claim-once | 20 | 20 | 0 | 0 | 0 |
What the table says
Check-then-set is the pattern that looks safe in code review. It ran 40 effects for 20 operations whenever a worker died after the effect or a stalled job came back, and 160 effects for 20 operations when runners raced on the same keys.
SET NX first never produced a duplicate. It never retries anything, which is also its failure: kill the worker after it marks the key and before the effect, and all 20 operations are gone with no error anywhere.
claim-once ran every effect once in every scenario. When a worker died after the effect had started, it did not guess. It left all 20 keys in_doubt.
That last column is the point. After a crash in that window, nothing in Redis can tell you whether the charge went through. A guard that marks the key done is guessing it happened. A guard that retries is guessing it did not. claim-once says in_doubt and makes you check the real system.
How it works
Each key is one Redis hash with a state: pending, in_doubt, done, failed or free. Every state change is one Lua script, so Redis runs it atomically. Leases use the Redis server clock, runOnce sends a heartbeat every leaseMs / 3, and each key carries a fencing token that strictly increases.
import { Redis } from 'ioredis';
import { createClaimOnce } from 'claim-once';
const once = createClaimOnce({ redis: new Redis(process.env.REDIS_URL), namespace: 'billing' });
const receipt = await once.runOnce(`refund:${orderId}`, async ({ idempotencyKey }) => {
return stripe.refunds.create({ payment_intent: pi }, { idempotencyKey });
});
Call it again with the same key, from any process, and you get the same receipt back without a second refund.
- Worker dies after claiming but before the effect began: the lease runs out and the next caller takes over with a higher token. Nothing is lost.
- Worker dies after the effect began but before reporting back: the key becomes in_doubt and every later call throws InDoubtError until you reconcile it.
- A worker that was only stalled can still finish with its token and settle the key as done.
Errors are yours to classify. A declined card should release the key. A timeout after the request was sent should not:
await once.runOnce(key, sendInvoice, {
onError: (err) => (isTimeout(err) ? 'in_doubt' : isBadRequest(err) ? 'fail' : 'release'),
});
A reconcile hook settles in_doubt keys against the real system, and returning undefined leaves the key for a person. For BullMQ there is onceProcessor, and for agent tool calls there is onceTool, which reads the toolCallId so a replayed call returns the first result.
What it does not promise
This is at most once effect execution with an explicit in_doubt state. It is not exactly once, and nothing that talks to an outside system over a network can promise that alone.
- Redis is the source of truth. If Redis loses writes, claims go with it. Use AOF or replicas you trust.
- The effect can still run twice if your reconcile hook says not_happened when it did happen.
- Downstream idempotency is still the best protection. ctx.idempotencyKey stays the same for a key across every attempt, so pass it to Stripe, your mail provider or your own API.
Run the lab yourself
npm install claim-once ioredis
npx claim-once-lab --redis redis://127.0.0.1:6379
If your numbers come out different from mine, open an issue with the output. That is the most useful bug report this library can get.
I'm Krrish Kumar and I maintain claim-once. It is MIT licensed: https://github.com/Krrishkumar125/claim-once and https://www.npmjs.com/package/claim-once
Top comments (0)