Retries and idempotency
Repeat a request safely after a timeout or network error.
If you don't know whether a request went through, send it again with the same Idempotency-Key. Keystorm runs it only once and returns the original result to every repeat.
| Rule | Detail |
|---|---|
| Format | 16 to 128 visible ASCII characters. A UUID works well. |
| Lifetime | 24 hours, per operation and link. |
| Same key, same request | Returns the original response with Idempotency-Replayed: true. No extra link and no extra quota. |
| Same key, different request | 409 IDEMPOTENCY_CONFLICT. Use a new key for a new request. |
| No key | Each call is a new request. Repeating a creation can create a second link. |
A replayed response shows the link as it was then. Use GET for its current state. Use a new key for each new operation, and the same key with the same body and If-Match for each retry of it.
import { randomUUID } from 'node:crypto';// Retries a write safely: same Idempotency-Key, waits on 429, backs off on 503.async function send(url, options, attempts = 4) { const key = randomUUID(); // one key for every attempt of this request for (let attempt = 1; ; attempt++) { let response; try { response = await fetch(url, { ...options, headers: { ...options.headers, 'Idempotency-Key': key } }); } catch (error) { if (attempt >= attempts) throw error; // network error: retry with the same key } if (response && response.status !== 429 && response.status !== 503) return response; if (attempt >= attempts) return response; const retryAfter = Number(response?.headers.get('retry-after')); const wait = retryAfter > 0 ? retryAfter * 1000 : 2 ** attempt * 500 + Math.random() * 500; await new Promise((resolve) => setTimeout(resolve, wait)); }}| Situation | Retry? |
|---|---|
| Network error or timeout | Yes, with the same key. |
429 | Yes, after the seconds in Retry-After. |
503 | Yes, with exponential backoff and jitter. |
400, 401, 403, 404, 409, 412, 422 | No. Fix the request first. For 412, read the link again. |