Idempotency and retries

One chat request can take several attempts. Keep those attempts from becoming extra saved replies.

Written deep dive25% previewBy Daniyal Saqib

Read the opening here. A free account gives you the full guide.

Follow one user question through several possible worker attempts. Keep the saved reply stable, recover abandoned work, and make an explicit decision when a timeout leaves the model provider’s outcome unknown.

01

The user presses Send once

One requested reply must not become extra saved replies when recovery repeats work.

The PDF from the queue guide is now searchable. The user asks, “What are the key decisions in this document?” and presses Send once. We create reply task A7: produce one response to that message. The browser may disconnect and a worker may crash, but those failures do not mean the user asked another question.

For this constructed example, the work runs in the background and the completed answer is saved before the chat displays it. Background execution lets accepted work outlive the connection; it is not a requirement for every chat product. We are protecting the stored reply, rather than making model output deterministic or proving its answer correct.

Name the promise precisely: at most one saved response per reply task. That leaves room for no response if every allowed attempt fails, and for repeated model computation during recovery. A user who intentionally asks for another answer creates new logical work. Those distinctions tell us which identities and safeguards the design needs.

02

Follow one successful reply

A task is the work owed to the user; an attempt is one effort to finish it.

The API saves the user message, creates A7 and records a recoverable queue handoff. As in the upload guide, accepted work cannot depend only on an API process remembering to publish. The dispatcher can publish again after a failure, but every delivery still identifies A7.

A worker receives the task and calls the LLM provider. The returned text is a candidate answer. It becomes the saved response only when our database accepts it. Saving the response and marking A7 complete happen together, so a reconnecting browser cannot observe “complete” without the corresponding reply.

The worker acknowledges queue progress after that durable save. The browser can fetch the saved response later; queue acknowledgment does not wait for the screen to display it. Keep user message, task, delivery, attempt, candidate answer and saved response distinct. We can now move a crash between these steps and identify the missing evidence without changing what the user requested.

03

Why the same work can return

Receiving a delivery and recording successful completion are separate events.

Suppose the provider returns text, then the worker crashes before saving it. A7 is still unfinished, so recovery tries again. The provider may do expensive work twice even though only one task exists. A queue can preserve unfinished work without preventing that repeated computation.

With SQS, receiving a message normally hides it for a visibility period. Successful processing is followed by deletion. If it is not deleted before the period expires, another worker can receive it. A worker can extend the period, but Standard queues can still deliver duplicates during visibility. Treat that timer as delivery coordination, not an exclusive correctness lock.

8 more sections to read

Sign in to keep reading

Idempotency and retries

Continue this section and read the complete guide and explore its visuals.

Free account. No payment required.

Read how we handle account information in our privacy notice.

8 more sections to read

There’s more in the full article

Continue reading with a free account.

Keep reading

Free account. No payment required.

Inside the full article

Continue the guide with 8 more sections.

  1. Protect the reply we saveRead after sign-in
  2. Recognize a retry and allow RegenerateRead after sign-in
  3. Reduce avoidable overlapping callsRead after sign-in
  4. A timeout leaves several possibilitiesRead after sign-in
  5. Reject a stale worker after takeoverRead after sign-in
  6. Decide whether another attempt helpsRead after sign-in
Show 2 more sections
  1. Match protection to the effectRead after sign-in
  2. Apply the framework to document ingestionRead after sign-in
Continue reading

8 more sections to read

Sign in to keep reading

Idempotency and retries

Continue this section and read the complete guide and explore its visuals.

Free account. No payment required.

Read how we handle account information in our privacy notice.