UUIDs Explained: v4, v7, ULID and More
A UUID (Universally Unique Identifier) is a 128-bit value you can generate anywhere without coordinating with a central server, yet still trust to be unique. They're ideal for database primary keys, distributed systems, and public-facing IDs. But "UUID" covers several versions with very different behavior — and there are strong non-UUID alternatives too.
The format
A UUID is usually written as 32 hex digits in five groups: xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx. The M position encodes the version and N encodes the variant. So you can read a UUID's version straight from its 13th hex digit.
UUID v4 — random
UUID v4 is 122 bits of randomness (six bits are fixed for version and variant). It's the most common UUID, trivially generated with crypto.randomUUID(), and collision risk is negligible. The one downside: v4 values are random, so when used as a database primary key they scatter inserts across the index, hurting write performance and locality on B-tree databases like PostgreSQL and MySQL.
UUID v7 — time-sortable
UUID v7 (standardized in RFC 9562) fixes the database problem. Its first 48 bits are a Unix millisecond timestamp, followed by random bits. That makes v7 values monotonically increasing, so they sort by creation time and keep inserts at the "right edge" of the index — much friendlier to databases — while still being globally unique and hard to guess. For new systems that want UUIDs as primary keys, v7 is usually the better default.
ULID — a compact, sortable alternative
A ULID packs a 48-bit timestamp plus 80 random bits into 26 Crockford Base32 characters, e.g. 01KT8EZDCTY8JDDZYKFN15YXK5. Like UUID v7 it sorts by time, but it's shorter, case-insensitive, and URL-safe. ULIDs are a great fit when you want sortable IDs that are pleasant in URLs and logs.
Other identifiers you'll meet
- MongoDB ObjectID — a 12-byte (24 hex char) ID combining a seconds timestamp and random data; the default
_idin MongoDB. - Snowflake ID — a 64-bit numeric ID (timestamp + machine + sequence) popularized by Twitter and Discord; compact and time-sortable, but requires coordination of machine IDs.
- NanoID — a compact, URL-safe random string with a configurable length; great for short public IDs where time-sorting isn't needed.
- GUID — Microsoft's name for a UUID, often shown braced:
{XXXXXXXX-...}.
How to choose
- Need a primary key in a relational DB? Prefer UUID v7 or ULID for index locality.
- Need an opaque public ID? UUID v4 or NanoID — no embedded timestamp to leak creation time.
- On MongoDB? ObjectID is the natural fit.
- Security note: UUIDs are identifiers, not secrets. Never use a UUID as a password, API key, or session token — for those, use a high-entropy random secret instead.
Generate IDs now
The IDs & Strings tab generates UUID v4, UUID v7, GUID, Nil UUID, ULID, MongoDB ObjectID, Snowflake IDs, NanoID, Base58 tokens, slugs, and PINs — in bulk, locally in your browser.
Try it: generate a batch of UUID v7 or ULID values and see how they sort.
Open the ID generator