Idempotency, Queues and Retries
What idempotency, queues and retries mean for AI systems, why duplicates happen, and how to make a retry safe, explained simply for your AI interview.
In short
A retry sends a failed request again, and a queue holds jobs until a worker takes them. Idempotency makes that safe: each action carries a unique key, so a repeat does not pay, send or book twice.
12 min readIntermediateOccasional · estimateFacts checked 27 November 2026
Say it out loud
Read it once, then say it without looking. Start with the short one; use the long one when they ask you to go deeper.
A retry means sending a failed request again. The danger is a duplicate. The bank may have moved the money, but my worker never got the reply, so it tries again. The fix is an idempotency key, a unique ID on each payment. The service checks it first, and a repeat gets the saved result instead of a second transfer. Queues matter too, because a queue can deliver the same job twice. So I make the action safe to repeat, and then the retry is safe.
A queue is a line of jobs waiting for a worker. Amazon SQS standard queues, for example, can deliver one message more than once. Retries add more repeats: a call times out, so the caller sends it again. This happens in many distributed systems, because a reply can get lost after the work is already done.
Here is the AI version. A claims agent asks the payment service to pay claim 48213 for 1,840 dollars. The bank moves the money, but the reply is lost, and the worker retries. Without protection, the claim is paid twice. With an idempotency key, the service saves the key and the result together with the payment record. The retry finds the key and gets the saved result, so the money moves once.
Retries need limits too. The Anthropic SDK retries a failed model call by default, so a job that the queue also retries can make many calls. I keep one retry layer, add backoff with jitter so clients do not retry together, and stop after a set number of tries. Errors that will not clear, such as bad input, should not be retried at all.
A model call is different from a payment. A second call can return different words, so I store the first answer under the job key and reuse it, instead of asking again.
What are idempotency, queues and retries, in plain words?
A queue is a line of jobs waiting for a worker. A retry is trying a failed step again. Idempotency is the promise that doing the same request twice has the same effect as doing it once.
Put together, they work like this. Work goes into a queue. A worker takes a job and calls another service, such as a payment API. If the call fails or times out, the job is tried again. The danger is that the first call may have worked after all, so the repeat does the work a second time.
Think of a bank teller who gets the same transfer slip twice, because the customer pressed the button twice. A good teller checks the reference number first, so the money moves once. An AI system needs the same check, because a claims agent can be asked to pay the same claim twice.
Key words
- Queue
- A line where jobs wait until a worker takes them.
- Retry
- Trying a failed step again, usually after a short wait.
- Idempotency
- Doing the same request twice has the same effect as once.
- Idempotency key
- A unique ID sent with each request, so a repeat can be spotted.
- At-least-once delivery
- The queue promises a job arrives one or more times, so repeats happen.
Why do queues and retries create duplicates?
A retry is only safe if repeating the action is harmless, because a timeout does not tell you whether the first try did the work. The service may have done the work and then lost its reply. Official sources put it plainly:
“An idempotent operation is one where a request can be retransmitted or retried with no additional side effects, a property that is very beneficial in distributed systems.”
AWS
“Standard queues ensure at-least-once message delivery, but due to the highly distributed architecture, more than one copy of a message might be delivered, and messages may occasionally arrive out of order.”
Amazon SQS
“Queues provides at least once delivery by default in order to optimize for reliability.”
Cloudflare Queues
A made-up example, with the numbers kept simple. An insurer approves claim 48213 for $1,840. The payment service calls the bank, the bank moves the money, and the reply times out after 30 seconds. The worker retries. Without a key the insurer has paid $3,680, and someone must chase $1,840 back from the bank.
The same lost reply, with and without a key
Payment request
claim 48213, $1,840
Without a key
Bank pays, reply lost
the money has already moved
Retry pays again
a second $1,840 leaves
With an idempotency key
Key checked first
the first payment left a record
Replay saved result
no second transfer
Paid twice
$3,680 left the bank
Paid once
$1,840 left, one record
- 1The payment request goes to the bank.
- 2The reply is lost, so the worker tries again.
- 3Without a key, the second try moves the money again.
- 4The check looks up the key saved with the first payment.
- 5The service replays the saved result and sends nothing new.
- 6One payment, one record, and the claim is closed.
Saving the key as done before the bank call is risky. A crash then leaves a job marked done that was never paid. Our own key and our payment record belong in one transaction, so a crash cannot split them.
The bank's transfer happens outside our database, so no transaction can cover it. That is why the same key must also go to the bank. Cloudflare's guidance makes the same point: a payment API that receives the key can reject the duplicate. Then a crash after the bank's transfer still gives a safe retry, as long as the bank honours the key.
Tip
Say it like this in the interview: the key is checked by the service that owns the payment, and it is saved with the payment record.
How do a queue and a worker fit together?
A queue smooths out peaks. If a burst of claims arrives in one hour, the queue holds them, and workers take them at their own pace. The agent does not wait for the payment to finish, and a slow bank does not block the agent.
The queue also has to be able to hand a job back. Azure describes the queue as a buffer that stores the message until the service retrieves it. In Amazon SQS, if a worker does not delete a message before its visibility timeout expires, the message becomes visible again and another worker can take it. So the worker must be safe to repeat.
A queue, a worker and a job that comes back
Claims agent
adds one payment job
Queue
holds jobs until a worker is free
Payment worker
checks the key, then pays
Paid once
the key stops a second payment
- 1The agent adds the payment job to the queue.
- 2A free worker takes the next job.
- 3The payment service confirms, so the job closes.
- 4No confirmation in time, so the job is offered again.
- Without a key
The job comes back, money goes out again
The worker has no record of the first payment, so it pays a second time.
- With a key
The job comes back, nothing new goes out
The worker finds the saved result and marks the job done.
How do you make a retry safe?
Five habits, in order. Each one is a step you can explain in an interview.
- 1
Give every action a key
Build a unique key from the business event, such as claim-48213-pay-1, and send it with every attempt.
- 2
Save the key with the result
Store the key and the outcome in one transaction with our own records. Also pass the same key to the bank, because its transfer sits outside our transaction.
- 3
Refuse a changed repeat
If a repeat arrives with a different amount, refuse it. The AWS article on safe retries says its service returns a validation error for a parameter mismatch.
- 4
Back off, with jitter
Wait longer after each failure and add a random delay, so clients do not all retry at the same moment.
- 5
Retry only what can succeed
Cancel errors that will not clear by waiting, such as bad input, and report them. Cap the number of tries.
AWS calls this an important consideration.
“An important consideration is that the process that combines recording the idempotent token and all mutating operations related to servicing the request must meet the properties for an atomic, consistent, isolated, and durable (ACID) operation.”
AWS
Layers multiply. At the time of checking in November 2026, the Anthropic SDK retries a failed request up to two times by default, so each call gets three attempts. Say the queue also gives a job three tries. One unlucky claim summary can then make nine model calls during an overload. Choose one retry layer and turn the other off.
Tip
Setting max_retries to 0 turns the SDK's retries off, so the queue becomes the one place that retries.
Here is the save-and-replay idea in plain Python. The service owns the payment, so the check lives there. The bank is a stand-in that refuses to move money twice for the same key.
# A simplified payment service. It owns the payment, so it owns the key check.
RESULTS = {} # key -> result, saved in the same database transaction as the payment
BANK = {} # the bank's own record: a repeated key does not move money again
def bank_transfer(key, amount):
if key not in BANK: # never reached in this run: RESULTS catches the repeat first
BANK[key] = amount
return "ok"
def pay_claim(key, amount, reply_lost=False):
if key in RESULTS:
return "REPLAY: " + RESULTS[key]
bank_transfer(key, amount)
RESULTS[key] = "PAID " + str(amount)
if reply_lost:
return "TIMEOUT: no reply"
return RESULTS[key]
key = "claim-48213-pay-1"
print(pay_claim(key, 1840, reply_lost=True)) # bank paid, reply lost
print(pay_claim(key, 1840)) # worker retries with the same key
print("Bank moved:", sum(BANK.values()), "from", len(BANK), "payment")Output
TIMEOUT: no reply REPLAY: PAID 1840 Bank moved: 1840 from 1 payment
How would you handle a repeat with a changed amount?
Say a bug changes the retry to 1,480 dollars but keeps the same key. Paying it would mix two different payments under one key. A safe service refuses the repeat and raises an error for a person to check.
AWS handles this case by returning a validation error, so the repeat is refused.
- The bug
Same key, new amount
The retry looks like the first request, but the money is different.
- The fix
Refuse and flag it
The service returns an error, so nothing moves and a person checks the claim.
What does the interviewer want to hear?
A strong answer covers
- A plain definition: a repeat must not do the work twice.
- Queues can deliver a job more than once, so the worker must be safe to repeat.
- A key is checked by the service that owns the effect, and saved with it.
- Backoff with jitter, a retry limit, and a clear rule for errors not to retry.
- One retry layer, chosen on purpose, so attempts do not multiply.
- Model output can differ on a second call, so store the first result.
Common traps
Trap"Just retry it until it works."
BetterA retry without a key can pay twice. Make the action safe to repeat first.
Trap"The queue runs each job exactly once."
BetterStandard SQS queues are at least once, so a copy can arrive. The worker must handle repeats.
Trap"Turn on every retry setting we have."
BetterRetries in two layers multiply. Keep one, and say which one and why.
Can you spot the duplicate risk?
Spot the risk
A claims worker times out on the bank call and tries again. Which call is safe to repeat?
Quick check
An agent's model call fails with a 529 overload error, and one job makes nine calls. Which change fixes the cause?
Can you answer these from memory?
Flash cards · tap a card to flip it
What will they ask next?
Last updated on 27 November 2026
Building an agent that pays, books or sends things?
I design and build AI agents with safe retries, queues and approval steps in place. Book a short call and tell me what yours needs to do.