# Retries and rate limits

> Decisions never wait for a retry: they fall back to your default. Outcomes and erasures, which must not be lost, are retried, waiting as long as Zigbongo asks within a budget.

## Which calls retry

| Call | Timeout per attempt | On failure |
|---|---|---|
| `decide()`, a registered flow's `start()` and `next()` | 150 ms (`timeoutMs`) | Not retried: returns your default ([Fallbacks and timeouts](https://zigbongo.com/docs/fallbacks-and-timeouts.md)) |
| `outcome()`, `outcomes()` | 5,000 ms (`requestTimeoutMs`) | Retried, up to 3 attempts in all (`outcomeAttempts`), then throws |
| `eraseSubject()` | 65,000 ms (`erasureTimeoutMs`) | Retried like outcomes, then throws ([Erasing customers](https://zigbongo.com/docs/erasing-customers.md)) |
| `register()`, `pause()`, `resume()` | 5,000 ms (`requestTimeoutMs`) | Not retried: throws |
| A registered run's `abandon()` | 5,000 ms (`requestTimeoutMs`) | Not retried; never throws |
| `warm()` | 5,000 ms (`requestTimeoutMs`) | Not retried; never throws ([The connection](https://zigbongo.com/docs/fallbacks-and-timeouts.md#the-connection-and-warm)) |
| Fallback reports | 5,000 ms in Node.js; in PHP all of them within 1,000 ms (`fallbackReportTimeoutMs`) | Not retried; the error goes to `onError` |

Retrying is safe because these calls are idempotent: an outcome by its `outcomeId`, an erasure because erasing again finds nothing. An outcome an earlier attempt stored usually reads `duplicate`, and may read `accepted` when the retry reaches another of Zigbongo's servers; either way it counts once.

## A dropped connection

A server or proxy between you and Zigbongo can close a connection while requests are still on it. On HTTP/2, which the Node.js SDK uses, that's a `GOAWAY`. When a call's connection closes before any answer, the SDK sends it once more at once, on a new connection, within the same timeout, so a decision still comes back within 150 ms.

That holds for every call, `decide()` included, and doesn't count as one of `outcomeAttempts`. Sending twice is safe: a decision by its idempotency key, an outcome by its `outcomeId`. The Node.js SDK does this itself; in PHP, curl does it for the SDK.

## What's retried, and how long it waits

A failed attempt is retried when it could succeed unchanged: a timeout, a network error, an unexpected answer, `429` or a `5xx`. Any other error (`400 invalid_input`, `401`, `404`, `409`, `413`, and in PHP a request that can't be encoded) is thrown at once, since sending it again won't help.

Before each retry the SDK waits its backoff, 200 ms and then 400 ms (doubling), or as long as Zigbongo asked in its `Retry-After` header, whichever is longer.

All the waits of one call together are capped by `maxRetryWaitMs`, 10,000 ms by default. When the next wait would go past it, the SDK doesn't sleep on it: it throws at once, and the error's `retryAfterMs` says how long Zigbongo asked you to wait.

```js
import { Zigbongo, ZigbongoError } from "@zigbongo/sdk";

try {
  await zb.outcome({ outcomeId: order.id, flowKey: "checkout", subjectId: user.id, value: order.total, at: order.paidAt });
} catch (e) {
  if (e instanceof ZigbongoError && e.retriable) {
    job.retryIn(e.retryAfterMs ?? 60_000); // your queue's retry
  } else {
    throw e;
  }
}
```

```php
use Zigbongo\ZigbongoException;

try {
    $zb->outcome($order->id, 'checkout', $order->total, subjectId: $user->id, at: $order->paidAt);
} catch (ZigbongoException $e) {
    if (!$e->isRetriable()) {
        throw $e;
    }
    $job->release(intdiv($e->retryAfterMs ?? 60_000, 1000)); // your queue's retry, in seconds
}
```

While it waits, the call holds up its process. Under PHP-FPM that's a worker, so report outcomes from a queued job, or lower `maxRetryWaitMs`.

## Rate limits

Each environment has three allowances, so one kind of traffic never starves another:

| Allowance | Calls | Default limit |
|---|---|---|
| `decide` | `decide`, a registered flow's starts, steps and abandon events | 1,000 a second |
| `outcomes` | Outcomes (each outcome in a batch counts) and fallback reports | 1,000 a second |
| `other` | Everything else: flow settings, pause and resume, registering, reports, input switches, erasure; and making a new flow | 20 a second |

- **Per environment.** A project's live and test environments each have their own.
- **A second's worth at a time.** Each allowance holds one second's worth of calls and refills at its limit. Each of Zigbongo's servers counts your calls on its own, so when they reach several servers, more than your limit can get through. Don't count on it: plan for your limit.
- **Batches.** A batch of outcomes is let in on one call's worth, then takes the rest of its size from the allowance, even below zero. A batch bigger than your limit still gets through, and your next outcome calls wait until the allowance has refilled.
- **New flows and content.** Two things count once more against `other`: a decide for a flow key Zigbongo hasn't seen, which makes the flow, and registering a definition Zigbongo doesn't have yet, whether with `register()` or at a registered flow's start. So a first decide for a new flow key, or a start with a new definition, can get a `429`, and falls back. Keep flow keys fixed: a key made per person or per order makes a flow each time, and is refused once past `other`.
- **The dashboard doesn't count.** Its own calls don't use your allowances.

If you need more, ask us: Zigbongo can give an environment limits of its own. The [terms](https://zigbongo.com/terms/) ask you not to load-test Zigbongo or send traffic beyond your limits on purpose without agreeing it with us first.

## The 429 answer

A call over an allowance is refused, and nothing about it is stored:

```http
HTTP/1.1 429 Too Many Requests
Retry-After: 1
Content-Type: application/json

{"error": {"code": "rate_limited", "message": "over this environment's `outcomes` limit of 1000 per second; retry after 1 s"}}
```

`Retry-After` is in whole seconds, at least 1. After a big batch it can be several seconds.

- **Outcomes and erasures** wait for it and retry, within `maxRetryWaitMs`. Outcomes sent one after another faster than your limit all arrive, paced by Zigbongo. A burst well over your limit can't all fit in three attempts: the rest throw, with `retryAfterMs`.
- **Backfills** should go in batches at your limit's pace: a batch no bigger than your per-second limit, about once a second.

## What decide does when limited

`decide()` doesn't wait. Over the `decide` allowance, or over `other` for a flow key Zigbongo hasn't seen, it gets a `429`, returns your default with `fallback: true`, and passes the error to `onError` with `status` 429 and code `rate_limited`. The fallback report goes on the `outcomes` allowance, so it still gets through, and the call counts in that decision point's fallback rate. That person sees your default and teaches Zigbongo nothing, as with any fallback.

## When Zigbongo is busy

When one of Zigbongo's servers has just started, or Zigbongo is busy, or its database is out of resources or restarting, it answers `503` with code `starting`, `timeout`, `overloaded` or `unavailable` and `Retry-After: 1`. Decisions, outcomes and fallback reports get `starting`, `overloaded` or `unavailable`, never `timeout`. `overloaded` means that server is too far behind in storing what it's sent, and turns those calls away until it catches up rather than accept what it might lose. Outcomes and erasures retry after it; decisions fall back.

A request body over 2 MiB is refused with `413 body_too_large` and isn't retried: split the batch.

If you call the HTTP API yourself, do the same: honour `Retry-After`, retry outcomes with the same `outcomeId`, and show your default rather than retry a decision.

---

The Markdown of https://zigbongo.com/docs/retries-and-rate-limits/. Every docs page: https://zigbongo.com/llms.txt
