UUID vs ULID: Choosing the Right Identifier for Your Project
Primary keys and public identifiers used to be simple: an auto-incrementing integer. Distributed systems, microservices, and offline-first apps broke that assumption, and the space of alternatives expanded — with UUID and ULID emerging as the two most common choices. Both guarantee uniqueness without coordination, but they behave very differently in indexes, logs, and URLs, and picking the wrong one can cost you measurable database performance.
This guide compares UUID and ULID on format, randomness, ordering, and real-world storage behavior, then maps each to the workloads where it shines.
Formats Compared
UUID (Universally Unique Identifier) is a 128-bit value written in five hexadecimal groups:
550e8400-e29b-41d4-a716-446655440000
The version you pick matters: v4 is 122 random bits; v1 embeds a timestamp and MAC address; v7 (RFC 9562) is a newer timestamp-first variant; v5/v3 derive deterministically from a name via hashing.
ULID (Universally Unique Lexicographically Sortable Identifier) is also 128 bits, but arranged as 48 bits of timestamp followed by 80 bits of randomness, encoded in Crockford's base32:
01JR5Y9VT8YQ8WK7B2E0R5NBNE
- First 10 characters: millisecond timestamp since the Unix epoch
- Last 16 characters: cryptographically random
The whole value is 26 characters — no hyphens, case-insensitive by design, and safe in URLs and filenames without escaping.
Head-to-Head
| Property | UUID (v4) | ULID |
|---|---|---|
| Size | 128 bits, 36-char canonical form | 128 bits, 26-char form |
| Randomness | 122 bits | 80 bits |
| Timestamp | None (v4) / embedded non-monotonic (v1) | First 48 bits (ms precision) |
| Sortable by generation time | No (v4) | Yes, lexicographically |
| URL/file safe | Yes (hyphens fine) | Yes (alphanumeric only) |
| Collision resistance | 122 bits of entropy | 80 bits of entropy |
| Spec status | ISO/IEC 9834-8, RFC 9562 | Informal but widely adopted |
The Sortability Problem
This is the difference that matters most in practice.
UUID v4 Is Random — So Are Its Sort Orders
A v4 UUID carries no time information. Insert rows in chronological order and their UUIDs appear in the table in essentially random order. In a B-tree index, that randomness has consequences:
- Random inserts defeat page locality. Each insert lands somewhere in the index, not at the right edge, causing page splits and cache misses as the table grows.
ORDER BY created_at, idpatterns degrade when the index leading column ties and the UUID tiebreaker forces random lookups.- Index bloat grows faster than with monotonically increasing keys.
Measured impact varies — modern databases handle random keys better than folklore suggests, and PostgreSQL's index structure absorbs random inserts reasonably well — but at scale, the difference between appending and scattering index entries is real.
ULID Sorts Naturally
Because ULID embeds the timestamp as its leading characters, sorting ULIDs lexicographically sorts them chronologically. New rows get keys that sort to the right edge of the index — the same append-friendly behavior auto-increment provides, without the coordination overhead:
-- Equivalent ordering, both index-friendly
ORDER BY id -- when id is a ULID (time-ordered)
ORDER BY created_at -- when id is a random UUID
Same millisecond, multiple ULIDs? Implementations apply monotonicity (increment the random section) so ordering stays stable within the millisecond.
When UUID Wins
Maximum entropy in 122 bits. If collision resistance is the priority — security tokens, session IDs, or identifiers that must never be guessable — v4's full randomness beats ULID's 80 random bits. 80 bits still means ~10^24 values before birthday-bound collisions become plausible, which is plenty for rows, but v4 remains the conservative default for untrusted contexts.
Standards and ecosystem weight. UUID is an ISO standard with decades of library support in every language, database, and protocol. PostgreSQL has a native uuid type; MySQL treats it as CHAR(36) or BINARY(16); every API designer understands it. Where organizational standardization matters, UUID wins on ubiquity.
Deterministic derivation. Need a stable ID derived from a name? UUID v5 (SHA-1 based) and v3 (MD5 based) generate the same UUID for the same input — a property ULID does not have. Namespaced URLs to canonical IDs is a classic use case.
Fits in 16 bytes regardless of form. Store UUIDs as BINARY(16)/UUID types, not strings — the 36-character text form wastes 2.25× the storage of the raw value.
When ULID Wins
Database primary keys. The append-friendly index behavior plus 128-bit global uniqueness makes ULID an excellent surrogate key for distributed writers — multiple services inserting without a central sequence, still getting time-ordered IDs.
Log correlation and debugging. A ULID in a log line tells you when the entity was created, just by reading it:
01JR5Y9VT8QK5M3R2E... → created 2026-04-14 ~08:41 UTC
Random UUIDs require a database lookup to answer the same question. In incident response, that embedded timestamp is quietly valuable.
URLs and filenames. 26 unreserved characters, no hyphens, case-insensitive alphabet (Crockford base32 excludes I, L, O, U to prevent transcription errors). Cleaner in shortlinks, object keys, and file paths.
Client-generated IDs. ULIDs can be minted in the browser with no server round-trip, then synced later — the timestamp ordering helps merge offline operations sensibly.
What About the Other Options?
The identifier landscape has more contenders worth knowing:
- Auto-increment integers — fastest and smallest, but leak row counts, fail under concurrent writers without coordination, and are unsafe as public-facing IDs.
- UUID v7 — RFC 9562's timestamp-first UUID, essentially "ULID inside a UUID wrapper": time-ordered prefix plus 74 random bits. If your organization standardizes on UUID, v7 captures most of ULID's benefits while keeping the UUID ecosystem. PostgreSQL 18+ and MySQL 8.4+ have native support.
- Snowflake-style IDs — 64-bit values combining timestamp, worker ID, and sequence; smaller than both, but require worker-ID allocation.
- NanoID — shorter random identifiers (default 21 chars) without time ordering; excellent for public-facing share IDs. Compare formats with the NanoID and UUID Generator, which produces UUIDs, ULIDs, and NanoIDs side by side.
If index locality matters and your stack supports it, prefer ULID or UUID v7 over UUID v4 for primary keys; keep v4 (or better randomness sources) for security tokens.
Practical Migration Notes
Existing tables with integer keys do not need abandonment — a hybrid is common: keep the integer as the internal clustered key and add a ULID/UUID as the public identifier used in URLs and APIs. When going all-in:
- Store raw bytes (
BINARY(16)), not display strings — 16 bytes vs 36. - For lexicographic string storage, UUIDs need time-ordering (v7) to sort by time; raw v4 strings sort randomly either way.
- Generate with a CSPRNG — never
Math.random()in JavaScript for anything security-adjacent. - Set the identifier on insert (
CREATED_AT-style default) rather than trusting application code on every path.
Generate a batch in the UUID & ULID Generator and sort them: the ULIDs sort chronologically by construction, the v4 UUIDs do not — the difference described in this article becomes visible in one paste.
Frequently Asked Questions
Are ULIDs collision-proof? They have 80 bits of random entropy per millisecond — collisions are astronomically unlikely across realistic workloads, but v4 UUIDs carry more randomness. For security tokens where guessing matters, prefer v4 or a dedicated random-token source.
Is ULID smaller than UUID? Same 128-bit size; the representation is 26 characters vs 36. Stored as bytes, they are identical; stored as text, ULID saves ~28% of string space.
Should I use UUID v4 or v7 for new projects? For database primary keys where ordering helps, v7 (or ULID). For general-purpose public identifiers where ordering is irrelevant, v4 remains fine. Avoid v1 (timestamp-based but MAC-derived and awkward) in new designs.
Do ULIDs expose sensitive timestamps? Yes — they reveal creation time to anyone who can read the ID. If that is a leak (private events, sequential-secret contexts), use a random UUID instead.
Conclusion
UUID v4 maximizes randomness and standards support; ULID trades 42 bits of entropy for timestamp ordering, cleaner strings, and friendlier index behavior. For primary keys written by distributed services, ULID (or UUID v7) is the modern default; for security tokens and standards-bound identifiers, UUID v4 still holds the crown. Experiment with all three formats in the UUID & ULID Generator — and for a practical walkthrough of generating and storing them in an application, see Generate UUIDs Online; when you need IDs that embed meaning deterministically, the UUID Validator and namespace tools cover the derivation path.