# Run it in production

> Where the gateway's image comes from, how to run several replicas, which health probes to wire up, how it stops, and what keeps working while the realm can't be reached.

In production the gateway runs as a few identical replicas behind your TLS front end, each next to the API it guards.
Each replica decides on its own from its copy of the realm's keys, policies and revocations, so replicas need nothing
from each other except one shared file, and a short outage of the realm doesn't stop your API.

**The gateway's image isn't published yet:** ask your Cvyūh contact for it, and for each update. There's no
packaged deployment, such as a chart, yet: run the image the way you run your other containers.

## Before you begin

- **The gateway running** in front of one API, as in [Get started with Gateway](/gateway/get-started/), with its
  client active in the realm. Changing the client takes a realm administrator in a group holding `RealmOwner`, with
  the API.
- **A TLS front end**, such as your load balancer or ingress, that ends TLS and forwards plain HTTP/1.1 to the gateway.
- **A way to mount files** into each replica: the gateway's private key, and the nonce key below.

## How it works

### Where it runs

```
callers ─TLS─▶ TLS front end ─HTTP/1.1─▶ gateway replicas ─▶ your API
                                               │
                                               └─▶ the realm
```

Every replica reaches the realm at `https://foobar.cvyuh.com` and your API at `BACKEND`. Nothing reaches a replica but
your front end, and nothing reaches your API but the gateway.

### Several replicas

Replicas are interchangeable: run as many as your traffic needs, behind your front end's load balancing, with the
same settings. Two things must be the same on every replica:

- **The gateway's key** (`CLIENT_KEY`). Every replica signs in as the same client.
- **The nonce key** (`DPOP_NONCE_KEY`), when you require nonces for bound tokens. Give every replica the same key
  file, and a nonce one replica gave is good at the others. Make it once with `openssl rand 32 > nonce.key` and keep
  it with your other secrets.

Keep each client on one replica, with session affinity at your front end. A replica refuses a key-bound token's proof
the second time it sees it, and keeping a client on one replica holds every proof it sends to that rule.

Each replica polls the realm on its own, so each learns of a policy change or a revocation within one of its own
intervals.

### When the realm can't be reached

Each replica keeps the last good copy of everything it holds:

| What it holds | While the realm is unreachable |
|---|---|
| The realm's keys | Kept. Tokens keep verifying |
| Policies | Kept. Requests keep being decided on the last policies it fetched |
| Revocations | Kept for three poll intervals (30 seconds by default), then every request with a token is refused `503 revocations_stale` until a poll lands |
| A decision only the realm can make | Refused `403 upstream_unavailable` at once |

So a realm outage of under half a minute goes unnoticed by callers, except for policies about groups. Past that, the
gateway refuses rather than let through a token it can't vouch for: see
[Fails closed](/gateway/protect-apis/run-the-gateway/#fails-closed).

### Health probes

The gateway answers its probes on `ADMIN_LISTEN` (`127.0.0.1:8802` by default), apart from the traffic it guards. Set
`ADMIN_LISTEN=0.0.0.0:8802` where your orchestrator probes from outside the container, and keep that port private.

| Probe | Path | Answers `200` | Wire it as |
|---|---|---|---|
| Liveness | `/health/live` | While the process runs | Restart the replica when it fails |
| Readiness | `/health/ready` | While it takes requests; `503` once it's stopping | Send traffic only while it passes |
| Startup | `/health/startup` | Once it has made its first try at the realm's keys, policies and revocations | Hold the other probes until it passes |

`/health/backends` says, as JSON, whether the realm's key set can be reached: use it on a dashboard, not as a probe.

A replica that starts while the realm is unreachable still passes its probes, and refuses every request until its
first policies and revocations arrive. Watch for `503 revocations_stale` and `403 no_policies_loaded` after a
rollout, and the log line that says why the first load failed.

### Stopping

On a stop signal, a replica fails readiness at once and keeps serving for 5 seconds, while your front end stops
sending to it. It then finishes the requests in flight, for up to 10 seconds, and exits. Give each replica a stop
grace of at least 15 seconds, so no request is cut off in a rollout.

### Updating

Roll replicas one at a time: start the new one, wait for its startup probe, then stop an old one. Replicas of two
versions decide the same requests the same way, since every replica reads the same policies.

## Set it up

1. **Mount the same two key files** into every replica: the gateway's private key at `CLIENT_KEY`, and, if you require
   nonces, the nonce key at `DPOP_NONCE_KEY`.
2. **Give every replica the same settings**: see [Gateway configuration](/gateway/configuration/).
3. **Wire the probes** as in the table above, and a stop grace of at least 15 seconds.
4. **Point your TLS front end** at the replicas, and close every other way to your API.
5. **Collect each replica's log**, which holds its decisions: see
   [Logs and tracing](/gateway/configuration/#logs-and-tracing).

## Limits and defaults

| What | Default | Can it change? |
|---|---|---|
| Revocations kept while polls fail | Three poll intervals: 30 seconds | Through the revocation poll interval, longer than 3 seconds |
| Stop: serving after readiness fails | 5 seconds | No |
| Stop: finishing requests in flight | Up to 10 seconds | No |
| A decision asked of the realm | 3 seconds | No |
| Gateways per API | One API per gateway, any number of replicas | — |

The rest of the gateway's limits are on [Run the gateway](/gateway/protect-apis/run-the-gateway/#limits-and-defaults).

## Troubleshooting

| Symptom | Cause | Fix |
|---|---|---|
| A client meets `401 use_dpop_nonce` on most requests | The replicas have different nonce keys, so a nonce from one is refused at the next | Mount the same `DPOP_NONCE_KEY` file on every replica |
| After a rollout, every request is refused for a while | The new replicas started before they could reach the realm | Check the log for why the first load failed; the replica recovers at its next good poll |
| Requests are cut off during a rollout | The stop grace is shorter than 15 seconds | Raise it |

## Related

- [Run the gateway](/gateway/protect-apis/run-the-gateway/): what each request goes through.
- [Gateway configuration](/gateway/configuration/): every setting, health and logging.
- [Bound tokens at the gateway](/gateway/protect-apis/bound-tokens/): nonces, and the shared key.
