Guide
UUID versions explained
There are eight UUID versions, and the right one makes a measurable difference to your database. Here is what each one contains, which ones the current standard recommends, and how unlikely a collision really is.
By Mark Stoumann ·
How long is a UUID?
A UUID is 128 bits, or 16 bytes. In its standard text form it is 36 characters long: 32 hexadecimal digits in five groups of 8, 4, 4, 4 and 12, separated by four hyphens. How many characters it takes depends on how you write it down:
| Form | Example | Length |
|---|---|---|
| Binary | 16 raw bytes | 16 bytes |
| Standard text | 019535d9-3df7-79fb-b466-fa907fa17f9e | 36 characters |
| Hex without hyphens | 019535d93df779fbb466fa907fa17f9e | 32 characters |
| URN | urn:uuid:019535d9-… | 45 characters |
| Base64 | AZU12T33efu0ZvqQf6F/ng== | 24 characters (22 without padding) |
| Decimal integer | Up to 2128 − 1 | Up to 39 digits |
For a database column, that means CHAR(36) if you must store text, but a native UUID type or BINARY(16) is less than half the size and compares faster. Microsoft tools sometimes wrap a GUID in braces, {…}, which adds two characters that aren’t part of the UUID itself.
The text form also tells you the version. The first digit of the third group is the version number (the 7 in 79fb above), and the first digit of the fourth group is 8, 9, a or b for every standard UUID. A regular expression that checks both:
/^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/iIt rejects the Nil and Max UUIDs, which have no version; add them separately if you use them. You can try it on your own IDs in the regex tester.
Every version at a glance
The UUID generator makes versions 4 and 7, the two most developers need. The full list comes from RFC 9562, which replaced the original UUID standard, RFC 4122, in May 2024 and added versions 6, 7 and 8:
| Version | Built from | Status in RFC 9562 |
|---|---|---|
| 1 | Timestamp (100 ns steps since 1582) and a node ID, often the MAC address | Legacy; use v7 instead if possible |
| 2 | DCE Security: v1 with a local user or group ID | Outside the RFC’s scope |
| 3 | MD5 hash of a namespace and a name | Use v5 instead where possible |
| 4 | 122 random bits | Current |
| 5 | SHA-1 hash of a namespace and a name | Current |
| 6 | Version 1’s fields reordered so they sort by time | For systems already on v1; otherwise use v7 |
| 7 | Unix time in milliseconds, then random bits | Recommended for time-ordered IDs |
| 8 | Anything, apart from the version and variant bits | Experimental or vendor-specific |
Two special values complete the set: the Nil UUID, all zeros (00000000-0000-0000-0000-000000000000), and the Max UUID, all ones (ffffffff-ffff-ffff-ffff-ffffffffffff). Both are handy as “none” and “end of range” markers. Uppercase and lowercase hex are equally valid, so compare UUIDs case-insensitively, or normalize them to lowercase before you store them.
Version 1 has a privacy history: it embeds the generating machine’s network card address, so a document’s UUID could identify the computer that created it. RFC 9562 now says MAC addresses should not be used in UUIDs at all.
What RFC 9562 changed
RFC 4122 had defined UUIDs since 2005. RFC 9562, published by the IETF in May 2024, replaces it as the standard to cite. Existing UUIDs stay valid; what is new:
- Versions 6, 7 and 8. Time-ordered IDs (v6 for systems already on v1, v7 for everything else) and a free-form v8 for vendor-specific layouts.
- The Max UUID, all 128 bits set to one, next to the existing Nil UUID.
- Clearer recommendations: prefer v7 over v1 and v6, and v5 over v3.
- Security advice for today: no MAC addresses in UUIDs, and no UUIDs as secret tokens.
- Fixes to the old text: errata on the bit layout of RFC 4122, and more test vectors with real values.
If a library or API spec still says “RFC 4122 UUID”, it means the same format; versions 1 to 5 didn’t change.
Why v7 is the better database key
Most databases store primary keys in a B-tree, an index kept in sorted order. When keys arrive in increasing order, like an auto-increment integer, each new row goes at the end of the index, and the pages being written stay in memory. Version 4 UUIDs are random, so every insert lands somewhere different in the index. Pages split, the index grows larger than it needs to be, and once it no longer fits in memory, inserts start waiting on disk. RFC 9562 states it plainly: random UUIDs “have poor database-index locality”.
Version 7 fixes this by putting the time first. Its first 48 bits are milliseconds since 1 January 1970, so IDs created later sort later, and inserts behave much like an auto-increment column, while still being generated anywhere, without asking the database for the next number.
PostgreSQL 18 added a built-in uuidv7() function next to gen_random_uuid(). In older versions, or in other databases, generate the value in your application. The format is simple enough to write yourself:
function uuidv7() {
const bytes = crypto.getRandomValues(new Uint8Array(16))
const ms = BigInt(Date.now())
for (let i = 0; i < 6; i++) bytes[i] = Number((ms >> BigInt(40 - 8 * i)) & 0xffn)
bytes[6] = (bytes[6] & 0x0f) | 0x70 // version 7
bytes[8] = (bytes[8] & 0x3f) | 0x80 // variant
const hex = Array.from(bytes, (b) => b.toString(16).padStart(2, '0')).join('')
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`
}This version is ordered to the millisecond. Two IDs made in the same millisecond can come out in either order; RFC 9562 describes counters and sub-millisecond precision for when that matters, and PostgreSQL’s function uses sub-millisecond time.
Reading the time back is one line, because the first 12 hex digits are the timestamp:
const id = '019535d9-3df7-79fb-b466-fa907fa17f9e'
new Date(parseInt(id.slice(0, 8) + id.slice(9, 13), 16)).toISOString()
// '2025-02-24T02:46:24.503Z'That is also the catch. Anyone who sees a v7 UUID knows when the record was created, to the millisecond. For order numbers or user IDs shown in URLs, consider whether that leaks something, such as how fast you sign up customers. If it does, use v4 for the public ID. The timestamp converter reads the millisecond value if you want to check one by hand.
How likely is a collision?
A version 4 UUID has 122 random bits; the other six mark the version and variant. By the birthday paradox, the chance that any two IDs in a set match grows with the square of the count:
| UUIDs generated | Chance of any duplicate |
|---|---|
| 1 billion | About 1 in 1019 |
| 1 trillion | About 1 in 1013 |
| 1 quadrillion | About 1 in 10 million |
| 2.7 × 1018 | About 50% |
In practice, the risk isn’t the math; it’s the random number generator. Duplicate UUIDs in the wild come from weak or badly seeded generators, cloned virtual machines starting from the same state, or code that falls back to Math.random(). Use crypto.randomUUID() in browsers and Node, or your platform’s equivalent, and put a unique constraint on the column anyway.
Version 7 has fewer random bits per millisecond, but collisions can only happen between IDs made in the same millisecond, so in practice the odds stay negligible for any single application.
Name-based UUIDs: v5 for stable IDs
Sometimes you want the same input to give the same UUID every time: to deduplicate records imported from different systems, or to derive an ID from a URL without storing a mapping. Version 5 hashes a namespace UUID and a name with SHA-1 and keeps 122 bits of the result. RFC 9562 defines namespaces for DNS names, URLs, ISO OIDs and X.500 names, and you can create your own by generating one v4 UUID and reusing it.
SHA-1’s known collision attacks don’t matter much here: v5 is for identification, not security. But the result is predictable by design, so a v5 UUID is never a secret. The hashing, encryption and encoding guide explains what “broken” means for SHA-1.
What UUIDs are not for
RFC 9562 is blunt: UUIDs must not be used as security capabilities, identifiers whose mere possession grants access. A password reset link, an unlisted document or an API key needs a token that is unguessable by design, and the standard allows implementations whose UUIDs are not. Use 128 or more bits directly from a cryptographic random generator, encoded as Base64url or hex, and give it an expiry.
UUIDs also aren’t compact. Stored as text they take 36 characters; most databases have a native 16-byte UUID type, so use it rather than a varchar column. For more on timestamps stored in too few bits, see the year 2038 problem.
Frequently asked questions
How long is a UUID?
A UUID is 128 bits, which is 16 bytes. Written out in the standard form it is 36 characters: 32 hexadecimal digits in groups of 8, 4, 4, 4 and 12, joined by four hyphens. Without hyphens it is 32 characters, and in Base64 it is 22 characters (24 with padding).
What is RFC 9562?
RFC 9562 is the current UUID standard, published by the IETF in May 2024. It replaces RFC 4122 from 2005, keeps versions 1 to 5 compatible, adds versions 6, 7 and 8 and the all-ones Max UUID, and updates the security advice, for example that MAC addresses should no longer be used in UUIDs.
Which UUID version should I use?
For new database keys, version 7: it is random enough to be unique without coordination and sorts by creation time, which keeps indexes compact. Use version 4 when the ID must not reveal when it was created, and version 5 when the same name must always give the same ID.
Is UUID v7 better than v4?
For primary keys, usually yes. RFC 9562 notes that random UUIDs like v4 have poor database index locality, while v7 values arrive in roughly increasing order. The trade-off is that a v7 UUID reveals its creation time to the millisecond.
Can I extract the date from a UUID?
From version 7, yes: the first 12 hex digits are a Unix timestamp in milliseconds. Versions 1 and 6 also contain a timestamp, counted in 100-nanosecond steps since 15 October 1582. Versions 4, 5 and 8 contain no standard timestamp.
Is a UUID the same as a GUID?
Yes. GUID is Microsoft’s name for the same 128-bit identifier. One difference to watch for: Microsoft’s COM saves GUIDs in little-endian byte order, so the raw bytes of a GUID can differ from the bytes of the same UUID written by other systems, even when the text form is identical.
Are UUIDs safe to use as secret tokens?
No. RFC 9562 says UUIDs must not be used as security capabilities, identifiers whose mere possession grants access, and that implementations should not assume UUIDs are hard to guess. For password reset links or API keys, use at least 128 bits straight from a cryptographic random generator.