# Self-hosting overview

> What you run when you host cairnflow yourself, the deployment shapes, and the ports involved.

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

:::note
Prebuilt binaries for Linux (`x86_64`, `arm64`) and macOS (`arm64`) are published on
[GitHub Releases](https://github.com/cairnflowdev/cairnflow/releases) (`v0.1.0-beta.0`), and a
container image is available at `ghcr.io/cairnflowdev/cairnflow:v0.1.0-beta.0` (see
[Docker](/docs/self-hosting/docker)). You can also [build from source](#build-from-source) from the
[repository](https://github.com/cairnflowdev/cairnflow). The runtime is source-available under the
Business Source License 1.1.
:::

Cairnflow can run on your own machines against your own bucket. There is no database, no
coordinator service and no etcd to operate: every piece of state lives in object storage, and the
runtime nodes only read and write it.

## What you run

| Binary | Role |
|---|---|
| `cairnflow` | The runtime. Serves the HTTP API, calls your services, keeps journals, state and timers in the bucket. |
| `cairn` | A thin CLI. Every command is one HTTP request to a node, so anything it does can also be done with `curl`. See [CLI reference](/docs/reference/cli). |
| `cairn-ctl` | A recovery tool that reads and writes the bucket directly, with no node running: `verify`, backup, restore, trash. See [Backup and restore](/docs/self-hosting/backup-restore). |
| `cairn-query-worker` | Not run by hand. `cairnflow` starts it as a child process for each SQL query sent to `POST /query`, so a hostile query cannot take the node down. Ship it in the same directory as `cairnflow`. See [Security](/docs/self-hosting/security#sql-queries-run-in-a-child-process). |

`cairnflow`, `cairn` and `cairn-ctl` print the same version line with `--version`:

```text
cairnflow <version> (data=<data format>, knows=<migration steps>)
```

Use a `cairn-ctl` with the same `knows=` value as the nodes of your cluster; taking every binary
from the same build is the simplest way to guarantee it.

## Shapes

| Shape | Minimal command | Use when |
|---|---|---|
| One node, local disk | `cairnflow --data-dir /var/lib/cairnflow` | Development, a single machine, no failover |
| One node, in memory | `cairnflow --main-store memory://` | Tests. Everything is lost when the process exits. |
| One node, S3 | `cairnflow --main-store s3://bucket/prefix` | One machine, with state outside the machine |
| Cluster, S3 | `cairnflow --main-store s3://bucket/prefix --partitions N --host <address>` on every node | Failover, several nodes sharing one bucket |

Without `--data-dir` or `--main-store`, data goes to `./cairnflow-data`, relative to the working
directory at startup. If both are given, `--main-store` wins.

A single node is a cluster of one: every node runs the same lease protocol, so moving from one node
to several is a matter of starting more nodes on the same bucket. The local-disk shape is the
exception: `file://` storage supports one node only (see [Object stores](/docs/self-hosting/storage)).

## What the bucket holds

Everything: journals, state, timers, queues, the API key book, deployments, and the ownership
records of each key range. A node owns a range of keys through a lease written to the bucket with
compare-and-swap. When a node stops renewing, another node takes its ranges over from the bucket.

This is why the store must support conditional writes. Each node checks its store at startup and
refuses to run on one that does not.

## Ports

| Flag | Default | Serves |
|---|---|---|
| `--port` | `8080` | Clients: invoke, admin and introspection API, `/metrics`, `/health`, and the web console under `/ui` |
| `--internal-port` | `--port` + 1 | Other nodes only: cross-range messages, range hand-offs, forwarded requests. Every request is signed with the cluster secret. |

The ingress port returns `404` for internal routes. Keep the internal port reachable only from the
other nodes. A single node still opens it, because messages between ranges always travel over HTTP,
even to the same process.

By default a node listens on `127.0.0.1`. Use `--host 0.0.0.0` to accept connections from other
machines on a single node; in a cluster `--host` must be an address that peers can reach (see
[Running a cluster](/docs/self-hosting/cluster)).

## Build from source

You need Rust (the toolchain named in `rust-toolchain.toml` installs itself through `rustup`),
`protoc`, CMake and a C++ compiler. To include the web console you also need Node.js 22.18 or
newer.

```sh
git clone https://github.com/cairnflowdev/cairnflow.git
cd cairnflow
git submodule update --init      # the SlateDB fork in vendor/slatedb; the build needs it
(cd ui && npm ci && npm run build)   # optional: the web console, embedded at build time
cargo build --release
```

The binaries are in `target/release/`. Without the console step, `/ui/` shows a placeholder page.

## A first node

```sh
export CAIRNFLOW_ADMIN_KEY="cf_$(openssl rand -hex 24)"
cairnflow --data-dir /var/lib/cairnflow --port 8080
```

Authentication is on by default. `CAIRNFLOW_ADMIN_KEY` is a bootstrap key with the `cluster-admin`
role, at least 32 bytes long (the command above makes a 51-character key); a node with no key at all refuses to start unless you pass `--no-auth`, which is for a
development machine only. See [Security](/docs/self-hosting/security).

Register a service that is already listening (for example one started with `serve()` on port 9080)
and call it:

```sh
export CAIRNFLOW_TOKEN="$CAIRNFLOW_ADMIN_KEY"
cairn register http://127.0.0.1:9080
cairn services
cairn invoke Greeter greet --input '"world"'
```

The runtime calls your service at the URL you register, so that URL must be reachable from the
node, not just from your laptop.

## Next

- [Object stores](/docs/self-hosting/storage): URIs, credentials, and which stores are supported.
- [Running a cluster](/docs/self-hosting/cluster): several nodes, failover, splitting ranges.
- [Docker](/docs/self-hosting/docker): the image and a two-node compose example.
- [Configuration](/docs/self-hosting/configuration): flags, environment variables and defaults.
- [Security](/docs/self-hosting/security): keys, roles, console sign-in, SSO, TLS.
- [Operations](/docs/self-hosting/operations): health, metrics, logs, upgrades.
- [Backup and restore](/docs/self-hosting/backup-restore): `cairn-ctl`, backups, the trash.
