-
Notifications
You must be signed in to change notification settings - Fork 0
Storage and Allocation
Production-safe human references require state. The canonical model is:
entity table reference table
MID primary key <--------- namespace + REF unique key
- Store the MID as the entity's canonical primary key, preferably as a native UUID or 16-byte value.
- Derive the PID from the MID and namespace unless a measured query pattern justifies indexing it.
- Store each REF as a unique value mapped to its MID and namespace.
- Keep retired namespace definitions available for historical parsing.
- Never use an in-memory uniqueness check as a production allocation boundary across processes.
A random REF candidate is not allocated until storage accepts it:
- Generate a payload using a cryptographically secure source and unbiased sampling.
- Format the prefix, payload groups, and modulo-37 check symbol.
- Attempt one atomic insert backed by a unique constraint.
- Return success only after the insert commits.
- On conflict, generate a new candidate and retry within a documented bound.
- Return
allocation_exhaustedwhen the bound is reached.
The TypeScript ReferenceStore.reserve boundary represents that atomic insert-or-conflict operation.
Sequential references require a transaction that:
- locks or atomically advances the namespace-and-scope counter;
- verifies that the fixed width is not exhausted;
- binds the allocated value to the supplied MID; and
- commits both changes together.
Calendar-year scopes use the UTC year. Sequential values reveal volume and ordering, so applications should choose this strategy deliberately.
When Alice enters Bob's ORD-… reference, the application should:
- normalize it against the active namespace registry;
- reject invalid length, symbols, or checksum;
- query the REF mapping table;
- treat no matching row as “not found,” not as a parsing error;
- load the entity by the returned MID; and
- run normal authorization before displaying it.
The PID may be derived from the MID for an API response or diagnostic event, but REF resolution does not require an intermediate PID.
The repository provides reversible migrations and tested functions:
-
identifold_reserve_referencefor atomic random reservation; -
identifold_allocate_sequencefor scoped transactional allocation; and - TypeScript adapters for direct PostgreSQL, Prisma, and Drizzle clients.
Apply the migration:
psql "$DATABASE_URL" --set ON_ERROR_STOP=1 --file integrations/postgres/migrations/001_identifold.up.sqlSee the PostgreSQL integration guide. Hosted PostgreSQL 18 tests cover concurrency, allocation ordering, client adapters, and reversible migrations.