DevKits

Comparison

nanoid vs UUID vs ULID vs CUID2: Which ID Should You Use?

Every app needs unique IDs, but 'unique' isn't the only requirement. Do you need them short and URL-friendly? Sortable by creation time for a database primary key? Fully opaque so they leak no information? UUID, nanoid, ULID, and CUID2 each optimise for a different combination of those goals. Picking the wrong one means ugly URLs, index fragmentation, or IDs that leak a timestamp you wanted hidden.

TL;DR

 UUID v4nanoidULIDCUID2
Length36 chars (with dashes)21 chars (configurable)26 chars24 chars (configurable)
Time-sortable?No (v4); yes for v7NoYes — lexicographicallyNo (by design)
URL-safe?Yes but longYes (A-Za-z0-9_-)Yes (Crockford base32)Yes (lowercase + digits)
Leaks info?NoNoYes — embeds a timestampNo — opaque by design
Standardised?Yes (RFC 9562)De-facto (library)Spec (not an RFC)Library (v2)
Best forInterop, defaultsShort URLs, public IDsDB keys that sort by timeOpaque IDs, anti-enumeration

The options in depth

UUID (v4 / v7)

The universal standard — reach for it unless you have a reason not to.

UUID (RFC 9562) is understood by every database, language, and framework. v4 is fully random (no ordering, no info leak); v7 embeds a Unix timestamp in the high bits so IDs sort by creation time — a big win as a database primary key because it avoids the index-page fragmentation that random v4 causes. The downside is length: 36 characters with dashes makes for long URLs and larger indexes than the alternatives.

Good for

  • ·Maximum interoperability and tooling support
  • ·Database primary keys (use v7 for time-ordering)
  • ·When a standard matters more than compactness

Avoid when

  • ·Short, pretty public URLs
  • ·Storage/index size is critical at huge scale

Try it: UUID Generator →

nanoid

Compact and URL-safe — great for public-facing IDs.

nanoid produces short (default 21-char), URL-safe IDs from a cryptographically strong random source, with a collision probability comparable to UUID v4 at that length. The alphabet and length are configurable, so you can trade length for collision margin. It carries no timestamp and isn't sortable — it's purely a compact random ID, ideal for things like short links, invite codes, and public record IDs where you want something smaller and prettier than a UUID.

Good for

  • ·Short, URL-friendly public IDs
  • ·Invite codes, share links, slugs
  • ·When UUID's length is annoying

Avoid when

  • ·You need time-ordering (use ULID/UUIDv7)
  • ·You need a formal standard for interop

Try it: nanoid Generator →

ULID

Lexicographically sortable by time — a tidy database key.

ULID is a 26-character, Crockford-base32 ID whose first 48 bits are a millisecond timestamp, so sorting IDs as strings sorts them by creation time. That makes it excellent as a primary key (good index locality) and for anything you want naturally time-ordered. The trade-off: because the timestamp is embedded, a ULID leaks roughly when it was created — fine for internal keys, a consideration for public IDs.

Good for

  • ·Database primary keys that should sort by time
  • ·Event logs and time-series records
  • ·When you want sortability without UUID v7's format

Avoid when

  • ·You must not leak creation time
  • ·You need the broad tooling support of UUID

Try it: ULID Generator →

CUID2

Opaque and collision-resistant — built to resist enumeration.

CUID2 is designed around security and horizontal scaling: IDs are opaque (no embedded timestamp or sequence to guess), collision-resistant across distributed machines, and use a reduced character set that avoids ambiguous characters. It deliberately drops the sortability of its predecessor to improve unguessability. Choose it when you want IDs that reveal nothing and are hard to enumerate, at the cost of not being time-sortable.

Good for

  • ·Public IDs that must not be guessable or enumerable
  • ·Distributed systems generating IDs on many nodes
  • ·When opacity matters more than ordering

Avoid when

  • ·You need time-ordering for index locality
  • ·You need a widely standardised format

Try it: CUID2 Generator →

Which one should you pick?

→ What's the best ID for a database primary key?

A time-sortable one — UUID v7 or ULID. Random UUID v4 scatters inserts across the index and fragments B-tree pages, hurting write performance at scale. v7 and ULID keep recent rows together because their IDs increase roughly with time.

→ I want short, pretty IDs in URLs — which one?

nanoid. It's URL-safe and about 40% shorter than a UUID, with a tunable length/collision trade-off. If you also need the IDs to be unguessable, increase the length or use CUID2.

→ I don't want my IDs to reveal when a record was created.

Use UUID v4, nanoid, or CUID2 — none embed a timestamp. Avoid ULID and UUID v7 for public IDs, since both encode creation time that anyone can decode.

Common pitfalls

  • ⚠Using random UUID v4 as a clustered primary key at scale: the randomness fragments the index and slows inserts. Switch to UUID v7 or ULID for time-locality.
  • ⚠Exposing ULID or UUID v7 as public IDs when you don't want to leak timing: both embed a decodable creation timestamp.
  • ⚠Shrinking nanoid's length too aggressively: shorter IDs raise collision probability. Keep enough length for your expected volume (the default 21 is safe for most apps).

Frequently Asked Questions

Is nanoid more secure than UUID?

Both draw from a cryptographically strong random source. At its default 21 characters, nanoid has a collision resistance comparable to UUID v4 while being shorter. 'More secure' really comes down to length and whether the ID leaks information — nanoid and UUID v4 leak nothing; ULID and UUID v7 leak a timestamp.

Should I switch from UUID to ULID for my database?

Only if you want time-sortable keys and can accept that the ID reveals creation time. ULID (or UUID v7) improves index locality versus random UUID v4. If you're already happy with v4 and don't have an index-performance problem, there's no urgent need to switch.

What replaced the original CUID?

CUID2. The original CUID was deprecated by its author over collision and security concerns; CUID2 redesigns the algorithm to be opaque and more collision-resistant, trading away the old sortability.

Related comparisons