Ankusa.Routes.Store.Redis (ankusa_redis v0.7.0)

Copy Markdown View Source

Route definitions in Redis, shared by every edge node, with a version counter and pub/sub invalidation.

Route management is the one part of Ankusa where every node must agree: node A accepting a webhook that node B rejects is an outage that looks like a provider problem. So definitions live in Redis and each node keeps a compiled snapshot of them in memory. The guard reads that snapshot — never Redis — on the request path: a network round trip per request, or per route change, would be a much worse trade than a few hundred kilobytes of duplicated route table.

Key layout

Everything hangs off the store's namespace (default "ankusa:routes:<instance>"):

KeyTypeContents
<namespace>:routeshashfield = route id, value = Ankusa.Routes.Route.to_stored_json/1 as JSON (tenant_id and source_id are left out while they are "default" and nil, so a node from before tenant routes still reads a table that has none)
<namespace>:ip_rulesstringthe global rules, JSON
<namespace>:versionstringmonotonically increasing counter
<namespace>pub/sub channela nudge: "the version moved"

Two nodes share definitions only if they are configured with the same namespace; the instance-scoped default means two instances in one VM (tests, a claim-check node beside an edge node) never collide.

Writes are conditional and atomic

Every mutation is one Lua script, so Redis applies it as a single step:

  • insert and replace are conditioned on the version the caller validated against (Ankusa.Routes.Store's contract). If Redis holds a different version, nothing is written and the answer is {:error, :stale} — after the node has reloaded, so the caller's retry validates against what is really there. insert also checks max_routes against the shared hash inside the same script, so two nodes racing for the last slot cannot both take it.
  • delete removes the route and bumps the version in one step, answers :not_found from Redis rather than from the node's copy, and, when it is made for one tenant, reads the stored route's owner in the same step and answers :not_found for another tenant's route.
  • put_ip_rules writes the rules and bumps the version in one step.

The condition is what keeps two nodes honest. Each validates a create against its own mirror, and a mirror lags by the length of a pub/sub round trip; without it, both could create the same id, or two enabled routes for one path.

A write whose new version is not exactly one past the node's own has raced another node's write, so the node reloads the whole table instead of applying its change to a copy that is missing the other's.

Invalidation

After a write the node publishes on the namespace channel. The message is a nudge, not data: a node reads the version and reloads only if it differs from the one it holds. Differs, not "is newer", because a flushed or restored Redis has a lower version, and a mirror that only reloaded upward would keep enforcing a table Redis no longer holds. The version and the definitions are read in one transaction, so a reload is always a consistent snapshot, and each one gets a new snapshot epoch, which retires every cached decision at once (Ankusa.Routes.Cache).

A missed broadcast is caught by a periodic tick (routes.store option tick_ms, default 30s) that does the same check. That is why the tick interval is the worst case staleness for a node that was disconnected while a route was changed.

The node subscribes — and waits for Redis to confirm the subscription — before it loads. Redix.PubSub.subscribe/3 returns before Redis has registered anything, and the load runs on another connection, so without the wait a write could land after the load and before the subscription is live, and be in neither. With it, that write is a message waiting in the mailbox. The supervisor is :rest_for_one with the pub/sub connection ahead of the state process, so a pub/sub connection that dies takes the state process with it and the restart subscribes again, instead of leaving a node that has silently stopped listening.

A publish failure is not treated as a write failure: the definitions are already durable in Redis and this node's mirror is already updated, so the only cost is that other nodes wait for their tick.

Loading at scale

A load reads the hash a page at a time (HSCAN … COUNT 1000), not with one HGETALL: no single command ties up Redis or the connection for the whole table, timeout_ms bounds each page, and load_timeout_ms (a much larger default) bounds the walk. A million definitions boot and reload without a command near a timeout. The snapshot is written in batches, and a change that arrives in step with this node's version is applied to it as one row change (Ankusa.Routes.Snapshot.mutate/3), not by republishing the table; only a write that raced another node's reloads the whole table. A table that would not fit routes.memory_budget_bytes is refused at the load.

Failing loudly

The store refuses to first boot against a Redis it cannot reach — sync_connect on the connection, and a load in the state process's init/1 — because the alternative is an edge that quietly denies every webhook. A store that is restarted once the instance has published a table does not: it resumes from that table without waiting on Redis, and loads once the subscription is confirmed (and on every tick) — so a store that crashes during a Redis outage comes back with the routes it had. Once running, a Redis error on a write is {:error, :store_unavailable} (the admin API reports it as a 503, and the connection reconnects on its own); reads keep being served from the in-memory snapshot, so a Redis outage does not start rejecting traffic.

Seeding happens only on a namespace's first boot, in one atomic script: of two nodes booting together exactly one writes the seed and the other loads it. A route deleted through the API is therefore never resurrected by a restart. (The default ETS store, having nowhere to remember a deletion, re-seeds on every boot.)

Summary

Functions

Returns a specification to start this module under a supervisor.

Options: url (required), namespace, tick_ms, timeout_ms (every command but a load's pages, default 5 s) and load_timeout_ms (a whole load, default 5 min) — the store options of config.routes.store's {module, opts} pair.

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

start_link(opts)

Options: url (required), namespace, tick_ms, timeout_ms (every command but a load's pages, default 5 s) and load_timeout_ms (a whole load, default 5 min) — the store options of config.routes.store's {module, opts} pair.