> ## Documentation Index
> Fetch the complete documentation index at: https://docs.oasm.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Scaling and high availability

> What scales and how: stateless core API replicas coordinated through Redis, horizontally scaled workers, the database as the single source of truth, and how background work runs exactly once across replicas

OASM is built so that every component except the data stores can run as several copies. This page explains what holds state, how replicas of the core API coordinate, and what an operator has to size.

For the deployment steps, see [Deployment](https://docs.oasm.io/deployment.md#scaling-workers) and [Worker](https://docs.oasm.io/worker.md#scaling-workers).

```mermaid theme={null}
flowchart TD
    LB["Load balancer"] --> A1["Core API replica 1"]
    LB --> A2["Core API replica 2"]
    W1["Worker 1"] -->|gRPC| A1
    W2["Worker 2"] -->|gRPC| A2
    W3["Worker N"] -->|gRPC| A1
    A1 <--> R[("Redis<br/>queues, locks, cache,<br/>event stream, pub/sub")]
    A2 <--> R
    A1 <--> PG[("PostgreSQL<br/>source of truth")]
    A2 <--> PG
    A1 <--> S3[("Object storage")]
    A2 <--> S3
```

## Where state lives

| Store | Holds | Loss means |
| - | - | - |
| **PostgreSQL** | Inventory, findings, jobs, runs, users, settings, audit trail. The only source of truth. | Data loss — back it up. |
| **Redis** | BullMQ queues and schedulers, distributed locks, the [event stream](https://docs.oasm.io/architecture/event-bridge.md), short-lived caches (sessions, statistics), worker join keys and pub/sub between replicas. | Recoverable: sessions fall back to the database, schedules are re-registered on boot, caches refill. Without Redis persistence, queued jobs and events not yet consumed are lost. |
| **Object storage** | Files, screenshots, reports, staged results. | See [Storage](https://docs.oasm.io/architecture/storage.md#what-happens-if-storage-is-lost). |
| **Core API process** | Open gRPC streams to workers, SSE connections to browsers, small caches. | Nothing durable: workers and browsers reconnect. |
| **Worker** | Its identity and encryption key on a volume, the tool cache, running containers. | Losing the volume mints a new identity; the old one is swept after 60 s. |

## Scaling the core API

The core API is stateless in the durable sense, so it scales by adding replicas behind a load balancer. Three patterns make that safe.

### Broadcast, holder acts

Some state is necessarily per process: a worker's gRPC streams and a browser's SSE connection are open to exactly one replica. When another replica needs to reach them — to cancel a job, to hint that new jobs are waiting, to deliver an AI agent command's output — it **publishes on a Redis channel**, every replica receives it, and only the replica that holds the stream acts. No replica needs to know where a worker is connected.

### Exactly-once background work

Cron jobs — retention, cleanup, rollups, the daily statistics snapshot, risk-acceptance expiry, the workflow-run reconciler — run on every replica's scheduler, but each one is wrapped in a **Redis lock** with a TTL. The first replica to take the lock does the work; the others skip that tick. The same applies to boot tasks such as connector sync and key rotation. Scheduled scans and project schedules are BullMQ jobs, which Redis already delivers once.

A few tasks need no lock because they are idempotent. The stale-worker sweep runs on every replica: deleting a worker that has no heartbeat, and re-queueing its jobs, gives the same result however many times it runs.

### The database decides

Where two replicas might race, the database arbitrates rather than an in-memory lock:

* **Claiming** uses `FOR UPDATE SKIP LOCKED`, so two workers on two replicas never get the same job. See [Claiming](https://docs.oasm.io/architecture/job-registry.md#claiming).
* **Terminal states** are conditional updates (`WHERE status = 'IN_PROGRESS'`), so a cancel and a result arriving together resolve cleanly.
* **Advancing a run** locks the run row and only happens when a step has drained, which every replica can check from committed data. See [Advancing a run](https://docs.oasm.io/architecture/job-registry.md#advancing-a-run).
* **Quotas and issue numbers** are single conditional statements or row locks, never read-then-write.

## Scaling workers

Workers pull work, so adding capacity is adding workers. Each worker sizes its own slots from the CPU and memory it can see, claims only as many jobs as it has free slots, and keeps its own identity even when several replicas share one host or one state volume. A queue that grows is always an execution-layer signal — not enough connected capacity — because jobs never time out of the queue. See [Capacity and scaling](https://docs.oasm.io/architecture/job-registry.md#capacity-and-scaling).

To scan from several networks, run workers in each network and attach them to an internal network, or scope them to the workspace that owns those targets.

## Sizing

| Setting | Guidance |
| - | - |
| **Database connections** | Each core API replica uses a pool of 30 connections. PostgreSQL's `max_connections` must cover the number of replicas × 30, plus migrations and maintenance. |
| **Result throughput** | Each replica processes up to 20 job results at once and evaluates at most 4 runs at once; add replicas when the result queue backs up. |
| **ASQ** | At most three concurrent query units per replica. Configure `ASQ_READ_REPLICA_URL` to move report-mode queries to a read replica. |
| **Redis** | Holds queues and the event stream, not bulk data; memory stays small. Use persistence so queues and schedules survive a restart. |
| **Retention** | Jobs, staged results, HTTP response history and audit events are pruned on schedules so tables do not grow without bound. See [Retention](https://docs.oasm.io/architecture/job-registry.md#retention). |

## Failure modes

* **A core API replica dies.** Its workers reconnect to another replica and resume with their tokens; their running jobs are not lost. Browsers reconnect their event stream. Jobs whose results were staged but not processed stay in the queue.
* **A worker dies.** After 60 seconds without a heartbeat it is removed and its jobs return to the queue for another worker.
* **Redis restarts.** Sessions are re-read from the database, schedules are re-registered when the API starts, and locks expire on their own. Worker gRPC streams do not go through Redis and stay up.
* **PostgreSQL is unavailable.** The platform cannot make progress until it returns, because every state change is recorded there first.

## Related

> [card] [Job registry](https://docs.oasm.io/architecture/job-registry.md)
>
> Claims, results and recovery in detail

> [card] [Worker and connector runtime](https://docs.oasm.io/architecture/worker-runtime.md)
>
> Worker identity, channels and capacity

> [card] [Event bridge](https://docs.oasm.io/architecture/event-bridge.md)
>
> Delivery guarantees of the event stream

> [card] [Deployment](https://docs.oasm.io/deployment.md)
>
> Run the stack and scale workers


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.