Idempotency & retries

A send is not idempotent by nature. Repeating one without a key is how somebody receives the same message twice.

The idempotency key

Send Idempotency-Key with anything that is stable and unique per logical message: an order id, a row id, a UUID you keep. If Pigeon has already accepted a send with that key on that project, it returns the original message instead of sending a second one.

curl
curl -X POST https://api.pigeon.dev/v1/messages \
  -H "Authorization: Bearer pgn_live_your_key" \
  -H "Idempotency-Key: order-10482-shipped" \
  -H "Content-Type: application/json" \
  -d '{ "channel_id": "ch_01h9", "to": "+256700000000", "body": "Shipped." }'
Go
msg, err := client.Messages.Send(ctx, &pigeon.SendRequest{
    ChannelID:      "ch_01h9",
    To:             order.Phone,
    Body:           "Shipped.",
    IdempotencyKey: "order-" + order.ID + "-shipped",
})

Derive the key from the event, not from the attempt. uuid.New() inside your retry loop is a new key every time, which is the same as having none.

Which failures are safe to retry

SituationRetry?Why
PROVIDER_UNAVAILABLEYesPigeon never reached the provider, so nothing was sent.
RATE_LIMITEDYes, after Retry-AfterRefused before anything happened. The header says when.
5xx, or a timeoutYes, with a keyThe server may have acted. A key makes repeating it free.
No response at allYes, with a keyYou cannot know whether it was received. Same uncertainty as outcome_unknown.
OUTCOME_UNKNOWNOnly with the same keyThe message may already be on its way to a real phone.
Any other 4xxNoThe request is wrong. Sending it again unchanged will be wrong again.

OUTCOME_UNKNOWN

The case most worth handling and the one most often ignored. A provider accepted the request and then the connection died, or Pigeon could not reach the provider after handing the message over. The message may be on its way to a real phone, or it may not be.

Pigeon says so rather than guessing, because guessing wrong in either direction is worse than admitting it:

  • Report it as failed, and your code resends — the customer gets it twice.
  • Report it as sent, and it never arrives — nobody ever finds out.
Go
if pigeon.IsOutcomeUnknown(err) {
    // Resend with the SAME key. Pigeon returns the existing message if the
    // first attempt landed, and sends it if it did not.
    msg, err = client.Messages.Send(ctx, sameRequest)
}

Backoff

Exponential, with jitter. A hundred clients retrying in lockstep is how a recovering server is knocked over a second time. When the API sends Retry-After, use it: guessing is how a client that is already too fast stays too fast.