UUID versions compared (v1, v4, v7)
Compare UUID versions 1, 3, 4, 5, 6 and 7 from RFC 9562, with decoding and generation examples and advice on which version to choose.
By the tools4free.dev team · Last reviewed 4 December 2026
A UUID is a 128-bit identifier written as 32 hexadecimal digits in five groups, like 61e69eae-21e4-419e-8328-fc863c515479. For most new systems, use version 4 (random) when you only need uniqueness, and version 7 (time-ordered random) when the identifier will be a database key or you want records to sort by creation time. The current specification is RFC 9562, which replaced RFC 4122 in 2024.
Anatomy of a UUID
The text form is 8-4-4-4-12 hex digits. Two small fields tell you what you are looking at:
- The version is the first hex digit of the third group. In
61e69eae-21e4-419e-8328-fc863c515479it is 4. - The variant is the first hex digit of the fourth group. For standard RFC 9562 UUIDs it is
8,9,aorb. In the example it is8.
Those fixed bits use 6 of the 128, which leaves 122 bits for the real content. Two special values also exist: the nil UUID 00000000-0000-0000-0000-000000000000 and the max UUID ffffffff-ffff-ffff-ffff-ffffffffffff.
The versions compared
| Version | Source of the value | Sortable by time | Typical use |
|---|---|---|---|
| 1 | 60-bit timestamp, clock sequence, node ID (often a MAC address) | Not in text order | Legacy systems |
| 3 | MD5 hash of a namespace and a name | No | Legacy stable IDs |
| 4 | 122 random bits | No | General purpose |
| 5 | SHA-1 hash of a namespace and a name | No | Stable IDs derived from names |
| 6 | Version 1 timestamp with the fields reordered | Yes | Migrating v1 data to sortable keys |
| 7 | 48-bit Unix millisecond timestamp plus 74 random bits | Yes | Database primary keys |
| 8 | Application-defined | Up to you | Custom schemes |
Version 1 and its privacy problem
Version 1 stores the number of 100-nanosecond intervals since 15 October 1582 (the start of the Gregorian calendar) plus the machine's node ID. The node ID was traditionally the network card's MAC address, so a v1 UUID can reveal which machine created it and when. The timestamp is also split so that the low bits come first, which means ordinary string or byte sorting does not order v1 values by time. RFC 9562 allows a random node ID instead of a MAC address, and version 6 fixes the ordering by putting the most significant timestamp bits first.
Version 4: random
Version 4 is 122 random bits plus the six fixed bits. It reveals nothing about the creator, is trivial to generate and needs no coordination between machines. Most platforms have it built in; in Node.js it is crypto.randomUUID() and in browsers it is crypto.randomUUID() in secure contexts.
How likely is a collision? Using the birthday approximation for 122 random bits, you would need to generate about 2.7 × 1018 UUIDs before the chance of any duplicate reaches 50%. If a system makes one billion a second, that is roughly 86 years. With a proper random source, duplicates are not a practical concern. The real risk is a poor random generator, so use the platform's cryptographically secure one and avoid Math.random().
Versions 3 and 5: names to UUIDs
These hash a namespace UUID and a name, so the same inputs always give the same UUID. Version 5 uses SHA-1 and is preferred over version 3 (MD5). RFC 9562 defines namespaces for DNS names, URLs, ISO OIDs and X.500 names. For example, the version 5 UUID for the DNS name example.com is always cfbff0d1-9375-5685-968c-48ce8b15ae17, and the version 3 UUID for the same input is 9073926b-929f-31c2-abc9-fad77ae3e8eb. They suit cases where independent systems must agree on an ID for the same thing without talking to each other. Do not use them for secrets, since anyone who knows the name can compute the ID.
Version 7: time-ordered
Version 7 puts a 48-bit Unix timestamp in milliseconds in the first 12 hex digits, then the version, then random bits. Because the timestamp is first, newer UUIDs sort after older ones as plain strings or bytes. This helps databases a great deal: a random v4 key inserts at a random position in a B-tree index, causing page splits and poor cache use on large tables, while a v7 key appends near the end like an auto-increment number.
Worked example 1: decoding a v7 UUID
Take 01a240ec-f200-78a9-b804-e4a9edb1adeb. The version digit in 78a9 is 7, and the variant digit in b804 is b. The first 12 hex digits are 01a240ecf200:
const hex = '01a240ec-f200-78a9-b804-e4a9edb1adeb'
.replace(/-/g, '').slice(0, 12);
const ms = parseInt(hex, 16); // 1796385600000
console.log(new Date(ms).toISOString()); // 2026-12-04T12:00:00.000Z
The creation time is recoverable from the identifier. That is useful for debugging and a privacy consideration: do not expose v7 IDs publicly if the creation time of a record is sensitive.
Worked example 2: generating a v7 UUID
Node's built-in crypto does not yet provide v7 in every release, but the layout is easy to build:
const crypto = require('crypto');
function uuidv7(now = Date.now()) {
const b = crypto.randomBytes(16);
const t = BigInt(now);
for (let i = 0; i < 6; i++) {
b[i] = Number((t >> BigInt(8 * (5 - i))) & 0xffn);
}
b[6] = (b[6] & 0x0f) | 0x70; // version 7
b[8] = (b[8] & 0x3f) | 0x80; // variant 10xx
const h = b.toString('hex');
return `${h.slice(0, 8)}-${h.slice(8, 12)}-${h.slice(12, 16)}-${h.slice(16, 20)}-${h.slice(20)}`;
}
This version is fine for illustration. UUIDs created within the same millisecond are ordered randomly, not by creation. RFC 9562 describes optional counters to keep them monotonic within one process, and mature libraries implement that.
Which one should you use?
- Public identifiers that should not leak information: version 4.
- Primary keys in a large relational table: version 7, or a database-native alternative.
- Same input must give the same ID: version 5.
- Existing v1 data that needs sorting: convert to version 6.
Common mistakes
- Treating a UUID as a secret. A UUID is an identifier. Do not rely on an unguessable URL instead of an authorisation check. For tokens, use a purpose-built random token.
- Using
Math.random()to build one. It is not a secure random source and can produce predictable or colliding values. - Storing them as 36-character strings. Where the database has a native UUID type, it stores 16 bytes instead of 36 characters, saving space and index size.
- Mixing case in comparisons. RFC 9562 says UUIDs should be output in lowercase, and input should be compared case-insensitively. Normalise before comparing strings.
- Assuming v4 is sortable. Sorting random IDs gives a random order. If you need creation order, add a timestamp column or use v7.
- Assuming v7 gives strict ordering across servers. Clocks differ between machines, so ordering is approximate across nodes.
Frequently asked questions
What is the difference between a UUID and a GUID?
None in practice. GUID is Microsoft's name for the same 128-bit format.
Can two UUIDs ever be the same?
In theory yes, in practice almost never for v4 and v7 with a good random source. Still, put a unique constraint on the column so a duplicate fails loudly.
Is v7 officially standard?
Yes. Versions 6, 7 and 8 were added in RFC 9562 in May 2024, which obsoletes RFC 4122.
Are UUIDs better than auto-increment IDs?
They differ. UUIDs can be generated anywhere without asking the database and do not reveal how many records exist. Integers are smaller and faster to index. Choose by your needs, not by habit.
Try it: generate v4 and v7 identifiers with the UUID generator.