Developer Tools

UUID v7 vs ULID vs NanoID: Which ID Should You Use?

Modern applications need sortable, collision-resistant IDs. Compare UUID v7, ULID and NanoID and pick the right one for your database.

1 min read·Updated 2026-05-21

Why ID choice matters

Identifiers are not just labels; in a database they are often the primary key. The format of that key affects index size, write performance and how easy it is to page through data. Modern IDs put a timestamp first so new rows append to the end of the index instead of scattering across it.

The contenders

TypeLengthSortableBest for
UUID v436 charsNoLegacy systems, pure randomness
UUID v736 charsYesStandards-compliant sortable keys
ULID26 charsYesCompact, URL-safe sortable keys
NanoID21 charsNoShort unique tokens and slugs

Why timestamp-first helps

Random keys force the database to insert rows at random points in the index, which causes page splits and slows writes. A timestamp as the leading bytes means new IDs are always larger than old ones, so inserts land at the end. As a bonus, sorting by the key roughly sorts by creation time, which simplifies pagination and debugging.

Choosing between them

  • Need a standard format with hyphens? Use UUID v7.
  • Want something shorter and URL-friendly? Use ULID.
  • Just need a short unique token? Use NanoID.

Generate them safely

Use a cryptographically secure random source, not Math.random. Browser tools can call crypto.getRandomValues, which is what the UUID v7, ULID and NanoID Generator uses. If you only need a plain random UUID, the UUID Generator covers v4.

Do not use IDs as secrets

Sortable IDs are predictable in their timestamp portion, so never use them as access tokens or password-reset links. For that, generate a long random value and hash it - see the Hash Generator. Keep IDs for identification and secrets for security.

Tools used in this guide

Related guides