Guides
Idempotency and retries
Make a send safe to repeat, and know which failures are safe to try again.
The problem#
You send an email and the connection drops before you see the answer. Did it go? If you send again you might email your customer twice; if you do not, you might not email them at all. An idempotency key removes the doubt: you can repeat the request as often as you like and it is carried out once.
Use an idempotency key#
Add an Idempotency-Key header to POST /v1/emails or POST /v1/emails/batch. A key is 1 to 255 printable ASCII characters with no spaces. Choose something that identifies the thing you are sending, such as order-42-confirmation, not something random per attempt.
- The same key with the same body returns the original response again, with the header
Idempotent-Replayed: true, and creates nothing. - The same key with a different body is refused with
422 idempotency_key_reused: it is almost certainly a bug on your side. - Keys are kept for 24 hours, per organisation and per endpoint.
- A request that fails does not use the key up: after a validation error you can fix the body and send it again with the same key.
- A bad key (wrong length or characters) is
422 validation_failed, withIdempotency-Keyas the field.
$ curl -i -X POST https://api.cirrusmail.example/v1/emails \
-H "Authorization: Bearer $CIRRUS_API_KEY" \
-H "Content-Type: application/json" \
-H "Idempotency-Key: order-42" \
-d '{"from": "Shop <orders@shop.example>", "to": "ann@example.org", "subject": "Hi", "text": "Hi"}'
HTTP/1.1 202 Accepted
Idempotent-Replayed: true
Content-Type: application/json
{"data": {"id": "b3c1f5a4-7d2e-4c8a-9a1b-0e6f2d4a8c11", "status": "queued", "stream": "transactional", "test_mode": false}}Which failures are safe to retry#
| What happened | Retry? |
|---|---|
A 429 answer | Always. The request was refused before it ran. Wait Retry-After seconds first. |
| A 5xx answer, a timeout or a lost connection on a read (a GET) | Yes. |
| The same on a send that carries an idempotency key | Yes, with the same key. This is the case the key exists for. |
| The same when registering a domain or a webhook | No, not blindly. Look up what exists first. |
| Any other 4xx answer (400, 401, 403, 404, 409, 422) | No. The request itself is wrong; repeating it gives the same answer. |
Wait between tries, and wait longer each time. A good rule is exponential backoff with jitter: wait a random time up to 0.5 seconds, then up to 1, then 2, and so on, to a cap of 8 seconds. If the answer has a Retry-After header, wait that long instead.
The libraries do this for you#
Every library follows exactly the rules above. Each send gets an idempotency key (yours, or a random one it makes up) and the same key is used on every retry, so an automatic retry can never send an email twice. By default a request is tried up to three times (the first try and two retries). A Retry-After of more than 60 seconds is not waited out; the 429 is raised immediately.
const cirrus = new Cirrus({
apiKey: process.env.CIRRUS_API_KEY!,
baseUrl: "http://localhost:8090", // the host only; the library adds /v1
timeoutMs: 10_000, // per attempt, default 30000
maxRetries: 4, // default 2; 0 turns retries off
});Pass your own key when a send is tied to something in your system, so a retry from your own code (a job queue that runs a task twice) is safe as well. See Client libraries for how each library takes it.