What is a UUID? Format, versions and when to use one
Learn what a UUID is, how to read its version and variant, how v1, v4 and v7 differ, the real collision risk, and when to use one instead of a numeric ID.
Every system has to give each record a unique name, whether it's an order, a user or an uploaded file. The classic approach is to let the database count 1, 2, 3… That stops working well when several servers, offline apps or microservices create records at the same time without talking to each other.
That's the problem UUIDs solve: an ID that every machine can mint on its own, without asking anyone, and that still practically never repeats. To follow along with real values, open the UUID generator in another tab.
What is a UUID?
A UUID (Universally Unique Identifier) is a 128-bit identifier that anyone can create anywhere, with no central authority, and with a negligible chance of ever repeating. It's written as 32 hexadecimal digits in five hyphen-separated groups following an 8-4-4-4-12 pattern, such as 3cfaa0d8-6899-498f-a462-118becd14942.
The current standard is RFC 9562, published in 2024 to replace the older RFC 4122. GUID (Globally Unique Identifier) is simply what the Microsoft world calls the same thing.
How to read a UUID
It looks random, but two digits sit in fixed positions and tell you how the UUID was generated:
- Version: the first digit of the third group. In the example, the
4in498fmarks version 4. - Variant: the first digit of the fourth group. For standard RFC UUIDs it's always
8,9,aorb.
So you can tell the version of any UUID at a glance, and spot non-standard ones too. A value with 4 in the version slot but c in the variant slot isn't an RFC 9562 UUID. The generator's validator reports its version as non-standard and its variant as Microsoft (legacy), an old layout that still shows up in some systems.
Versions 1, 4 and 7 compared
| Version | How it's built | Strength | Watch out for |
|---|---|---|---|
| v1 | Timestamp + node ID (historically the MAC address) | Records when it was created | Can leak machine details; its bit layout doesn't sort by time |
| v4 | 122 random bits | Simple, reveals nothing, supported everywhere | Fully random, so new values land all over the index |
| v7 | 48-bit Unix timestamp in milliseconds + random bits | Sorted by creation time, great as a database key | Reveals roughly when the record was created |
There are other versions too. Versions 3 and 5 always produce the same UUID from a given name (using MD5 or SHA-1), version 6 is a reordered v1, and version 8 is reserved for custom layouts. There are also two special values: the nil UUID (00000000-0000-0000-0000-000000000000) and the max UUID (all f).
Why is UUID v7 better as a primary key?
Because it starts with the time. v7 UUIDs generated one after another come out in ascending order, so the database appends each new row at the end of the index, just as it would with a sequential number. With v4, each insert lands at a random spot in the index, which means more page splits and fragmentation on large tables.
One caveat: within the same millisecond, ordering is only guaranteed if the generator uses a counter or another monotonicity method described in section 6.2 of RFC 9562. Many libraries do, but check the documentation of the one you use.
Here is a v7 generated on November 12, 2026 at 14:30 UTC: 01a1d02a-5e40-7910-8431-5a7b6ba77138. Its first 12 digits (01a1d02a5e40) are exactly the milliseconds since 1970. An ID created one millisecond later would start with 01a1d02a5e41.
The trade-off is that anyone who sees the ID can estimate when the record was created. If that matters, as in a public link, v4 is still the more discreet choice.
The math behind collisions
A v4 UUID has 2¹²² possible values, about 5.3 × 10³⁶. Because of the birthday paradox, the chance of a repeat grows with the square of how many you generate, not linearly: you'd need around 2.7 × 10¹⁸ UUIDs for a 50% chance of a single collision. That's roughly one billion per second for about 86 years.
For perspective, a system that generates a trillion UUIDs has a collision risk of about 1 in 10 trillion. The real danger isn't the math but a poor random source. Always use a cryptographic generator, such as crypto.randomUUID() in the browser or your database's built-in function, and never Math.random().
UUID or auto-increment ID?
There's no single right answer. Here's how they compare:
- Choose a UUID when records are created in many places (offline apps, microservices, replicated databases), when you'll merge data from different sources, or when you don't want the ID to reveal how many customers or orders you have. A URL like
/order/1042practically invites someone to try/order/1043. - Choose a numeric ID when there's one database, a large volume and storage matters. A 64-bit integer takes 8 bytes, half the 16 bytes of a binary UUID and far less than its 36-character text form.
One practical tip: if your database has a native type, such as uuid in PostgreSQL, use it instead of storing the UUID as text. It takes less space and comparisons are faster.
Also keep in mind that hiding a sequential number is no substitute for access control: the server still has to check that whoever requests a record is allowed to see it. For real secrets, our guide on how to create a strong password shows what protects you beyond randomness.
Generate and validate UUIDs in your browser
The UUID generator creates 1 to 1,000 v4 UUIDs at once. You can pick the format (no hyphens, uppercase, or wrapped in braces GUID-style), copy them one by one or download the list as a .txt file. Its validator reads the version and variant digits of any UUID you paste and tells you whether it's valid and which version it is. Everything runs locally, using the browser's cryptographic generator.
The tool only generates v4. If you've settled on v7, generate the IDs where they'll be used, for example with PostgreSQL 18's uuidv7() function or v7() from the npm uuid package, then paste one into the validator to confirm its version.
Frequently asked questions
Are UUIDs case-sensitive?
No. Under RFC 9562, hexadecimal digits can be read in upper or lower case and mean the same value. The recommendation is to generate them in lowercase and compare them case-insensitively. Some Microsoft tools display GUIDs in uppercase inside braces, but the underlying 128-bit value is exactly the same.
How many characters long is a UUID?
In the standard text form, 36 characters: 32 hexadecimal digits plus 4 hyphens. Without hyphens it's 32. Stored in binary, it takes 16 bytes. If you use a text column, allow 36 characters; if your database offers a dedicated UUID type, prefer it to save space and make comparisons faster.
UUID, ULID or NanoID: which should I pick?
A ULID is also 128 bits and starts with a timestamp, but it's written as 26 Base32 characters and isn't an IETF standard. NanoID produces short random strings, 21 characters by default. If your database has a native UUID type, v7 gives you ULID-style ordering without leaving the standard.
What does the urn:uuid: prefix mean?
It's how a UUID is written as a URN, a Uniform Resource Name, as defined in RFC 9562 itself: for example, urn:uuid:3cfaa0d8-6899-498f-a462-118becd14942. You'll see it in XML documents, Atom feeds and some metadata standards. The value is the same; to validate it, just strip the prefix.
Can I shorten a UUID for URLs?
You can drop the hyphens or re-encode the 128 bits in another base, such as Base64URL, which gives 22 characters (our Base64 guide explains that variant). The value stays the same; only its representation changes. Just don't truncate the original UUID, because cutting digits shrinks the number of possible values and sharply raises the chance of collisions.