Client retries
When to retry
| Code | Retry? | Strategy |
|---|---|---|
2xx | No (success) | — |
400 | No | Fix the body |
401 | No | Check the API key |
403 | No | Request the scope |
404 | No | Check the document key |
409 | No | Conflicting Idempotency-Key |
413 | No | Reduce the body |
429 | Yes | Respect Retry-After |
500 | Yes | Exponential backoff |
502 | Yes | Exponential backoff |
503 | Yes | Aggressive backoff (system down) |
504 | Yes | Exponential backoff |
| Client timeout | Yes | With Idempotency-Key |
Exponential backoff algorithm with jitter
async function backoff(intento) {
const base = Math.min(1000 * Math.pow(2, intento), 30000); // max 30s
const jitter = Math.random() * 1000;
await sleep(base + jitter);
}
for (let i = 0; i < 5; i++) {
try {
return await llamarAPI();
} catch (err) {
if (esTransitorio(err) && i < 4) {
await backoff(i);
continue;
}
throw err;
}
}