Comparison
nanoid vs UUID vs ULID vs CUID2: Which ID Should You Use?
TL;DR
| UUID v4 | nanoid | ULID | CUID2 | |
|---|---|---|---|---|
| Length | 36 chars (with dashes) | 21 chars (configurable) | 26 chars | 24 chars (configurable) |
| Time-sortable? | No (v4); yes for v7 | No | Yes — lexicographically | No (by design) |
| URL-safe? | Yes but long | Yes (A-Za-z0-9_-) | Yes (Crockford base32) | Yes (lowercase + digits) |
| Leaks info? | No | No | Yes — embeds a timestamp | No — opaque by design |
| Standardised? | Yes (RFC 9562) | De-facto (library) | Spec (not an RFC) | Library (v2) |
| Best for | Interop, defaults | Short URLs, public IDs | DB keys that sort by time | Opaque IDs, anti-enumeration |
The options in depth
UUID (v4 / v7)
The universal standard — reach for it unless you have a reason not to.
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
nanoid
Compact and URL-safe — great for public-facing IDs.
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
ULID
Lexicographically sortable by time — a tidy database key.
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
CUID2
Opaque and collision-resistant — built to resist enumeration.
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
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.