LESSON 05 · Retries and idempotency

Recover from ambiguous failure

Plan for repeated work and uncertain provider responses.

SYSTEM VIEW

The delivery path

Diagram view
Duplicates—
Waited—
Dead letters—

Watch SMS attempts through a crash, a run of timeouts and a replay, and see what keeps each to one text.

  1. The SMS worker takes attempt k-881 from the SMS queue and sends it with key k-881. The SMS gateway accepts, but the worker crashes before storing that result: Notification DB still says sending, and the queue still holds the attempt.

    Gateway
    accepted k-881
    Stored state
    k-881 sending

    What changed

    • k-881 sent with its key
    • Gateway accepts
    • SMS worker crashes before storing
  2. The SMS queue never deleted k-881, so it redelivers it after its visibility timeout. The restarted SMS worker resends it with the same key: the SMS gateway counts it once, and the worker stores the result. A gateway without idempotency keys can’t rule out a duplicate.

    Delivery
    at least once
    Same key
    counted once
    Per intent
    a unique constraint

    What changed

    • SMS worker restarted
    • Queue redelivers k-881
    • Same key: counted once
    • Result stored
  3. The SMS gateway doesn’t answer k-882 in time, a failure that may be transient. The Backoff timer waits 1 s before retry 1, then 2, 4, 8 and 16 s: five retries span about 31 s. A circuit breaker pauses calls to an unhealthy gateway.

    Waits
    1, 2, 4, 8, 16 s
    Five retries
    about 31 s

    What changed

    • Backoff timer
    • k-882 times out
    • Retry 1 after 1 s
  4. Suppose 10,000 attempts wait in the Backoff timer. With equal waits they would all come back at once. Jitter, a small random change to each wait, spreads the retries out, so the SMS gateway sees a steady stream.

    Waiting
    10,000 attempts
    Jitter
    a random change per wait

    What changed

    • 10,000 attempts waiting
    • Each wait ± jitter
    • Retries arrive spread out
  5. Attempt k-883 goes to a malformed address, and the SMS gateway rejects it. That failure is permanent, like an invalid token or denied consent, so the SMS worker records k-883 as failed in Notification DB and never retries it.

    Permanent
    bad address, token, consent
    Retries
    none

    What changed

    • k-883 rejected: malformed address
    • Stored as failed
    • Not retried
  6. Retry 5 of k-882 times out after about 31 s of waits. The SMS worker stores k-882 as exhausted, so a crash can’t erase it, and moves it with its context to Dead letters for inspection and controlled replay. Each stored transition costs one more write per attempt.

    Retries
    5 of 5
    Waited
    about 31 s

    What changed

    • Dead letters
    • Retry 5 of k-882 times out
    • k-882 moved with its context
  7. Any timeout of k-882 may have followed acceptance. Original key: the replay reaches the SMS gateway as k-882 and counts once. New key: as k-990 it can send a second text. Don’t claim exactly-once; state the guarantee and the residual duplicate risk.

    What changed

    • k-882 leaves Dead letters
    • Replayed: original or new key
    • Duplicates: none or possible
    Replay with

Space plays · ← → step · Home resets

THE IDEA

How it works

Workers commonly provide at-least-once processing: a message may be handled more than once, but it is not silently dropped. Amazon SQS standard queues behave this way, redelivering a message that was not deleted before its visibility timeout ends. If a worker crashes after a provider accepts a request but before it stores the result, the queue delivers the attempt again. Use a stable delivery-attempt key, a uniqueness constraint on logical intent, and a provider idempotency key, which makes a repeated request count once, where supported. These reduce duplicates, but a provider that lacks idempotency can leave an unavoidable ambiguity.

Classify failures before retrying. A timeout or provider 5xx may be transient; use exponential backoff with jitter, a retry limit, and a circuit breaker that pauses calls while the provider is broadly unhealthy. With waits of 1, 2, 4, 8, and 16 seconds, five retries span about 31 seconds, and jitter, a small random change to each wait, stops 10,000 attempts from returning at once. Invalid tokens, malformed addresses, and denied consent are permanent outcomes and should not be retried. After the bounded retry budget, move the attempt to a dead-letter queue, a holding area with context for inspection and controlled replay.

Persist attempt state and audit transitions so a crash cannot erase the work; that costs one more write per attempt. When replaying a dead-letter item, retain its original notification identity. Do not claim exactly-once delivery across independent services; state the actual guarantee and the residual duplicate risk.

DECISION POINT

A worker times out after sending an SMS; the provider may have accepted it. What is the safest next step?

Write a sentence or two to unlock the guidance. Committing first is what makes it stick.

Keep in mind

  • At-least-once work requires idempotency and explicit duplicate handling.
  • Retry transient failures; quarantine permanent or exhausted attempts with a replay path.
CHECK YOUR UNDERSTANDING

Put it into practice.

1 OF 3
RecallQUESTION 01

What delivery behavior should a queue worker normally assume?

Choose an answer. You’ll see the explanation once you get it right.

Next lesson

0 of 3 checkpoints answered correctly to complete this lesson.