esc
  • WorkflowsPages
  • Case studiesPages
  • Machine Learning FoundationsLearn
  • AI EngineeringLearn
  • AI Guardrails & Red TeamingLearn
  • UI/UXLearn
  • Design SystemsLearn
  • Production System DesignLearn
  • AI papersPages
  • Interview KitPages
  • Book a callContact
Let’s talk

I’ll build the agent for it.

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.

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

  1. 1The payment request goes to the bank.
  2. 2The reply is lost, so the worker tries again.
  3. 3Without a key, the second try moves the money again.
  4. 4The check looks up the key saved with the first payment.
  5. 5The service replays the saved result and sends nothing new.
  6. 6One payment, one record, and the claim is closed.
Both paths start with the same payment request and the same lost reply. Without a key, the retry moves the money again. With a key, the service finds its saved record and replays the result, so the money moves once.

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

  1. 1The agent adds the payment job to the queue.
  2. 2A free worker takes the next job.
  3. 3The payment service confirms, so the job closes.
  4. 4No confirmation in time, so the job is offered again.
The agent adds a job, a free worker takes it, and the job comes back if it is not finished in time. Because the job can return, the worker checks the key before it pays.
  • 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. 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. 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. 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. 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. 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.

Python
# 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

1 / 6

What will they ask next?

  1. Q337Timeouts, retries and backoff?14 min
  2. Q356Design rate limiting.12 min
  3. Q364Security architecture for LLM apps?13 min
  4. Q366Design human approval for risky agent actions.13 min
  5. Q368Build a cost model for the system.12 min

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.