# Idempotency and retries

> How to retry a write without doing it twice, and which failures are safe to retry

Source: https://tickets.roostertail.com/docs/idempotency

A network can fail after a write has happened. Every `POST`, `PATCH` and `PUT` takes an `Idempotency-Key` header so that sending it again does not do it again. **Always send one**, and send the same one on a retry. The SDK does this for you.

## The rules

- A key is 1 to 255 printable characters with no spaces. A UUID works.
- The first answer is kept for 24 hours. The same key with the same body returns that answer, with the header `Idempotent-Replayed: true`.
- The same key with a **different** body, or on a different path, answers 409 `idempotency_key_reused`. Make a new key for a different request.
- The same key while the first request is still running answers 409 `idempotency_key_in_use`: wait a moment and send it again.
- A 5xx or a 429 is never kept, so a retry with the same key runs the request again.
- A key belongs to its caller within the org and mode: one API key, one member, one device, one holder link. Someone else's key never replays your answer.
- Two writes **require** a key and answer 400 `parameter_missing` without one: `POST /v1/door_sales` and `POST /v1/settlement/payouts`.
- A 4xx answer is kept under its key too. To fix a request that was refused, use a **new** key (the SDK does this when the body changes).

```bash
export API=https://rcms-tickets-test.web.app/v1
rt() { curl -sS "$@" -H "Authorization: Bearer $RCMS_TICKETS_KEY" -H "Content-Type: application/json"; }

KEY=$(uuidgen)
DAY=$(date -u -d '+45 days' +%F 2>/dev/null || date -u -v+45d +%F)
BODY=$(cat <<JSON
{
  "name": "Retry Fest",
  "currency": "usd",
  "venue": { "name": "Hall", "address": { "line1": "1 Main St", "line2": null, "city": "Detroit", "region": "MI", "postal_code": "48201", "country": "US" }, "timezone": "America/Detroit", "location": null },
  "days": [{ "date": "$DAY", "name": "Day", "doors_at": "${DAY}T20:00:00Z", "starts_at": "${DAY}T21:00:00Z", "ends_at": "${DAY}T23:30:00Z" }]
}
JSON
)

FIRST=$(rt "$API/events" -H "Idempotency-Key: $KEY" -d "$BODY" | jq -r .id)
SECOND=$(rt "$API/events" -H "Idempotency-Key: $KEY" -d "$BODY" | jq -r .id)
echo "same event both times: $([ "$FIRST" = "$SECOND" ] && echo yes || echo no)"

rt "$API/events" -H "Idempotency-Key: $KEY" -d '{"name":"A different body"}' | jq -r .error.code
```

## Which failures to retry

| You got | What happened | Do |
| --- | --- | --- |
| A connection error, or no whole answer | The write may or may not have happened. | Retry with the **same** key. |
| 429 `rate_limited` | Too many requests. [The limits](#rate-limits). | Wait `Retry-After` seconds, then retry with the same key. |
| 503 `service_unavailable`, `request_timeout`, 500 `internal_error` | Our side. Nothing was kept. | Retry with the same key, after `Retry-After`. |
| 503 `outcome_unknown` | We could not confirm whether it was saved. | Retry with the **same** key. Never a new one. |
| 409 `lock_timeout` | The record was busy. Nothing was written. | Retry at once. |
| 409 `idempotency_key_in_use` | The first request is still running. | Wait, then retry with the same key. |
| 400, 402, 404, 409 with another code | The request was refused. | Fix it. Retry with a **new** key. |

## Orders have two more guards

`POST /v1/orders` with an `external` payment is also idempotent on its payment `reference`: the same reference on the same hold answers the order it already made. A hold that already became an order answers that same order when the retry carries the same key or the same payment, so a timeout after the order was made never looks like `hold_consumed`.

## A malformed id

An id in the path that is not shaped like an id (`/v1/orders/not-an-id`) cannot name anything, so it answers 404 `route_not_found`, not `resource_missing`. `route_not_found` is answered before any route is chosen, so no operation lists it. An id that is shaped right and does not exist answers `resource_missing`.

```bash
export API=https://rcms-tickets-test.web.app/v1
curl -sS "$API/orders/not-an-id" -H "Authorization: Bearer $RCMS_TICKETS_KEY" | jq -r .error.code
curl -sS "$API/orders/ord_doesnotexist0000000" -H "Authorization: Bearer $RCMS_TICKETS_KEY" | jq -r .error.code
```

## Rate limits

Each key has its own. Over one, you get 429 `rate_limited` with `Retry-After`.

| Limit | Test key | Live key |
| --- | --- | --- |
| Any request | 25 a second, bursts to 50 | 100 a second, bursts to 200 |
| Writes (POST, PATCH, DELETE) | 120 a minute | 300 a minute |

The per-second limit is counted on each server instance, so it can be looser when several are running, and never count on the exact point it starts. The per-minute limit holds across all of them. An MCP tool call costs two: the call and the request it makes. A few routes have tighter limits of their own, such as the door's look-up and a hosted link.
