# `Ankusa.SourceStore.Redis`
[🔗](https://github.com/jamescarr/ankusa/blob/ankusa_redis-v0.7.0/packages/ankusa_redis/lib/ankusa/source_store/redis.ex#L1)

Writable source store shared by every node: API-managed, tenant-scoped
sources live in Redis, and each node keeps a mirror of them in ETS.

`Ankusa.SourceStore.Persistent` keeps the sources it is given in the node's
own `Ankusa.Store`, so a source created through node A's admin API is a `404`
on node B. This store keeps them in Redis instead: a source created, updated
or deleted on any node reaches every node configured with the same
`namespace`.

Reads (`fetch/2`, `get/3`, `list_tenant/2`) never touch Redis: they go to the
node's mirror (the same ETS layout `Ankusa.SourceStore.Persistent` reads),
so ingest costs no network round trip and a Redis outage does not start
`404`ing hooks.

## Options

`config.source_store = {Ankusa.SourceStore.Redis, opts}`:

  * `:url` (required) — the Redis URL
  * `:namespace` — key prefix, default `"ankusa:sources:<instance>"`. Two
    nodes share sources **only if they use the same namespace**.
  * `:tick_ms` — the periodic version check, default 30s
  * `:timeout_ms` — the timeout of every Redis command, an `HSCAN` page of a
    load included, default 5s
  * `:load_timeout_ms` — the deadline of a whole load (a boot or a reload
    reads the hash a page at a time, `HSCAN … COUNT 1000`, not with one
    `HGETALL`), default 5 min: a million sources load without any command
    near a timeout
  * `:sources` — seed sources from configuration, the same shape
    `Ankusa.SourceStore.Static` takes. Seeds are this node's and read-only:
    they are never written to Redis, never appear in `list_tenant/2`, and a
    Redis entry with a seed's id is skipped with a warning.
  * `:decoder` — `(source_id, spec_map -> keyword)`, as for `Persistent`:
    run on every write, and on every entry loaded from Redis (an entry that
    no longer decodes is skipped with a warning).

## Key layout

| Key | Type | Contents |
| --- | --- | --- |
| `<namespace>:sources` | hash | field = source id, value = `{"tenant", "name", "spec"}` as JSON |
| `<namespace>:version` | string | bumped by every write |
| `<namespace>` | pub/sub channel | a nudge: "the version moved" |

## Writes and invalidation

A write is one Lua script: the create/update existence check, the `HSET` or
`HDEL`, and the version bump happen in one step in Redis, so two nodes
creating the same source cannot both succeed. Last writer wins on updates,
as with `Persistent`. After a write the node publishes the new version; every
node that holds a different version reloads the hash (version and hash read in
one `MULTI`). A missed broadcast is caught by the tick, which is therefore the
worst-case staleness for a node that was disconnected during a write.

The node subscribes, and waits for Redis to confirm it, before its first load,
so a write that lands in between is a message in the mailbox, never lost.

## Failing loudly, and outages

A node refuses to *first* boot against a Redis it cannot reach: an edge with
an empty mirror would `404` every API-managed source. A store process that
crashes while the node runs keeps its mirror (the table survives in
`Ankusa.SourceStore.Redis.TableOwner`) and resumes from it without waiting on
Redis. A Redis error on a write answers `{:error, :store_unavailable}`
(the admin API's `503`), and nothing is written; reads keep serving the
mirror.

## Secrets at rest

With `secrets.kek` (or `secrets.kek_provider`) configured, the secret-bearing
fields of the `spec` in each hash entry are encrypted before the write
(`Ankusa.Crypto.Envelope`), so Redis, its persistence files and its backups
never hold a verify secret or a sink credential in the clear. Nodes decrypt
as they load; the mirror, and the admin API's merge-the-stored-secret
update, see plaintext. Every node of a namespace needs the same keys. An
entry a node holds no key for stops that node's *first* boot, and on a
later reload keeps its mirror and logs the entry on every tick.
`rewrap/1` moves every entry to the current key.

# `child_spec`

Returns a specification to start this module under a supervisor.

See `Supervisor`.

# `ready?`

Whether the mirror has been loaded from Redis (it carries the version it was
built from): what `Ankusa.Lifecycle.reconcile/2` waits for.

# `rewrap`

```elixir
@spec rewrap(atom()) ::
  {:ok, %{rewrapped: non_neg_integer(), current: non_neg_integer()}}
  | {:error, :store_unavailable}
  | {:error, {:secrets, String.t(), term()}}
```

Rewrite every entry under the current KEK (plaintext ones, and ones under a
`secrets.kek_previous` key). Returns `{:ok, %{rewrapped: n, current: m}}`.

# `start_link`

```elixir
@spec start_link(keyword()) :: Supervisor.on_start()
```

---

*Consult [api-reference.md](api-reference.md) for complete listing*
