# Requirements and sizing

What the host needs, and the CPU, memory and disk to plan for each monthly event volume, with the PostgreSQL settings that go with each size and the queries that stay slow.

## Requirements

- **A Linux host** with Docker Engine and the Docker Compose plugin
  (`docker compose`, not the old `docker-compose` script). The images exist for
  `amd64` and `arm64`.
- **A domain name** with an `A` record pointing at the host. Caddy gets a
  Let's Encrypt certificate for it, so the host must be reachable from the
  internet on ports **80 and 443** while the certificate is issued and
  renewed.
- **The license key and the images** that come with your contract. See
  [Images and license key](/self-hosting/images).

Add an `AAAA` record only after checking IPv6. A client that reaches Docker
over IPv6 is seen by Caddy as the Docker bridge gateway, unless the Docker
daemon is set up for IPv6 (for example `ip6tables` enabled with the userland
proxy off). Every such client would then share one rate-limit bucket.

An instance with no internet access can run from the [offline package](/self-hosting/images#c-offline-package),
with the limit on HTTPS described there.

## Sizing

Pick the row for the number of events you expect to send in a month.

| Events per month | CPU    | Memory | Disk   |
| ---------------- | ------ | ------ | ------ |
| Up to 1 million  | 2 vCPU | 4 GB   | 20 GB  |
| Up to 5 million  | 4 vCPU | 16 GB  | 80 GB  |
| Up to 15 million | 8 vCPU | 32 GB  | 300 GB |

These three sizes are extrapolated, not measured. They come from a scale test
of the Cohorly cloud service at 15 million events a month, and the smaller
rows are scaled down from it. They hold for the current storage architecture
and can change in a future release. There is no recommended size above 15
million events a month.

What the scale test measured:

- **Disk:** 1.28 GB per million events (1.02 GB of table data and 0.26 GB of
  indexes). At 5 million events a month, a year of data is about 77 GB.
- **What the disk column holds:** at that rate, the disk of each row fills with
  roughly 12 to 15 months of events at the top of its band (1.28 GB per million
  events), before PostgreSQL's write-ahead log, backups kept on the same disk
  and index rebuilds. Plan more disk to keep longer, or set
  [retention](/self-hosting/configuration#retention) (for example
  `COHORLY_SELF_HOST_RETENTION_MONTHS=12`) to cap growth.
- **Ingestion:** 8 to 14 thousand events per second sustained on `/track`. This
  does not limit the size you choose.
- **The smallest size that works for 30-day queries with a filter:** 8 vCPU,
  32 GB and 300 GB of disk, where filtered 30-day queries finish in under 3
  seconds.

### Queries that stay slow

In the scale test, a query that names one event was fast: 95th percentile up
to 0.7 s over 30 days and 1.6 s over 90 days, with a warm cache. Keeping 3 or
12 months of history did not change the latency, because the events table is
partitioned by month and a query reads only the months it needs.

Other queries are slower, and **no size we measured serves a 90-day query with
a filter in under 3 seconds**:

- Filters on a property.
- The `$all_events` selection.
- A breakdown with no event chosen.
- A funnel with a filter.
- Flows.

The slowest of these took more than 110 seconds, the timeout ceiling, for a
property filter over 90 days, a filtered funnel over 90 days, a breakdown over
90 days, and flows over 30 and 90 days. A query that runs longer than
`COHORLY_QUERY_TIMEOUT_MS` (30 seconds by default) is cancelled and answers
`504` with the code `query_timeout`, and one whose statement would return more
rows than `COHORLY_QUERY_MAX_ROWS` (100,000 by default) is stopped and answers
`422` with the code `query_too_large`; see
[Operator settings](/self-hosting/configuration).

The first query after the database cache is cold took 24 to 84 seconds on the
90-day golden-path queries.

Limits of the test: the sizes were simulated with cgroup limits, on a machine
with other containers active, on a SATA SSD, with synthetic data, and the
heavy queries were measured with n between 2 and 4.

## PostgreSQL settings

The compose file takes its PostgreSQL settings from `.env`. The scale test
recommends `shared_buffers` at 25% of the host's memory,
`effective_cache_size` at 75% and `random_page_cost` at 1.1.

The compose file ships three ready-made profiles that follow the first two
rules. Paste the block that matches the host into `.env`; the default is
`small`.

| Profile | Host memory   | `PG_SHARED_BUFFERS` | `PG_EFFECTIVE_CACHE_SIZE` | `PG_WORK_MEM` | `PG_MAX_CONNECTIONS` | `COHORLY_PG_POOL_MAX` |
| ------- | ------------- | ------------------- | ------------------------- | ------------- | -------------------- | --------------------- |
| small   | 2 GB          | `512MB`             | `1536MB`                  | `8MB`         | `50`                 | `15`                  |
| medium  | 4 to 8 GB     | `1GB`               | `3GB`                     | `16MB`        | `100`                | `30`                  |
| large   | 16 GB or more | `4GB`               | `12GB`                    | `32MB`        | `200`                | `60`                  |

- The `small` profile starts the stack on a 2 GB host. That is enough to
  evaluate Cohorly; it is below the smallest row of the sizing table.
- The 2 vCPU / 4 GB row maps to `medium`, and the 4 vCPU / 16 GB row to
  `large`.
- The `large` profile is written for 16 GB. On the 32 GB row apply the rule:
  `PG_SHARED_BUFFERS=8GB` and `PG_EFFECTIVE_CACHE_SIZE=24GB`.
- `COHORLY_PG_POOL_MAX` is the pool of the API, reports and queries use a
  second pool of the API (`COHORLY_QUERY_POOL_MAX`, default 4), and the error
  worker has its own pool (`COHORLY_ERROR_WORKER_PG_POOL_MAX`, default 3).
  Keep twice the two API pools together below `PG_MAX_CONNECTIONS`.
- Raise `PG_SHM_SIZE` (default `256mb`) on the `large` profile if PostgreSQL logs
  `could not resize shared memory segment`.

The compose file has no variable for `random_page_cost`. To set it, replace
the `postgres` command in a `docker-compose.override.yml` next to the compose
file. A `command` in an override replaces the one in the compose file, so
repeat every setting:

```yaml
services:
  postgres:
    command:
      - postgres
      - -c
      - shared_buffers=${PG_SHARED_BUFFERS:-512MB}
      - -c
      - effective_cache_size=${PG_EFFECTIVE_CACHE_SIZE:-1536MB}
      - -c
      - work_mem=${PG_WORK_MEM:-8MB}
      - -c
      - max_connections=${PG_MAX_CONNECTIONS:-50}
      - -c
      - random_page_cost=1.1
```

### Maintenance the scale test suggests

These are recommendations from the scale test, not something Cohorly does for
you:

- After a restart, warm the cache with `pg_prewarm`, and raise
  `effective_io_concurrency` on SSD or NVMe storage. Both shorten the slow
  first queries.
- Run `VACUUM FREEZE` on the events partition of a month once that month has
  closed.
- Run `REINDEX` on a schedule for the partitions of the current month.

Event partitions are named `events_pYYYY_MM`.
