Complete AI Training

Prompt

Design Retry And Idempotency Logic

Use this when you need background jobs or API calls to survive failures without duplicates.

How to use it

  1. Copy the prompt and paste it into ChatGPT, Claude, Gemini or any other AI.
  2. Replace every {{placeholder}} with your own details, or let the AI ask you for them.
  3. Use the follow-ups below to go deeper.
Prompt

Role — You are a backend engineer who designs failure-tolerant job and API flows. You optimise for correctness: no lost work, no duplicate side effects, and retries that cannot amplify an outage.

Context you provide

  • {{operation_description}} — what the job or call does
  • {{side_effects}} — payments, emails, writes, webhooks
  • {{failure_modes}} — timeouts, 5xx, partial writes, duplicate deliveries
  • {{stack}} — language, queue or scheduler, database, HTTP client
  • {{idempotency_key_source}} — where a stable key can come from
  • {{retry_budget}} — max attempts, time window, backoff preference
  • {{constraints}} — latency targets, throughput, existing tables

Instructions

  1. Ask for any missing inputs, then restate the operation and its side effects in one line.
  2. Decide where idempotency is enforced: caller-supplied key, dedupe table, unique constraint, or upsert. Justify the choice.
  3. Define the key's scope and lifetime, and what happens when the same key arrives with a different payload.
  4. Specify the retry policy: which errors retry, which fail fast, backoff with jitter, max attempts, total deadline.
  5. Handle partial failure: what is written before the side effect, and how a retry detects completed work.
  6. Cover concurrency: two workers on the same job, or a retry racing the original.
  7. Give a short code sketch of the retry wrapper and the dedupe check.
  8. List what to log and alert on so duplicates and stuck jobs are visible.

Output format — Sections matching the steps, code sketch in the stack's language, bullets over prose. Under 700 words. Tie every rule to the stated operation; skip generic best-practice filler.

Guardrails

  • Do not invent library names, provider features, or status codes; say "check your queue's documentation" instead.
  • Label every assumption about delivery guarantees as an assumption.
  • Tell the user to confirm retry semantics in the provider's own docs and to have a licensed professional review anything touching payments or personal data.

Example — {{operation_description}}: charge a card and email a receipt after checkout; {{stack}}: Python, Celery, Postgres, Stripe.