TSIDs with typed prefixes
Status
Section titled “Status”Accepted.
2026-05-26 (the TSID package is in the first commit). The typed Go identifier types in
internal/ids were added on 2026-09-29.
Context
Section titled “Context”Every record needs an identifier. The requirements pull in several directions:
- Insert locality. The busiest tables take millions of rows. Random identifiers (UUIDv4) insert at random places in a B-tree index, which makes the hot part of the index large and slows inserts.
- Generated anywhere. Producers write events into their own outbox, in their own database, before the platform has seen them. They need to mint identifiers without calling the platform, in any of half a dozen languages, in a format the platform accepts.
- Readable in operations. A support engineer who sees
01234in a log cannot tell whether it is a client, a principal or an event. Mixing them up, such as passing a principal id where a client id was expected, is a real class of bug when both are plain strings in JSON and in the database. - Compact. Identifiers appear in URLs, logs, indexes and every foreign reference.
A tenant client and an OAuth client_id also share a word and are easily confused.
Decision
Section titled “Decision”Use TSIDs: a 64-bit time-sorted number encoded as 13 characters of Crockford Base32.
| Bits | Field |
|---|---|
| 63 to 22 | timestamp, 42 bits, milliseconds since the Unix epoch |
| 21 to 10 | sequence, 12 bits |
| 9 to 0 | random, 10 bits |
Within a process the generator is strictly increasing: the sequence counts up inside a millisecond, and when it wraps the generator borrows the next millisecond rather than repeat a value. A later fix made that counter update atomic (compare-and-swap on the millisecond and sequence), after a collision was found when the counter wrapped. Across processes, ids sort by millisecond and then by chance.
Entities that people and applications refer to carry a typed form, {prefix}_{raw}, with a
lower-case three-letter prefix: clt_ client, app_ application, prn_ principal, sub_
subscription, con_ connection, rol_ role, oac_ OAuth client, and so on. Columns are
VARCHAR(17): three letters, an underscore and thirteen characters.
Rules that go with it:
- Ids are always generated, never typed in by a person. The platform generates them for the records it creates, and every SDK ships a generator in the same format, so an application can mint ids for its own outbox rows and for its own entities with its own prefixes.
- The high-volume data-plane records (events, dispatch jobs, outbox rows) are bare 13-character TSIDs with no prefix. They are minted where they are written, usually by the producer’s SDK.
- Codes are separate from ids. Things that must line up across environments (application codes, event-type codes, role names, connection codes) use human-chosen codes. Ids differ in every environment.
- Inside the platform’s Go code, each kind of id has its own type (
ClientID,PrincipalID,ApplicationID). They are strings on the wire and in SQL, but the compiler rejects a swapped argument. The types do not validate on conversion;Parse*is the checking constructor for input from outside.
Consequences
Section titled “Consequences”Good
- Time-ordered keys keep index inserts at the right edge, and a list sorted by id is sorted by creation time. The age of a record can be read from its id.
- Self-describing ids in logs, errors and tickets, and the prefix separates a tenant (
clt_) from an OAuthclient_id. - Producers mint ids offline with no coordination, and idempotent batch endpoints can accept the producer’s id.
- 13 characters against 36 for a UUID.
Bad
- Ids leak creation time. This is accepted, because ids are not secrets.
- Uniqueness across processes depends on 10 random bits plus the sequence per millisecond, not on a central allocator. The collision risk is low but is not zero, so primary keys remain the backstop.
- The prefix catalogue defines
evn_(event) anddjb_(dispatch job), but the code that creates events and dispatch jobs generates unprefixed ids; consumers should treat those as opaque 13-character strings. - Not every column that holds an id has a typed Go field: where a column is filled from external payloads and unchecked, the type is left as a string on purpose.
- Prefixes are conventions, not enforced by the database. A column can hold an id of the wrong type.
Alternatives considered
Section titled “Alternatives considered”- UUIDv4. No ordering, 36 characters, no type information.
- UUIDv7 or ULID. Time-ordered like a TSID, but longer and with no prefix convention.
- Auto-increment integers. Compact and ordered, but need a central allocator, so a producer cannot mint them offline, and they reveal volumes.
- A snowflake-style service. Coordinated allocation adds a dependency to every producer.