# Performance (/docs/self-host/performance)



## Benchmark [#benchmark]

`bun run bench` (in `apps/api`) drives the execute endpoint with concurrent callers and prints
throughput and p50/p95/p99 latency, audit write included. Published versions are cached in memory
by id (`VERSION_CACHE_SIZE`): they are immutable, so the cache never needs invalidating.

For reference, on a laptop with a local Postgres, `transaction-monitoring` at 16 concurrent
callers: without Redis \~850 req/s, p50 16 ms, p99 48 ms; with Redis \~1,440 req/s, p50 10.5 ms,
p99 24 ms. What remains is mostly the execution row itself, which is the audit trail.

## Caching model [#caching-model]

| Read (per execution)                             | Cached as                                         | Invalidated by                                                                                                                                |
| ------------------------------------------------ | ------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------- |
| API key lookup                                   | Redis, per key                                    | disabling the key, deleting its project                                                                                                       |
| Which version `latest` / `3` / `tag:x` means     | Redis, per project (and environment for `latest`) | publish, restore, deploy, promote, deleting a decision or table, the project                                                                  |
| Published decision and table bodies (pinned)     | process memory, keyed by the project's generation | the same project events                                                                                                                       |
| Console session, its user and project membership | Redis, per user                                   | sign-out, revoking sessions, password reset, 2FA change, role or membership change, custom role edit, SCIM deactivation, deleting the project |
| `last_used_at` of an API key or session          | written at most once a minute                     | —                                                                                                                                             |

### Generational invalidation [#generational-invalidation]

Each project and each API key has a **random generation token** in Redis. Entries are stored
under it, and a change rotates the token *after* its database commit. A reader takes the token
*before* reading the database, so a load that races a write is stored under a dead token and
never served.

Rotating orphans every entry at once on every replica, with no keys to enumerate; a lost or
evicted token simply becomes a new one, never an old one. Entries also expire after
`CACHE_TTL_MS` (60 s), which only matters if a rotation was lost while Redis was down.

### Deliberately not cached [#deliberately-not-cached]

* **Dashboard statistics** — they would be stale by design; rollup tables are the right tool.
* **The outbox** — it must commit in the same transaction as the execution.

## Redis is optional [#redis-is-optional]

Redis holds only data derived from Postgres, so it runs **without persistence** (a restored
snapshot could bring back entries that no longer match the database) and every use of it falls
back to the database when it is unreachable. `/readyz` reports it but does not fail on it.

## Database connections [#database-connections]

Each process holds a pool of `DB_POOL_MAX` Postgres connections (default 10). With several API
replicas and a separate worker, keep `replicas x DB_POOL_MAX + worker` under the server's
`max_connections`, or put PgBouncer in front. A request that cannot get a connection within
`DB_CONNECT_TIMEOUT_MS` fails fast instead of queueing. `DB_STATEMENT_TIMEOUT_MS` caps any single
statement; leave it off on the worker, whose retention sweep can run long.

## Writes and database resilience [#writes-and-database-resilience]

* **Write-behind.** With `EXECUTION_WRITE_MODE=batched`, a successful run with no actions is written
  after the response, with one `createMany` per batch (`EXECUTION_WRITE_BATCH_SIZE`), which takes the
  per-call commit out of the execute path. Failed runs and runs with actions are always written
  before the caller hears about it, and a graceful shutdown flushes first.
* **Fail fast when Postgres is unreachable.** A connection failure opens a short gate
  (`DB_UNAVAILABLE_COOLDOWN_MS`): requests are refused at once with `503` `UNAVAILABLE` and
  `Retry-After`, and after the cooldown a single request probes the database. Without it every
  request waited out its own connect timeout — up to \~26 s — and answered `500`.
* **Idle timeout.** `Bun.serve` closes a connection that sits idle for `HTTP_IDLE_TIMEOUT_S` (60 s),
  reaping connections nobody is using; the proxy in front should enforce the real deadline.
* **Read replica.** With `DATABASE_READ_REPLICA_URL` set, reads that tolerate a little lag — the
  dashboard, audit exports, backtests and monitoring — go to the replica and fall back to the
  primary when unset.
* **Outcome rollups.** `execution_outcome_rollups` summarises past hours; monitoring and drift read
  the rollups for whole hours and the live table only for the current hour, so they do not scan the
  whole `executions` history.
