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

The fleet store in Redis: one tenant quota and one dedupe window for every edge
node that shares a `namespace`.

    fleet: %{
      store: {Ankusa.RateLimiter.Store.Redis,
              url: "redis://redis:6379", namespace: "ankusa:fleet", pool_size: 4},
      timeout_ms: 20
    },
    rate_limits: %{scope: :fleet, default: %{rate: 10, burst: 20}},
    dedupe: %{scope: :fleet}

It implements `Ankusa.RateLimiter.Store` (`hit/4`, `reset/3`) and
`Ankusa.Dedupe.Store` (`settle/3`); `Ankusa.Edge.Fleet` is its only caller.

## Options

  * `:url` (required) — the Redis URL
  * `:namespace` — key prefix, default `"ankusa:fleet:<instance>"`. Nodes share
    limits and dedupe **only if they use the same namespace** (and the same
    limits, which are read from each node's own config)
  * `:pool_size` — connections per node, default `4`. A request picks one by its
    pid, so a node's hooks pipeline over them instead of queueing on one socket

## Key layout

| Key | Type | Contents |
| --- | --- | --- |
| `<ns>:rl:{<tenant>}:h` | string | the tenant's hook bucket: its GCRA theoretical arrival time, microseconds on Redis's clock; expires when the bucket is full again |
| `<ns>:rl:{<tenant>}:b` | string | the tenant's byte bucket, same encoding |
| `<ns>:dd:{<tenant>}:<len>:<source>:<key>` | string | `pending:<node>:<hook id>` (expires after `fleet.pending_ttl_ms`) or `committed:<hook id>` (expires after the source's dedupe TTL) |

Every key of one hook carries the same `{<tenant>}` hash tag, so the one script
that touches them is valid on a Redis Cluster. Time is Redis's own (`TIME`
inside the script), so nodes with skewed clocks still share one bucket.

## One round trip

`hit/4` is one `EVALSHA` for the whole hook: GCRA on every bucket, then the
event-key claim. A denied bucket writes nothing and claims nothing; a key that
is already claimed spends no bucket. The scripts are loaded with `SCRIPT LOAD`
at boot, and a `NOSCRIPT` (Redis restarted) is answered with `EVAL`, which
loads it again, inside the same call budget. `settle/3` pipelines a batch of
claim outcomes the same way.

## Budget

Every command carries the caller's `timeout_ms` as Redix's own command timeout,
so a Redis that answers in 300 ms holds a hook for `timeout_ms`, not 5 s.
Connecting never blocks the boot (`sync_connect: false`): a node starts, and
limits and dedupes per node, until Redis answers.

# `child_spec`

Returns a specification to start this module under a supervisor.

See `Supervisor`.

---

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