# Object stores

> Store URIs, S3 credentials, the startup capability checks, and which object stores cairnflow supports.

Source: https://cairnflow.dev/docs/self-hosting/storage

Cairnflow keeps all of its state in one object store, the **main store**. Choosing the store is
the most important decision of a self-hosted deployment: the safety of leases and of the write-ahead
log depends on how the store behaves under concurrent and interrupted writes.

## Store URIs

| URI | Meaning |
|---|---|
| `file:///path/to/dir` | A local directory, created if missing. One node only. |
| `memory://` | The process's memory. For tests: everything is lost when the process exits. |
| `s3://bucket/prefix` | An S3 or S3-compatible bucket. `prefix` is optional. |

Pass the URI with `--main-store` (or `CAIRNFLOW_MAIN_STORE`). `--data-dir DIR` is a shortcut for
`--main-store file://DIR`.

Several clusters can share one bucket as long as each uses a different prefix.

`--wal-store URI` moves the write-ahead log tier to a different store; the prefix is taken from
`--main-store`. Most deployments keep one store.

Among cloud stores, only `s3://` is available. Native Google Cloud Storage and Azure Blob Storage
URIs are not supported.

## S3 credentials and endpoint

There are no command-line flags for credentials. The S3 client is configured from the environment
of the `cairnflow` process:

```sh
export AWS_ENDPOINT=http://seaweedfs:8333      # omit for AWS S3
export AWS_ACCESS_KEY_ID=...
export AWS_SECRET_ACCESS_KEY=...
export AWS_REGION=us-east-1
export AWS_ALLOW_HTTP=true                     # only when the endpoint is http://
export AWS_VIRTUAL_HOSTED_STYLE_REQUEST=false  # path-style requests, needed by SeaweedFS and MinIO
```

:::caution
The client reads **every** environment variable of the process, lower-cased, as possible S3
configuration. Plain variable names that match the client's aliases are picked up too: `ENDPOINT`,
`REGION`, `BUCKET`, `TOKEN`, `SESSION_TOKEN`, `ACCESS_KEY_ID`, `SECRET_ACCESS_KEY` and so on. A
stray `TOKEN=` in a container environment would be sent as a session token. Keep the environment
of the `cairnflow` process clean.
:::

Static access keys are the configuration that has been exercised. Other credential sources that
the S3 client understands (session tokens, web identity, container credentials) are read the same
way but have not been tested with cairnflow.

The backup tool can use a separate set of credentials for a backup bucket at another provider; see
[Backup and restore](/docs/self-hosting/backup-restore).

## Startup capability checks

Before writing anything, each node checks its store. If a check fails the node does not start, and
there is no flag to skip checks 1 and 2.

1. **Conditional create.** The node writes the same key twice with "create only if absent". The
   second write must be rejected, and the first object must be left intact. This is the fence that
   stops a stale leader from writing. Checked on every shape, and on both tiers when the
   write-ahead log is on a separate store.
2. **Compare-and-swap on ETag.** An update with the current ETag must succeed and an update with a
   stale ETag must be rejected. Leases are renewed with exactly this operation. Checked on the main
   store.
3. **Atomic PUT for the write-ahead log.** When a write to the log has an ambiguous outcome (a
   timeout, a retried request), the node resolves it by reading the object back. That is only safe
   if a PUT is whole-or-nothing even when the writer crashes mid-request. A store that can leave a
   torn object could lead the node to acknowledge a write that was lost.

Checks 1 and 2 test real behaviour, so any store either passes or not. Check 3 cannot be observed
at startup (a torn object only appears when a client dies mid-PUT), so for `s3://` it works as an
allowlist. The node identifies the store and runs only on stores known to be safe:

- **AWS S3**: no custom endpoint, or an endpoint under `amazonaws.com`;
- **Tigris**: endpoint `t3.storage.dev` or `fly.storage.tigris.dev`;
- **SeaweedFS**: identified by the `Server` header returned by the endpoint.

Any other `s3://` store, a proxy that rewrites the `Server` header, or an endpoint that does not
answer stops the node at startup until you declare the capability yourself:

- `--wal-atomic-put false` is safe on **every** store. An ambiguous log write is treated as fenced:
  the node stops itself and its ranges recover on another node (or on its restart). You trade some
  availability for safety.
- `--wal-atomic-put true` declares that PUTs are atomic under crashes. Use it only if you have
  verified that on your store.

The startup log prints which capability was used and why (declared, identified by endpoint, or
identified by header).

## Supported stores

| Store | Status | Notes |
|---|---|---|
| SeaweedFS | Verified, recommended for self-hosting | Passes all three checks. Crash and range split/merge testing was done on a single-process SeaweedFS; the distributed mode (several filers and volume servers) has not been measured. |
| Tigris | Verified | Use a **single-region** bucket (for example `iad`) or a multi-region one. A bucket created without a location is Global: strongly consistent only within a region, so a lease update could be judged against a stale copy. The region cannot be changed after creation. |
| AWS S3 | Accepted by the startup check, not yet measured | AWS S3 supports conditional writes, and the node checks them at startup. The project has not yet published latency or cost measurements on AWS, nor run its crash test for atomic PUTs there. |
| MinIO | Only with `--wal-atomic-put false`; not recommended for clusters | Passes checks 1 and 2. A local erasure-coded MinIO has been observed to tear a lease object when a client is killed mid-update, leaving that range without a leader for good. |
| Garage (v1.0.1) | Unsupported | Fails check 1: it accepts the second conditional write and overwrites the object. |
| Other S3-compatible stores (Ceph, gateways) | Not identified | The node refuses to start until you pass `--wal-atomic-put`. Checks 1 and 2 still run. |
| `file://` | One node only | See below. |
| `memory://` | Tests only | One process. |

To create and check a Tigris bucket in a single region:

```sh
aws s3api create-bucket --bucket my-bucket \
  --create-bucket-configuration '{"LocationConstraint":"iad"}'
aws s3api get-bucket-location --bucket my-bucket   # "global" means a Global bucket
```

## Limits of `file://`

A local directory passes the conditional-create check but has no compare-and-swap. As a result:

- only **one** node can run on it; the node holds an exclusive lock on the directory;
- range split and merge are not available, since they need compare-and-swap;
- objects that are normally updated with compare-and-swap (such as the flow-control rule book) fall
  back to plain overwrites, which is safe with a single writer;
- on a **case-insensitive** file system (the default on macOS, and NTFS), tenants `Acme` and `acme`
  share one directory: tenant isolation breaks, and purging one tenant deletes the other's objects.
  Use a case-sensitive file system, or only lower-case tenant names. S3 is not affected.

For more than one node, use an `s3://` store.

## Bucket settings worth turning on

- **Versioning.** Cairnflow moves its own deletions to a trash area for a week (see
  [Backup and restore](/docs/self-hosting/backup-restore)), but some low-level deletions do not go
  through it, and nothing in cairnflow can stop a deletion made from outside: an operator, a
  lifecycle rule, a store bug. Only the range table keeps its own history (see
  [Rebuilding the range table](/docs/self-hosting/backup-restore#rebuilding-the-range-table)); any
  other lost object comes back only from bucket versioning or from a backup. Keep old versions at
  least as long as it may take you to notice an incident; a periodic `cairn-ctl verify --deep` finds
  missing files before a read does. Cairnflow does not check at startup whether versioning is on.
- **A lifecycle rule on `archive/`.** Archived invocations never expire on their own (see
  [Operations](/docs/self-hosting/operations)). Keep every other lifecycle rule away from the
  cluster's prefix.
- **Object Lock on `audit/`**, if you need the audit log to be tamper-resistant. Cairnflow only
  creates audit objects; it never overwrites or deletes them.
