# Scaling api and worker replicas (https://docs.opensequence.dev/docs/self-hosting/scaling)

How many api and worker replicas to run, the knobs that raise throughput, what really limits sending, and how to stop the engine safely.

The `api` and `worker` roles scale on their own, and several replicas of each are safe. Read this page when you size a deployment or when a queue falls behind.

| Setting | `api` | `worker` |
|---|---|---|
| Replicas | 1 to N; scale on CPU or request rate | 1 to N; scale on queue depth or oldest-due lag |
| Resources to start | 500 mCPU, 512 MB | 500 mCPU, 512 MB |
| Port 8080 | public, behind TLS | private: health only |
| On `SIGTERM` | stops accepting, finishes in-flight requests, exits | stops reading queues, waits up to 30 s for in-flight messages, exits |

The whole shutdown is capped at 35 seconds, after which the process exits with code 1. Give the platform a grace period of at least 40 seconds. Work left unfinished reappears when its visibility timeout ends.

## Replicas are safe [#replicas-are-safe]

Two workers never send the same step twice:

- The planner claims due enrollments with `FOR UPDATE SKIP LOCKED`, so two planners never take the same one.
- An executor owns a run while it works on it (the enrollment's `owner_run_id`), and every write re-checks state under a row lock.
- Each scheduler job takes a lease row in `seq.job_locks` before it runs, so one replica at a time runs a given job.

The API is stateless apart from one per-replica cache: the rate-limit counter.

## The knobs [#the-knobs]

| Knob | Default | Raise it when |
|---|---|---|
| `SEQ_CONCURRENCY` | 8 messages in flight per queue per replica (max 64) | queues have depth and the database has headroom |
| worker replicas | 1 | the same, or for availability |
| `SEQ_DB_POOL` | 10 connections per process (max 100) | you raise `SEQ_CONCURRENCY`: the pool serves the in-flight messages and the scheduler |
| planner claim | 50 per sender, 500 per tick, every 5 seconds | thousands of enrollments come due in the same second. These are engine constants, not variables |
| `SEQ_QUEUE=redis` | `postgres` | the queue table itself is hot, with many thousands of messages a minute. Most installs never need it |

## What really limits throughput [#what-really-limits-throughput]

Usually not the engine. Thirty sequences with about 500 active contacts and a step every one to three days is 5,000 to 15,000 steps a day, a few hundred an hour at peak. One worker and one Postgres handle that easily.

The binding limits are outside: each sender's email `daily_limit` (30 by default, with a 60-second gap between sends), the SMS `daily_limit` (100 by default), and your SMS carrier registration. More capacity means more senders or higher caps, not more replicas.

## Measure the lag [#measure-the-lag]

Queue depth comes from the health endpoints (`GET /v1/admin/engine/health`). Oldest-due lag, how long enrollments wait past their `next_run_at`, has no endpoint yet; read it from the database:

```sql
select now() - min(next_run_at) as oldest_due_lag
from seq.enrollments
where status = 'active' and owner_run_id is null and next_run_at <= now();
```

Keep it under 2 minutes. If it grows while queues are empty, the planner is not running: check the `plan` job in the health output.

## Stopping the engine [#stopping-the-engine]

| Action | Effect |
|---|---|
| Scale the worker to zero | hard stop. Steps, signals and deliveries wait in the tables and resume when a worker returns. Nothing is lost |
| `SEQ_PAUSED=1` on the workers | the planner claims nothing and executors defer every send by 5 minutes. Signals are still recorded and webhooks still delivered. Needs a restart to change |
| `SEQ_DISABLED_CHANNELS=sms` on the workers | executors defer every send on that channel by 5 minutes; the planner keeps going. Needs a restart |
| project flag `global` off | executors defer that project's sends by 5 minutes, with no restart: `PUT /v1/engine/flags/global` with `{ "value": false }`. `channel:sms` does the same for one channel |
