ADR 0016 — Storage and Counter Driver Deviations from the Siblings
Status: Accepted
Context
Section titled “Context”ADR 0004 ports the ten flags/webhooks KV drivers unchanged and adds a counter port. Porting surfaced
bugs in the sibling drivers (case-folding collations, a MySQL-invalid LIKE escape, case- and
normalization-insensitive filesystems, unstorage’s lossy key normalization) and the engine needs
compareAndSwap for revisioned customer writes, which webhooks only had in memory.
Decision
Section titled “Decision”State storage (same factory names and options; defaults say entitlements — xtandard_entitlements_kv):
- Exact keys everywhere. Postgres keys
text COLLATE "C"; MySQL keysvarchar(512) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin;LIKE … ESCAPE '!'(pg/mysql);GLOBon SQLite/libSQL; RedisSCAN MATCHglob-escaped; MongoDB regex-escaped; everygetKeyspost-filters withstartsWith. - File driver encodes path segments (
A→^a, other bytes outside[a-z0-9._~!@,=+-]→%XX, leading.→%2E, empty →%), writes via temp file + rename, and has an in-processcompareAndSwap(per-key lock). Mirrors@xtandard/links. - unstorage percent-encodes
% : \ ? $and lists the enclosing namespace, then filters. - compareAndSwap on every driver with an atomic conditional write: Redis/RedisJSON (Lua +
cjsonstructural compare), Postgres (jsonbequality), SQLite/libSQL (values stored as canonical JSON, singleUPDATE … WHERE value = ?), Drizzle (per dialect), MongoDB ($expr+$literalequality; values written with sorted keys). Not on unstorage / Cloudflare KV. - Webhooks’ delivery-queue code (
claimDue, due zset) is dropped.
Counters (new):
- SQL drivers share one core (
src/counters/sql.ts): one tablekey, value, applied, expires_at;addis a singleINSERT … ON CONFLICT DO UPDATE … RETURNING value, appliedwhose bounds decision runs in the statement, with theappliedcolumn carrying the decision back. MySQL (Drizzle) orders itsON DUPLICATE KEY UPDATEassignments and reads back in the same transaction. Numbers are inlined as finite literals; keys are parameters. - Claims share the counter keyspace under the reserved
_claims/prefix (ascounters/kvdoes). - Redis stores
%.17gstrings via Lua instead ofINCRBYFLOAT; MongoDB uses a pipelinefindOneAndUpdatewith upsert and a TTL index.
Consequences
Section titled “Consequences”- The shared conformance suites (case/accent, metacharacters,
? \ ] : #, CAS races, 50–100 parallel bounded adds) pass on every driver, live backends included. bun:sqlitedrivers are excluded from vitest coverage (covered bybun run test:bun).- Backport candidates for flags/webhooks: collations,
ESCAPE '!', file encoding, unstorage encoding, SQL/Redis/Mongo compare-and-swap.