How UUIDs work

A UUID, a universally unique identifier, is a 128-bit number that anyone can make on their own and still be practically sure that nobody else, anywhere, ever makes the same one. That is why databases, file systems, message queues and APIs use them as keys without asking a central registry. Here is how the 128 bits are laid out, what the versions mean, why version 7 is the new favourite for databases, and how unlikely a duplicate really is.

A UUID in its five coloured groups between faded rows of other IDs
A UUID in its five coloured groups between faded rows of other IDs

128 bits in 36 characters

A UUID is 16 bytes. Written out, it is 32 hexadecimal digits in five groups of 8, 4, 4, 4 and 12, joined by hyphens, 36 characters in all, such as 0192f3a4-5b6c-7d8e-9f01-23456789abcd. The hyphens carry no information and upper and lower case mean the same; the standard asks programs to write lowercase and to read either. GUID, globally unique identifier, is Microsoft's name for the same thing, often shown in uppercase and in braces. Other forms add the prefix urn:uuid: or drop the hyphens; they are all the same 128 bits.

The version and the variant

Two small fields say how to read the rest. The variant, the top bits of the 17th hex digit, tells which layout the UUID follows: for every UUID made today it is the bits 10, so that digit is 8, 9, a or b. The version, the 13th hex digit, tells how the other bits were made: 1 and 6 from a clock and a node, 3 and 5 from a name hashed with MD5 or SHA-1, 4 from random numbers, 7 from the Unix time and random numbers, and 8 for custom layouts. RFC 9562, published in 2024, defines them all and replaces RFC 4122 from 2005.

Version 4: random

A version 4 UUID is 122 random bits plus the four version bits and the two variant bits. It reveals nothing about who made it or when, and needs no clock, no counter and no coordination, which made it the most common UUID for two decades. Its weakness shows in databases: because each new key lands at a random place in the index, inserts touch pages all over the disk and the index fragments as the table grows.

Version 7: time first

A version 7 UUID puts the Unix time in milliseconds in its first 48 bits, enough until the year 10889, then the version, then 74 random bits around the variant. Sorted as text or as bytes, version 7 UUIDs fall in the order they were made, so new rows go to the end of an index like an auto-increment number, while keys can still be made on any machine without asking the database. Generators that make several in one millisecond can count up in the random bits to keep the order. The cost is that anyone can read the creation time from the UUID.

Versions 1 and 6, nil and max

Version 1, the original, combines a 60-bit count of 100-nanosecond intervals since 15 October 1582, the start of the Gregorian calendar, with a clock sequence and a 48-bit node, traditionally the computer's network card address. That leaked where a UUID came from: in 1999 the GUIDs in Word documents helped trace the author of the Melissa virus. Version 6 is version 1 with the time bits reordered, most significant first, so it sorts by time. The nil UUID has all 128 bits zero and the max UUID all bits one; both serve as markers, not as identifiers.

Will two UUIDs ever be the same?

For random UUIDs the birthday problem answers it. Among n values with b random bits, the chance of any two matching is about 1 − e^(−n²/2^(b+1)). With version 4's 122 bits, a billion UUIDs give odds of about 1 in 10¹⁹, and a 50% chance of a single duplicate needs about 2.7 × 10¹⁸ UUIDs, a billion a second for 86 years. In practice, duplicates come from broken random generators, cloned virtual machines or copied data, not from chance.

Frequently asked questions

Is a GUID the same as a UUID?

Yes. GUID is the name Microsoft uses, in Windows, COM, .NET and SQL Server, for the same 128-bit identifier. One difference only matters in binary: Microsoft stores the first three groups in little-endian byte order, so the same GUID can have different bytes on disk than on the network, while its text form is the same.

Should I use UUIDs or auto-increment numbers as database keys?

Auto-increment numbers are small and fast but are handed out by one database and reveal how many rows there are. UUIDs can be made anywhere, before the row is saved, and merge without clashes across servers. If you use UUIDs as primary keys, prefer version 7, which keeps the index in insertion order like a counter, and store them as 16 bytes rather than 36 characters where the database allows it.

Can a UUID be used as a password or a secret link?

Not safely. A version 4 UUID from a good random generator is hard to guess, but the standard does not require cryptographic randomness, many libraries do not promise it, and a version 7 UUID shows when it was made. For secret links, session tokens and passwords use a token made for the purpose, with at least 128 random bits from a cryptographic generator.

Why is the 13th character always 4 or 7?

Because it is the version: the generator writes 4 there for a random UUID and 7 for a time-ordered one. In the same way the 17th character is always 8, 9, a or b, because its first two bits are the variant 10. So a version 4 UUID really has only 122 random bits, not 128.

Ready to try it?

Open PictorX in your browser — no install, no credit card required to start.

Open the UUID Generator