# Data residency

URL: https://lyeve.com/docs/features/data-residency/

> **Requires** a license with the `data-residency` feature. See [pricing](https://lyeve.com/pricing/).

Data residency records where each tenant's data must live and holds every
engine to it. You define the regions you run in, assign each
[tenant](https://lyeve.com/docs/features/tenants/) to one, and start each engine with the region it serves. A
write from a tenant that belongs in another region is refused and told where to
go.

:::tip[Best for]
- **Contracts that keep data in one country.** Assign an EU customer to your EU
  region, and an engine in the US refuses that customer's writes.
- **A sign-up page that picks the closest region.** Two public routes list your
  regions and return the nearest one to a point, with no credentials.
- **Answering an auditor.** One report lists every region, every tenant's
  assignment and how many tenants have none.
- **Keeping a record of your recovery plan.** Note each tenant's replica region
  with its recovery targets, and planned moves between regions.
:::

## How enforcement works

When an engine starts with `INSTANCE_REGION` set, the Content API checks every
write (any method except `GET`, `HEAD` and `OPTIONS`):

| Tenant | Result |
|---|---|
| No region assigned | Writes anywhere. |
| Assigned to this engine's region | Writes as usual. |
| Assigned to another region | `421 Misdirected Request`. |
| Region cannot be read | `503` with `cannot determine the tenant's data region`. The write is never let through. |

```http
HTTP/1.1 421 Misdirected Request
X-CMS-Region: us-east-1
X-CMS-Region-Target: eu-west-1
X-CMS-Region-Reason: data-residency-enforced

{"error":"write routed to wrong region","target_region":"eu-west-1"}
```

Route the request to an engine in `X-CMS-Region-Target`. Reads are not checked,
and neither is the Admin API. Without `INSTANCE_REGION`, nothing is enforced.
With it, every Content API response carries `X-CMS-Region` with this engine's
region, so a load balancer can route on it.
Residency decides where a tenant's writes land. It does not look at where a
client connects from.

Uploads follow the same assignment. A tenant's files go only to storage
providers in its region, and an upload whose region cannot be read answers
`503` rather than land elsewhere. See
[object storage](https://lyeve.com/docs/features/object-storage/#keep-a-tenants-files-in-its-region).

## In the console

Open **Settings > Data residency**. **Where tenants are** counts the regions,
the tenants, the replicated tenants and the tenants not placed. Below the
counts it lists how many tenants each region holds, and each tenant's region,
replica and assignment date. **Regions** lists every region with where it runs
and whether it accepts tenants. **New region** opens a form with **Slug**,
**Name**, **Runs on**, **Latitude**, **Longitude**, **Accepting new tenants**
and **Default for new tenants**.

(Image: The Data residency page with two regions. Northwind Traders is placed in eu-west-1, and the regions EU West (Ireland) and US East (Virginia) both accept tenants.)

LyEve Console does not assign a tenant to a region on this release. Do that
over the API, as [Try it](#try-it) shows.

## Try it

This defines a region, assigns a tenant to it and reads the report. Sign in as
a super admin. The tenant here is `northwind`, made on
[Tenants](https://lyeve.com/docs/features/tenants/). A new install lists no tenant until you make one.

Create a region:

<div class="paths">
<div data-path="console">

1. Open **Settings > Data residency** and choose **New region**.
2. Enter `eu-west-1` in **Slug** and `EU West (Ireland)` in **Name**. Leave
   **Runs on** as `aws`.
3. Enter `53.35` in **Latitude** and `-6.26` in **Longitude**. Leave
   **Accepting new tenants** on.
4. Choose **Create**. The page says **Created EU West (Ireland)**, and the
   region is listed under **Regions** as **Accepting tenants**.

</div>
<div data-path="api">

You need a super admin token in `TOKEN`. The
[quickstart](https://lyeve.com/docs/getting-started/quickstart/) shows how to get one.

```bash
curl -X POST http://localhost:3001/api/admin/regions \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"slug": "eu-west-1", "display_name": "EU West (Ireland)", "provider": "aws", "coordinates": {"lat": 53.35, "long": -6.26}}'
```

The answer is `201` with the region. Copy its `id` into `REGION`.

</div>
</div>

Assign the tenant. Only the API does this on this release. If you work in the
console, sign in over the API to get a token, as
[Turn on drafts and publishing](https://lyeve.com/docs/getting-started/first-content-type/#5-turn-on-drafts-and-publishing)
shows, and keep it in `TOKEN`.

1. Find the region's id and the tenant's id. A tenant is assigned by its id,
   not its slug:

   ```bash
   REGION=$(curl -s http://localhost:3001/api/admin/regions \
     -H "Authorization: Bearer $TOKEN" \
     | jq -r '.data[] | select(.slug == "eu-west-1") | .id')
   TENANT=$(curl -s http://localhost:3001/api/admin/tenants \
     -H "Authorization: Bearer $TOKEN" \
     | jq -r '.data[] | select(.slug == "northwind") | .id')
   ```

2. Assign it:

   ```bash
   curl -X POST http://localhost:3001/api/admin/tenants/$TENANT/region \
     -H "Authorization: Bearer $TOKEN" \
     -H "Content-Type: application/json" \
     -d "{\"region_id\": \"$REGION\"}"
   ```

   ```json
   {
     "id": "909c7ca6-f9ed-4445-82bb-51f62b836bd7",
     "tenant_id": "3f64a521-1146-481c-aaf8-a3334f505aa3",
     "region_id": "76e7b309-80f4-4341-a958-2b15dacbbe44",
     "region_slug": "eu-west-1",
     "assigned_at": "2026-10-02T09:14:56Z"
   }
   ```

Read the report:

<div class="paths">
<div data-path="console">

Open **Settings > Data residency**. **Where tenants are** reads **1** under
**Regions**, **Tenants** and the region's count, and **0** under
**Replicated** and **Not placed**. The table below lists **Northwind
Traders** in **eu-west-1** with no replica and when it was assigned.

</div>
<div data-path="api">

```bash
curl http://localhost:3001/api/admin/residency/report \
  -H "Authorization: Bearer $TOKEN"
```

It lists `regions`, every tenant's assignment under `tenant_regions`, and a
`summary`:

```json
{
  "total_regions": 1,
  "total_tenants": 1,
  "tenants_per_region": {"eu-west-1": 1},
  "replicating_count": 0,
  "unassigned_count": 0
}
```

</div>
</div>

Ask for the nearest region the way a sign-up page would, with no credentials.
This is a public Content API route, the same on both paths:

```bash
curl -X POST http://localhost:3002/api/v1/regions/nearest \
  -H "Content-Type: application/json" \
  -d '{"lat": 48.85, "long": 2.35}'
```

The answer is the region with `distance_km`, about `781` from Paris to
Dublin.

## Turn it on

1. Run with a license that includes `data-residency`. If you set
   `LYEVE_PLUGINS`, include `data-residency` in it. See
   [Licensing and tiers](https://lyeve.com/docs/concepts/licensing-and-tiers/).
2. On each engine, set `INSTANCE_REGION` to the slug of the region it serves.
3. Create regions in the console or over the API, and assign tenants over the
   API.

Without the license the feature does not start, and its routes answer `404`.

## Manage regions

Regions need a super admin. `slug` and `display_name` are required, and
`provider` defaults to `aws`. `coordinates` lets the nearest-region lookup use
the region. `is_default` marks the default region, and marking one clears the
mark from the others. `PUT` also takes `enabled`. Only enabled regions are
listed publicly.

A region with `enabled: false` is closed to new tenants. Assigning a tenant to
it answers `409` with the code `data_residency.region_closed`, even for a
tenant already there. Tenants already assigned to it keep their assignment. A
closed region cannot be the default: marking a closed region `is_default`, or
closing the default region, answers `400`. Make another region the default
first.

An admin can read and set their own tenant's region. A super admin can do it for
any tenant. `GET /api/admin/tenants/{id}/region` returns the assignment, or `404`
with `no region assigned`.

## Record replicas and planned moves

These record your disaster-recovery setup and planned moves between regions.
The engine stores them and shows them in the report. Copying the data is up to
you.

- `POST /api/admin/tenants/{id}/replication` takes `replica_region_id`,
  `target_rpo_secs` (default `3600`) and `target_rto_secs` (default `1800`),
  and answers `201` with a record in status `syncing`.
- `PUT /api/admin/tenants/{id}/replication/{rid}` updates `status`, one of
  `syncing`, `synced`, `failed` or `paused`, and the lag.
- `POST /api/admin/tenants/{id}/migrate` takes `to_region_id` and an optional
  `requested_by`, and answers `202` with a migration record in status
  `estimating`.

## Public region lookup

Two Content API routes need no credentials:

- `GET /api/v1/regions` lists enabled regions, paginated (`limit` default 50,
  `offset`). 30 requests a second per address, bursts of 50.
- `POST /api/v1/regions/nearest` takes `{"lat": ..., "long": ...}` and answers
  `{"region": {...}, "distance_km": ...}`. 10 requests a second per address,
  bursts of 20.

## Settings

| Variable | What it does | Default |
|---|---|---|
| `INSTANCE_REGION` | The region slug this engine serves. Set, it turns on write enforcement. | unset |

:::caution[Records only]
Replica and migration entries are records you keep, and they move no data.
:::

## Routes

<details>
<summary>Regions, assignments and records</summary>

| Method | Path | Who |
|---|---|---|
| `GET`, `POST` | `/api/admin/regions` | super admin |
| `GET`, `PUT`, `DELETE` | `/api/admin/regions/{id}` | super admin |
| `GET`, `POST` | `/api/admin/tenants/{id}/region` | admin of that tenant |
| `GET`, `POST` | `/api/admin/tenants/{id}/replication` | admin of that tenant |
| `PUT`, `DELETE` | `/api/admin/tenants/{id}/replication/{rid}` | admin of that tenant |
| `POST` | `/api/admin/tenants/{id}/migrate` | admin of that tenant |
| `GET` | `/api/admin/tenants/{id}/migrations` | admin of that tenant |
| `GET` | `/api/admin/tenants/{id}/migrations/{mid}` | admin of that tenant |
| `GET` | `/api/admin/residency/report` | super admin |
| `GET` | `/api/v1/regions` | anyone |
| `POST` | `/api/v1/regions/nearest` | anyone |

</details>

## Errors

| Status | Message | Cause |
|---|---|---|
| `421` | `write routed to wrong region` | The tenant belongs in another region. |
| `403` | `forbidden: super_admin required` | A tenant admin called a region or report route. |
| `403` | `forbidden: cannot access another tenant's data` | A tenant admin named another tenant. |

<details>
<summary>Every error</summary>

| Status | Message | Cause |
|---|---|---|
| `400` | `slug is required`, `display_name is required` | Region created without them. |
| `400` | `a closed region cannot be the default` | A closed region marked `is_default`, or the default region closed. |
| `400` | `region_id is required`, `to_region_id is required` | Assignment or migration without a region. |
| `400` | `invalid status: must be one of syncing, synced, failed, paused` | Unknown replication status. |
| `404` | `region not found`, `no region assigned`, `replication not found`, `migration not found` | Wrong id. |
| `409` | `region with slug "<slug>" already exists` | Duplicate slug. |
| `409` | `This region is closed to new tenants.`, with the code `data_residency.region_closed` | The region is not enabled. |
| `503` | `cannot determine the tenant's data region` | The tenant's region could not be read during a write. |

</details>

## Related

- [Tenants](https://lyeve.com/docs/features/tenants/): what a tenant is and how a request picks one.
- [PII masking](https://lyeve.com/docs/features/pii-masking/): keep personal data out of responses and logs.
- [Data protection](https://lyeve.com/docs/trust/data-protection/): how residency fits a vendor
  review.
