# The Redis driver

`driver: Driver.redis` reaches any Redis-compatible service, natively and on
Workers. Its one setting is `url:`, read from a `RedisUrl` setting, which is
always secret
([Connection types](/docs/database-and-other-connection-urls.md#connection-types));
[Where the counts are kept](/docs/rate-limits.md#where-the-counts-are-kept-rate_limits--store-)
shows one. It counts rate limits, keeps caches
([Caches](/docs/caches.md)) and keeps entities in Redis
([Entities in Redis](/docs/entities-in-redis.md)).

## The address

- **`rediss://` is TLS from the first byte**, the server's certificate checked
  for the address's host against Mozilla's roots natively and the platform's
  on Workers. `redis://` is plain TCP, for a server on a private network or on
  this machine.
- **A user and a password sign in**, and a database number after the host
  chooses that database, both written before anything else on each
  connection. The port is 6379 unless the address gives another.
- **What the driver can't read** leaves the connection unbuilt, so none of
  it is quietly ignored: options after `?`, which it reads none of, and a user
  with no password. The deployable's `runtime.started` line names the
  connection and why, never the address, and its rate limits are counted in
  memory.
- **One server.** The driver talks to one server, so a cluster that shards
  keys across servers, such as Redis Cluster or ElastiCache with cluster mode
  on, isn't one it can use.

There are no presets: each service is the driver with its own address.

| Service | `url:` |
|---|---|
| Upstash | `rediss://default:<password>@<endpoint>:6379`, the database's TLS address |
| Amazon ElastiCache, cluster mode off | `rediss://:<auth token>@<primary endpoint>:6379`, with encryption in transit on |
| Redis or Valkey you run | `rediss://<user>:<password>@<host>:6379/<database>`, or `redis://` on a private network |
| This machine | `redis://localhost:6379` |

## How a count is kept

Each count is one key, `jadpo:rate_limit:` and a hash of what it counts, never
the address or the user's id as written. One transaction starts the key's
window when it isn't there, adds the request and reads how long the window has
left, so the servers share one window, timed by Redis's clock, and a refused
client's `Retry-After` is what is left of it. The key expires with its window.

Every app's keys start the same way, so two apps counting in one database share
their counts. Give each app a database of its own, `/1` or `/2` at the end of
its address, or a Redis of its own where the service has one database.

Natively each of a server's workers keeps one connection, opened when it first
counts and again once it breaks, and each request's count is written to it as
it comes, the answers read back in order. On Workers each request opens its
own ([A Redis store on Workers](/docs/rate-limits-on-workers.md#a-redis-store-on-workers)).

## How a cache is kept

Each entry is one key, `jadpo:cache:entry:` and 32 hex digits of a hash of
the query, the caller and the arguments, expiring with its `keep:`. A
record cache's copy is one too, hashed from its entity and the record's
`id` alone, since one copy serves every caller, and listed under its
entity only. Each
entity has a generation, `jadpo:cache:generation:<Entity>`, and a set of
the entries that read it, `jadpo:cache:entries:<Entity>`, kept a day and a
minute after its latest entry. A read is one `MGET` of the generations and
the entry. Keeping an entry is one transaction (`SET` with `PX`, and
`SADD` and `PEXPIRE` for each entity), with the generations read again in
the same write. A clear is one transaction of `INCR`, `SMEMBERS` and `DEL`
for each entity, then a `DEL` of the entries the sets named. A cache that
doesn't answer within a second, refuses or breaks is left alone for 10
seconds, its reads going to the database, and logged once as
`caches.fallback`, never with a key. Two apps sharing a database share
their caches' generations, so give each a database of its own.

## How an entity is kept

An entity in Redis ([Entities in Redis](/docs/entities-in-redis.md))
keeps each record under `jadpo:entity:<table>:record:<id>`, as one JSON
value, `{"revision":3,"expires":...,"record":{...}}`: the revision Jadpo
keeps on it, when it expires by the app's clock, and the record as the
API's JSON. It expires `expires_after:` after its last write. Each unique
field's value has a key holding the record's `id`,
`jadpo:entity:<table>:unique:<field>:` and 32 hex digits of a hash of the
value, never the value as written. Each indexed field's value has two
sorted sets, `…:index:<field>:<hash>` of the records' ids, which a page
reads in order, and `…:expiry:<field>:<hash>` of when each expires,
which a count reads.

A read by `id` is one `MGET`, and a read by a unique field two. A page is
one script, run with `EVAL`, that takes the expired ids out of the sets
and gives the next ids with their records. A count is one `ZCOUNT`. An
action's writes commit as one more script, which checks every record's
revision and every unique value it takes, then makes every change, so
no other command comes between. The scripts are Lua, which Redis, Valkey,
Upstash and ElastiCache all run.

A store that can't answer fails the read or write `unavailable`, since a
record in Redis has no other copy to fall back to.

## When it can't count

A count that hasn't answered within a second, opening the connection included,
isn't waited for, and nor is one the store refuses, such as a wrong password,
or one whose connection breaks. The server, or the Worker's instance, counts
that request in its own memory, and every request after it for 10 seconds
without asking, then asks again. No request fails for it.

Each time the store stops answering is logged once, as `rate_limits.fallback`,
with the connection, why (`unreachable`, `timed_out` or `failed`) and what that
means, never a key. `GET /health/ready` doesn't ask Redis, as it doesn't ask a
cache. `jadpo dev` counts in its own memory whatever the store, since it is one
server, and a route test counts nothing
([What a client sees](/docs/rate-limits.md#what-a-client-sees)).
