UUIDs are the default way to give something an ID without asking a central server for the next number. Databases, message queues, file formats, APIs and Bluetooth all use them. For two decades the standard was RFC 4122, and in practice "UUID" meant a random version 4. In May 2024 the IETF replaced RFC 4122 with RFC 9562, which standardizes a time-ordered version 7 designed for database keys. This guide explains how UUIDs are laid out, what each version is for, how small the collision risk really is, and when to choose v4 or v7.
- A UUID is 128 bits written as 32 hex digits in the pattern 8-4-4-4-12. One digit gives the version, and part of another gives the variant.
- RFC 9562 (2024) obsoletes RFC 4122. It adds versions 6, 7 and 8 plus the Nil and Max UUIDs.
- v4 has 122 random bits. You'd need about 103 trillion of them for a one-in-a-billion chance of any duplicate.
- v7 starts with a 48-bit Unix millisecond timestamp, so IDs sort by creation time and keep B-tree indexes compact.
- Store UUIDs in a native 16-byte type, not a 36-character string, and don't use them as secrets.
Anatomy of a UUID
Take the version 7 example from RFC 9562 Appendix A:
017f22e2-79b0-7cc3-98c4-dc0c0c07398f
^ ^
| variant: 9 = binary 1001, top bits "10"
version: 7The 13th hex digit (the first digit of the third group) is the version. The top bits of the 17th digit are the variant. For every UUID defined by RFC 9562 the variant bits are 10, so that digit is always 8, 9, a or b. Other variant patterns are reserved for old NCS identifiers and Microsoft's legacy formats. The remaining bits depend on the version. The standard text form uses lowercase hex; parsers must accept uppercase too. Microsoft's GUIDs are the same 128-bit values, often displayed as {0192F0A4-…} with braces and capitals.
The Versions in RFC 9562
| Version | Contents | Use today |
|---|---|---|
| 1 | 60-bit timestamp (100-ns ticks since 1582-10-15), clock sequence, node (often a MAC address) | Legacy; leaks the host and doesn't sort |
| 2 | DCE Security, embeds a POSIX UID/GID | Rare |
| 3 | MD5 hash of namespace + name | Deterministic IDs; prefer v5 |
| 4 | 122 random bits | General-purpose random IDs |
| 5 | SHA-1 hash of namespace + name | Deterministic IDs from names |
| 6 | v1's timestamp reordered most-significant first | Migrating v1 systems to sortable IDs |
| 7 | 48-bit Unix ms timestamp + 74 random or counter bits | Database keys, event IDs |
| 8 | Vendor-defined (only version and variant are fixed) | Custom layouts |
The RFC also names two special values. The Nil UUID is all zeros, 00000000-0000-0000-0000-000000000000, and the Max UUID is all ones, ffffffff-ffff-ffff-ffff-ffffffffffff. Versions 3 and 5 always produce the same UUID for the same namespace and name, which is useful for stable IDs derived from URLs or DNS names. Their use of MD5 and SHA-1 is not a security feature, which is why our hashing guide's warnings about those algorithms don't make v5 unsafe for this job.
Version 4: Random, and How Random Is Enough
A v4 UUID fixes 6 bits (4 version, 2 variant) and fills the other 122 from a random source, giving 2122 ≈ 5.3 × 1036 possibilities. The chance of at least one duplicate among n UUIDs follows the birthday approximation:
p(n) ≈ 1 − exp(−n² / (2 × 2^122))
n = 1,000,000,000 (one billion) → p ≈ 9.4 × 10^-20
n ≈ 1.03 × 10^14 (103 trillion) → p ≈ 1 in a billion
n ≈ 2.71 × 10^18 → p ≈ 50%At a billion UUIDs per second, the 50% point would take about 86 years of continuous generation. The real-world risk lies in the random number generator, not the math. Seeded pseudo-random functions such as Math.random(), virtual machines cloned with the same entropy state, and hand-rolled generators have all produced genuine duplicates. Use the platform's cryptographic generator: crypto.randomUUID() in browsers and Node.js, uuid.uuid4() in Python, UUID.randomUUID() in Java, or gen_random_uuid() in PostgreSQL.
Version 7: Time-Ordered IDs
Version 7 puts a 48-bit big-endian Unix timestamp in milliseconds at the front, which lasts until the year 10889. Then come the 4-bit version, 12 bits called rand_a, the 2-bit variant, and 62 bits called rand_b. Decoding the RFC example:
017f22e2 79b0 → 0x017F22E279B0 = 1,645,557,742,000 ms
= 2022-02-22 19:22:22.000 UTC
7 → version 7
cc3 → rand_a (12 bits)
9 8c4 dc0c0c07398f → variant "10" + rand_b (62 bits)Paste that UUID into our UUID generator's inspector and it reports the same timestamp. Because the most significant bits are the time, v7 UUIDs sort in creation order as raw bytes and as lowercase strings.
Within a single millisecond, ordering depends on the generator. RFC 9562 Section 6.2 describes optional methods for monotonic ordering. One is to treat rand_a, or more bits, as a counter that starts at a random value each millisecond and increments for each new ID. When the counter overflows, the generator can either wait for the next millisecond or advance the timestamp slightly. Our generator uses a 12-bit counter in rand_a and keeps 62 random bits in rand_b, so a batch of 1,000 v7 UUIDs is always strictly ascending. Different generators can still produce IDs in the same millisecond. Their order relative to each other is arbitrary, but a collision would still require matching 62 or more random bits.
Why v7 Beats v4 as a Database Key
Primary keys in PostgreSQL, MySQL (InnoDB), SQL Server and most other relational databases live in B-tree indexes, and InnoDB stores the whole table in primary-key order. With random v4 keys:
- Each insert lands on a random leaf page, so the database must keep the whole index in memory or read pages from disk constantly.
- Full pages split, leaving them about half empty. The index grows larger than it needs to be and fragments over time.
- Write-ahead logs grow, because more distinct pages change for the same number of rows.
Time-ordered v7 keys behave like an auto-increment integer: new rows append to the right-hand edge of the index, the hot pages stay cached, and range scans by creation time come almost free. You keep what made UUIDs attractive in the first place. Any service, or even the client, can create an ID before the row exists, and data can be merged across shards and databases without renumbering.
A few practical notes:
- Use a native 16-byte type. PostgreSQL's
uuidtype stores 16 bytes, and PostgreSQL 18 added a built-inuuidv7()function. In MySQL, useBINARY(16). Storing the 36-character text form asCHAR(36)more than doubles the key size, and every secondary index that carries the key grows with it. - Mind the byte order. SQL Server's
uniqueidentifiersorts by its own byte grouping, not left to right, so v7 values don't sort chronologically there. - Accept the timestamp leak. Anyone who sees a v7 ID knows when it was created, to the millisecond. If creation times are sensitive, for example in public URLs for user accounts, use v4 for the public ID or keep a separate internal key.
Formats You'll Meet
| Form | Example | Where |
|---|---|---|
| Canonical | 017f22e2-79b0-7cc3-98c4-dc0c0c07398f | RFC 9562, most APIs |
| Uppercase + braces | {017F22E2-79B0-7CC3-98C4-DC0C0C07398F} | Windows registry, .NET, COM |
| Compact | 017f22e279b07cc398c4dc0c0c07398f | Filenames, some databases |
| URN | urn:uuid:017f22e2-79b0-7cc3-98c4-dc0c0c07398f | XML, RDF, standards documents |
All four are the same 128-bit value, so normalize to the canonical lowercase form before comparing or storing. The UUID generator can output any of these formats and its inspector accepts all of them.
Common Mistakes
- Treating UUIDs as secrets. RFC 9562 warns against assuming UUIDs are hard to guess. For password-reset links, API keys or session tokens, use a dedicated random token of at least 128 bits from a cryptographic generator, with an expiry.
- Inventing your own layout. A "UUID" with random version and variant digits fails strict validators. If you need a custom layout, use version 8, which exists for that purpose.
- Using v1 for new systems. It can expose a MAC address, and its scrambled timestamp order doesn't sort. Choose v7, or v6 if you must stay compatible with v1's 100-nanosecond clock.
- Comparing strings case-sensitively.
ABC…andabc…are the same UUID.
Frequently Asked Questions
Is RFC 4122 still valid?
No. RFC 9562 obsoletes it. Every UUID that was valid under RFC 4122 is still valid; the new RFC clarifies the old versions and adds versions 6, 7 and 8.
Are v7 UUIDs less unique than v4?
They have fewer random bits (up to 74 instead of 122), but those bits only need to be unique among IDs created in the same millisecond. Collisions remain practically impossible with a proper random generator.
Can I extract the creation time from any UUID?
Only from versions 1, 6 and 7. Version 4 contains no time. Our UUID inspector decodes the timestamp when one is present.
Should I switch existing v4 keys to v7?
Usually only for new tables, or when index size and insert performance have become a real problem. Both versions can live in the same uuid column, so new rows can use v7 without migrating old ones.
Do UUIDs work in URLs?
Yes. Hex digits and hyphens need no URL encoding, so UUIDs are safe in paths and query strings as-is.
Sources: RFC 9562 (UUIDs, 2024); RFC 4122 (obsoleted); MDN: crypto.randomUUID(); PostgreSQL: UUID functions; PostgreSQL: UUID type.