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

Publishes delivered hooks to a **Redis pub/sub channel** (`PUBLISH`). The
value is `Ankusa.Sink.Message`, byte-identical to what `Ankusa.Sink.RabbitMQ`,
`Ankusa.Sink.Kafka`, and `Ankusa.Sink.NATS` publish: inline up to
`:inline_max_bytes`, a claim ticket above it.

Pub/sub has no headers, so the envelope's `id`, `source_id`, and `tenant_id`
travel only inside that JSON — a consumer decodes the `Ankusa.Sink.Message`
body and reads them there.

## Pub/sub keeps no copy

`PUBLISH` is fan-out to *live* subscribers and nothing else: Redis stores
nothing on a channel, so a subscriber that is disconnected — or connects a
moment later — never sees the message. `deliver/3` therefore returns `:ok`
only when the server's reply says at least one subscriber received the
publish, and `{:error, :no_subscribers}` when it says zero: a hook nobody
heard is retried by the source's `Ankusa.RetryPolicy`, then dead-lettered
and replayable, instead of being recorded as delivered.

For the same reason `durable?/1` is `false` — a subscriber that disconnects
after the publish loses the message. A source whose only sinks are Redis
cannot run `wal.type: none` (`Ankusa.Queue.validate_config!/1` rejects it at boot), and a Redis
sink listed next to a durable one still gates every ack in that mode: boot
only needs one durable sink, but every sink has to confirm, so
`{:error, :no_subscribers}` is a `503` for the whole request. With the
default queue the hook stays in the store and the DLQ, which is what makes
replay possible at all. A Redis that *keeps* messages is a Redis Stream
(`XADD`), a different sink than this one.

Delivery is at-least-once: a retry or a DLQ replay republishes, so
consumers dedupe on the message's `idempotency_key`.

## Process model

The first `deliver/3` for an `{instance, url}` pair starts a `Redix`
connection under this package's `DynamicSupervisor` and returns at once
(`sync_connect: false`): the connect runs in the connection, never in the
caller or the `DynamicSupervisor`, so a slow or dead server cannot stall
other deliveries behind it. A publish before the socket is up is `{:error,
{:connection, :closed}}`, retried by the source's `Ankusa.RetryPolicy`, then
dead-lettered, exactly like any other sink failure; errors answered by a
live server (NOAUTH, WRONGPASS) are `{:error, {:redis, message}}`.

Redix keeps (re)connecting on its own with backoff, and a publish sent
meanwhile is `{:error, {:connection, :closed}}`. In the narrow window where
the connection dies between the registry lookup and the call, `deliver/3`
returns `{:error, :not_connected}` — also just a retry.

Connections are keyed by URL and registered under a digest of it
(`connection_key/1`) in the instance's registry, so two sinks pointed at the
same server share one connection, and two pointed at different servers never
do. Servers, credentials, and database are fixed by the first delivery that
starts the connection; use a different `:url` for a different server.

The URL's password is in neither the registry key nor the connection's child
spec: Redix is started with host/port/database options and a password MFA
whose argument is a closure, so `:sys.get_status/1` of the supervisor or the
connection, and a supervisor report, never print it.

## opts

  * `:url`                — required; `redis://[:user:password@]host:port[/db]`
                            (`rediss://` for TLS), passed to `Redix.start_link/2`
  * `:channel`            — required; a string, or `(Envelope.t() -> String.t())`
  * `:inline_max_bytes`   — default 64 KiB (65,536), configurable
  * `:max_message_bytes`  — the largest message published; default
                            `536_870_912`, Redis's `proto-max-bulk-len` (512 MiB)
  * `:publish_timeout_ms` — how long `deliver/3` waits for the `PUBLISH`
                            reply; default `5_000`
  * `:idle_timeout_ms`    — close the connection after this long without a
                            delivery through it, default `600_000` (10 min);
                            `0` never. Never under `:publish_timeout_ms` plus
                            1 s. See `Ankusa.Sink.Reaper`

---

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