ADR 0005 — Storage = KV Contract, Slug Pointers, CAS Uniqueness
Status: Accepted (★ extends flags/webhooks ADR 0005 with a key layout for links)
Context
Section titled “Context”The product promise is “point it at the database you already run”. Flags and webhooks proved a
four-method KV contract (getItem, setItem, removeItem, getKeys) plus feature-detected
capabilities (watch, transaction, compareAndSwap) ports to ten backends nearly mechanically.
Links add two requirements: global slug uniqueness per domain (two tenants creating go.acme.com/ sale at once must not both win) and a one-read redirect lookup.
Decision
Section titled “Decision”-
Keep
LinksStorage= the sibling contract verbatim, with the same capability interfaces andrequirePeerhelper; the 10 drivers port with renames. -
Key layout (
src/keys.ts):lnk/tenants -> string[] indexlnk/t/{tenant}/meta -> Tenantlnk/domains -> string[] index (hostnames)lnk/domains/{hostname} -> Domainlnk/s/{hostname}/{slugKey} -> SlugPointer { tenant, id } (the redirect lookup)lnk/t/{tenant}/links/{id} -> Linklnk/t/{tenant}/ext/{externalId} -> link idlnk/t/{tenant}/customers/{id} -> Customer (conversion attribution)lnk/clicks/{clickId} -> ClickRef (attribution window; pruned by retention)lnk/deferred/{fingerprint} -> DeferredHandoff[] (short TTL)lnk/t/{tenant}/audit-log -> AuditEntry[]slugKeyis the normalized slug (ADR 0012), URI-component-encoded. -
Slug claims use
compareAndSwap({ expected: null })when the storage supports it (memory, redis, postgres, sqlite, libsql, drizzle, mongodb); otherwise read-then-write with a re-read check (single-writer assumption, documented — the same stance as webhooks’ plain-KV dispatcher). -
The resolver reads
lnk/s/...thenlnk/t/.../links/{id}(two reads, cached per ADR 0002). -
Analytics never go through this contract except via the default KV rollup sink (ADR 0006).
Consequences
Section titled “Consequences”- Every sibling driver ports; only key prefixes change.
- Listing a tenant’s links scans
lnk/t/{tenant}/links/(fine at library scale; the UI paginates in memory per tenant). A secondary index per tag is not kept; tag filtering is a scan.