# High availability

> Several stateless gateways, standby gateways and PostgreSQL failover.

---

LLMS index: [llms.txt](/llms.txt)

---

> [!NOTE]
> Running multiple stateless read-write gateways against a single PostgreSQL primary is part of the core `pgsilo` MVP. Read-only hot standby gateways (`SILO_PG_READ_ONLY=on`) connected to physical PostgreSQL standbys are a **Preview** feature. `pgsilo` does not provide automatic database failover, old-primary fencing, or zero-RPO guarantees; persistence, replication, and failover are governed by your PostgreSQL HA cluster.

Because `pgsilo` gateways hold no local object files on disk, multiple gateways can share one PostgreSQL primary while PostgreSQL physical replication protects the underlying data.

## Add a writable gateway {#add-a-writer}

1. Install the exact same `silo` binary build on the new gateway host.
2. Point `SILO_PG_DSN` to the same PostgreSQL primary database (`silo`) and owner (`silo_owner`). Use identical root credentials, effective region, static SSE-S3 keys, `SILO_PG_SEGMENT_SIZE`, and `SILO_PG_MAX_OBJECT_SIZE`.
3. Install the PostgreSQL CA certificate and S3 TLS certificates covering the gateway's hostname or load-balancer SANs.
4. Let the first writable gateway finish schema initialization before starting additional gateways. Verify both `/minio/health/cluster/read` and `/minio/health/cluster` return HTTP `200` on the new gateway before adding it to the load balancer's write pool.

Each gateway opens up to `SILO_PG_MAX_CONNS` connections (`32` by default: `4` control, `2` coordination, and `26` data connections). Two default gateways consume up to 64 PostgreSQL connections before counting application SQL sessions, replication, and administration. Route read-write traffic with `/minio/health/cluster` and read-only traffic with `/minio/health/cluster/read`.

## Read-only gateways on a physical standby (Preview) {#standby-readers}

After configuring PostgreSQL physical streaming replication and confirming the standby has replayed schema 14 (`migration:0001` through `0014`), configure a read-only gateway against the standby endpoint:

```ini
SILO_PG_DSN="postgresql://silo_owner:URL_ENCODED_PASSWORD@pg-read.example.net:5432/silo?sslmode=verify-full&sslrootcert=/etc/silo/postgres-ca.crt"
SILO_PG_READ_ONLY="on"
```

A read-only gateway verifies schema compatibility at startup but never initializes, migrates, or runs background maintenance. Even if pointed at a writable primary, `SILO_PG_READ_ONLY=on` rejects all S3 mutation requests.

| Gateway & database state | `/minio/health/cluster/read` | `/minio/health/cluster` | Mutation behavior |
| --- | --- | --- | --- |
| Healthy writable gateway on primary | `200` | `200` | Allowed (subject to IAM / native auth) |
| Healthy explicit read-only gateway (`SILO_PG_READ_ONLY=on`) | `200` | `503` | Rejected with `AccessDenied` |
| Writable gateway whose database enters recovery / read-only | `200` (if readable) | `503` | Fails with retryable service/read-only error |
| Database unreachable | `503` | `503` | Fails (process liveness `/minio/health/live` may remain `200`) |
{.wide}

Standby reads reflect asynchronous replication lag and can be canceled by PostgreSQL recovery conflicts. Applications requiring read-after-write consistency must read from a primary gateway or coordinate against PostgreSQL LSN replay positions.

## IAM cache freshness and the 5-second lease {#iam-lease}

Gateways authorize S3 requests against an in-memory IAM and bucket-metadata snapshot governed by a strict **5-second lease** measured from the start of each database observation query:

- Gateways poll PostgreSQL every **1 second** and also listen on `LISTEN silo_control_rev` for immediate wakeups. `NOTIFY` is only a hint; missed notifications never cause permanent staleness.
- Schema 14 pairs the monotonic integer `revision` with a persistent random UUID `revision_token` updated in every control-plane transaction. After a failover, any change in `revision_token`—or any change in `pg_is_in_recovery()` state or listener reconnection—forces a full control-plane reload even if the integer `revision` matches.
- Until a required reload completes, an unexpired snapshot keeps its existing deadline without renewal; local invalidation revokes the lease immediately. Once the 5-second lease expires without a fresh database observation, IAM-authenticated requests fail closed (`503`) rather than serving stale permissions.
- Physical standby gateways verify freshness using replicated control-plane heartbeats written by a writable gateway on the primary. **Clocks on the primary and standby hosts must stay synchronized within 500 milliseconds.** Heartbeat age plus the 500 ms clock-skew allowance is subtracted from the 5-second lease window. Always keep at least one writable gateway running on the primary so standby heartbeats stay fresh.

For design details, see [IAM, Control Plane & Cache Freshness](/docs/s3/design/iam-control-plane/).

## PostgreSQL failover sequence {#failover-sequence}

1. **Fence the old primary** through your PostgreSQL HA manager (e.g., Patroni) and verify it can no longer accept writes.
2. **Promote the selected standby** according to your RPO policy and update the primary DNS/VIP endpoint.
3. **Verify writable gateways** reconnect to the promoted primary, observe `pg_is_in_recovery() = false`, and return HTTP `200` on `/minio/health/cluster`.
4. **Verify authorization and maintenance**: test both allowed and denied S3 requests on every gateway, confirm clock synchronization (`<= 500 ms`) and heartbeat replication for any remaining standbys, and verify via metrics (`minio_pg_lifecycle_leader`) that a writable primary gateway has acquired the maintenance lease.
5. **Restore application traffic**. Note that `revision_token` detects split-brain control caches after failover, but it does not fence an old primary or recover object writes lost during asynchronous replication.

---

Backlinks:

- [Architecture](/docs/s3/architecture/)
- [Operations](/docs/s3/operations/)
