Choosing an identifier

CUID2, UUID v4, UUID v7, and database integers solve different allocation problems. Start with who must create the value, whether order may be visible, and which systems must understand it.

Review notes,

Claims checked against implementation-owner docs, RFC 9562, PostgreSQL docs, and OWASP guidance.

This guide turns four recurring architecture constraints into worked decisions. It avoids throughput and storage rankings because those require a benchmark of the reader's own runtime, schema, indexes, and workload.

Ask these questions in order

  1. 1

    Is this a credential?

    If possession grants access, stop comparing record-ID formats and design a bearer secret with lifecycle controls.

  2. 2

    Who allocates IDs?

    A single database can allocate integers. Offline clients and multiple writers need an independently generated format.

  3. 3

    Must other systems recognize it?

    Choose an RFC UUID where standards support is a contract; choose CUID2 only when its implementation-specific format is acceptable.

  4. 4

    May creation time be visible?

    UUID v7 and CUID v1 expose time information. UUID v4 and CUID2 do not expose a decodable timestamp field.

What each option actually commits you to

OptionAllocated byVisible orderingPublished contract
CUID2Application or clientDo not infer time from the outputImplementation-defined lowercase text; 24 characters by default
UUID v4Any conforming implementationRandom; no creation-time orderRFC 9562; 128 bits with 122 random bits
UUID v7Any conforming implementationUnix-millisecond timestamp in the most significant bitsRFC 9562; time field plus 74 remaining variable bits
Database identityOne database sequenceSequence allocation orderDatabase-specific integer/identity semantics

The facts behind this table come from the CUID2 implementation documentation, RFC 9562 UUID v4, RFC 9562 UUID v7, and PostgreSQL identity-column semantics.

Four worked decisions

Public database ID generated by an API

Decision: UUID v7 if RFC interoperability and time-ordered values matter; CUID2 if the team owns both ends and prefers its shorter default text form.

Rejected: database identity, because the API wants the final public ID before persistence. UUID v4 remains valid when ordering is irrelevant.

Offline-first client records

Decision: CUID2 or UUID v4/v7 generated on the client, then synchronized under a unique constraint.

Rejected: database identity as the client cannot reserve values while disconnected. The server must still define conflict handling; identifier choice alone does not merge records.

Internal child rows in one PostgreSQL database

Decision: a database identity can be the simplest choice when no external writer needs to preallocate keys.

Required: declare a primary-key or unique constraint. PostgreSQL explicitly notes that identity generation by itself does not guarantee uniqueness.

Password-reset link or session credential

Decision: use the security framework's purpose-built token/session facility or a design based on cryptographically secure random bytes, with expiry, secure storage, revocation, and single-use behavior where applicable.

Rejected: choosing CUID2, UUID, or a database ID merely because it looks random. A bearer secret is a protocol and lifecycle, not just a string format.

OWASP's forgot-password guidance requires cryptographically safe random generation, sufficient length, secure storage, expiry, and single use; its session guidance treats entropy and lifecycle as explicit controls.

Rules that survive every choice

  • Enforce uniqueness: theoretical collision resistance is not a database constraint.
  • Authorize every lookup: an unguessable-looking ID does not prove the caller may access the record.
  • Store the native type when it helps: evaluate the actual database representation and indexes instead of comparing only displayed character counts.
  • Record the generator and version: a format name is insufficient when behavior comes from a specific library.
Use the strategy advisorApply these constraints interactively.Implement CUID2Use the version-pinned reference.Model collision riskWork through the calculation.