# Evolu Relay

Evolu Relay provides sync and backup for Evolu apps. Evolu apps can use multiple relays simultaneously. For resilience, it's recommended to use two relays: a fast primary "home/company" relay (on‑prem or close to users) and a geographically distant secondary relay if the primary relay fails (hardware failure, network issues, etc.).

Evolu apps can combine self‑hosted relays with cloud relays. The relay's logic is platform-independent and needs WebSockets and SQLite with Evolu's SQL functions, which Evolu's SQLite core provides. The reference relay runs on Node.js. Relays on different providers keep Evolu apps syncing even when a major cloud provider goes down.

Direct peer‑to‑peer (P2P) sync between clients is technically possible and on our roadmap; implementation awaits additional local transports like Bluetooth, Wi‑Fi, LAN, etc.

For details on privacy, check the [privacy](https://evolu.dev/docs/privacy) docs.

## Hosting Evolu Relay

The reference relay is published as the [`@evolu/relay`](https://www.npmjs.com/package/@evolu/relay) npm package and the [`evoluhq/relay`](https://hub.docker.com/r/evoluhq/relay) Docker image. Its source code can be found at [github.com/evoluhq/evolu/tree/main/apps/relay](https://github.com/evoluhq/evolu/tree/main/apps/relay). [Supply chain](https://evolu.dev/docs/supply-chain#verify-a-release) shows how to verify both. To serve it over `wss://` from your own server, follow [Run on a server](https://github.com/evoluhq/evolu/tree/main/apps/relay#run-on-a-server) in its README.

> We provide a free `free.evoluhq.com` relay for testing, but no hosting because
> it's not Evolu's mission to compete with established clouds (yet). Our plan
> for Evolu is different: We will sell Evolu apps, Evolu HW devices, and provide
> paid support. Testing relay data may be deleted; do not rely on it as your
> only backup.

## Configuring Evolu Relay

The relay reads its listening port from `PORT`, defaulting to `4000`, and its
optional per-owner storage quota from `EVOLU_RELAY_MAX_OWNER_BYTES`.
No per-owner storage limit is imposed when this variable is unset. Set a quota
appropriate for your deployment; it does not limit total disk usage.
The quota counts every change an owner stores, updates and soft deletes
included: history is append-only, so deleting rows never lowers an owner's
usage.
Quota values must be size literals such as `512KiB` or `10MiB`, rather than
bare byte counts such as `1048576`.
`PORT` also supports hosting platforms that supply the listening port;
`EVOLU_RELAY_PORT` is not supported. Invalid supplied values and unknown
`EVOLU_RELAY_*` variables stop the relay with a validation message.
See the [package README](https://github.com/evoluhq/evolu/tree/main/apps/relay#configure)
for the full list.

```bash
PORT=4001 EVOLU_RELAY_MAX_OWNER_BYTES=10MiB npx @evolu/relay
```

The relay serves each connection's requests one at a time and serves other
connections between them, and one request holds it only briefly, but many
connections can still load it. Run it behind a reverse proxy that serves TLS,
and limit connections per IP address there.

The relay sends every connection an empty heartbeat at least every thirty
seconds while it is idle, and clients replace a connection that stays silent
for about two minutes, so the proxy's idle timeout must be longer than thirty
seconds. See
[OwnerWebSocketTransport](https://evolu.dev/docs/api-reference/common/local-first/Owner/interfaces/OwnerWebSocketTransport#heartbeats)
for the heartbeat contract.

## Access control

A relay admits anyone who knows an OwnerId. Whoever does can download the owner's encrypted changes and see the metadata that [Privacy](https://evolu.dev/docs/privacy#what-relays-see) lists. Ideally, users sync through private relays that admit only their own devices and the people they share data with. Evolu doesn't provide that access control: a relay knows a connection only by the OwnerId it names, which anyone who knows it can name too, and an admitted connection can sync any owner. `createRelay`'s `isOwnerAllowed` decides which OwnerId a connection must name to connect, but not who connects or which owners the connection then syncs. To store only some owners' data, such as paying customers', refuse other owners' writes with `isOwnerWithinQuota`. Access control has to come from how the relay is deployed, such as a home network, a VPN, or the host's own authentication. A relay that several customers share admits all of them, so each customer's OwnerIds must stay secret from the others.

A relay accepts an owner's writes only with the owner's write key, which every relay the owner syncs with and every collaborator has. It doesn't let them read or forge the owner's changes, which the owner's encryption key protects, but whoever has it and can connect to a relay the owner uses can store data that uses up the owner's quota and disrupts sync through that relay, as [Fingerprint](https://evolu.dev/docs/api-reference/common/local-first/Storage/type-aliases/Fingerprint) describes. A write key can't be rotated or revoked yet.

A relay keeps the write key of the first write it accepts for an owner. Whoever knows an OwnerId can register their own key by writing before the owner's devices do, and the relay then rejects the owner's writes until its operator deletes everything it stores for that OwnerId with the relay's [`deleteOwner`](https://evolu.dev/docs/api-reference/common/local-first/Relay/interfaces/Relay#deleteowner). Refuse the squatter's writes first, as `deleteOwner` describes, or its next write registers its key again.