What Is a UUID? Structure, Versions and Collision Odds
What is a UUID? A 128-bit identifier written as 36 characters that can be generated anywhere without coordination. Learn its structure, versions and odds.
Updated
A UUID (universally unique identifier) is a 128-bit number used to label something, such as a database row, a file or a request, so that the label will not clash with any other, even one created on a different machine at the same moment. It is written as 32 hexadecimal digits in five hyphenated groups, for example f47ac10b-58cc-4372-a567-0e02b2c3d479. No central authority hands them out; any computer can create one on its own.
Why UUIDs exist
The traditional identifier is a counter: 1, 2, 3. A counter needs one place that knows the last number issued, which works for a single database and gets awkward everywhere else. Two servers, an offline mobile app, or two companies merging their data will all produce a second record numbered 1001.
A UUID removes the coordination. Each generator picks from a space so large that independent choices do not collide in practice. That lets a client create an ID before the server has seen the record, lets databases merge without renumbering, and avoids revealing how many records exist, which sequential IDs do.
Structure
The text form has 36 characters laid out as 8-4-4-4-12:
f47ac10b-58cc-4372-a567-0e02b2c3d479
Two positions carry meaning in every UUID:
- The version is the first digit of the third group. In the example it is
4, so this is a version 4 UUID. - The variant is the first digit of the fourth group. For the standard variant it is always
8,9,aorb. The example hasa.
The remaining bits depend on the version: random data, a timestamp, a hash, or a mixture. The format is defined by RFC 9562, published in 2024, which replaced the earlier RFC 4122 from 2005.
Hex digits are case insensitive. The RFC recommends writing lowercase and accepting either. The hyphens are part of the standard text form but carry no information, so many systems store the 32 digits alone or the raw 16 bytes.
UUID vs GUID
A GUID is a UUID. Microsoft adopted the design under the name globally unique identifier, and the term stuck across Windows, COM, .NET and SQL Server. Guid.NewGuid() in .NET returns a version 4 UUID.
The differences are presentational:
- GUIDs are often shown in uppercase, and in the Windows registry and some APIs wrapped in braces:
{F47AC10B-58CC-4372-A567-0E02B2C3D479}. - When a GUID is stored as 16 raw bytes, Microsoft's layout writes the first three groups in little-endian order. Reading those bytes on a system that expects the RFC's big-endian order shows the first three groups with their bytes reversed. If an ID looks scrambled only in its first half after crossing systems, this is the cause.
As text, a UUID and a GUID are interchangeable.
Versions at a glance
| Version | Built from | Typical use |
|---|---|---|
| 1 | Timestamp plus a node ID, originally the MAC address | Older systems. Reveals time and possibly the machine |
| 2 | DCE security variant of version 1 | Rare |
| 3 | MD5 hash of a namespace and a name | Deterministic IDs. Version 5 is preferred |
| 4 | 122 random bits | The general-purpose default |
| 5 | SHA-1 hash of a namespace and a name | Deterministic IDs: the same input always gives the same UUID |
| 6 | Version 1 fields reordered so values sort by time | Migration path for version 1 users |
| 7 | Unix timestamp in milliseconds plus random bits | Database keys that should sort by creation time |
| 8 | Custom layout defined by the application | Experimental or vendor-specific schemes |
RFC 9562 also defines two special values: the nil UUID, which is all zeros, and the max UUID, which is all f digits. Both are used as placeholders or sentinels, never as real IDs.
Two versions cover most needs. Version 4 is pure randomness and is what you get from crypto.randomUUID() in browsers and Node.js, uuid.uuid4() in Python and gen_random_uuid() in PostgreSQL. Version 7 puts a timestamp first so new IDs sort in creation order, which databases prefer. The trade-offs between them are covered in UUID v4 vs v7.
Versions 3 and 5 are different in kind: they are not random at all. They hash a namespace UUID together with a name, so the URL https://example.com/a always maps to the same UUID. That is useful for giving stable IDs to things that already have a natural name. For the hash functions behind them, see MD5 vs SHA-256.
Collision odds
A version 4 UUID has 122 random bits; the other 6 are fixed for the version and variant. That gives 2 to the power 122 possible values, about 5.3 undecillion, a number with 37 digits.
The relevant question is not whether one specific UUID is repeated, but whether any two in a set match, which is the birthday problem. The numbers are still enormous:
- Generate about 103 trillion version 4 UUIDs and the chance that any two match is roughly one in a billion.
- To reach a 50 percent chance of one collision you would need about 2.7 quintillion of them. At a billion UUIDs per second, that takes around 86 years.
For any realistic application, the count of IDs you will ever create is nowhere near these figures. Real-world duplicates come from other causes: a random number generator seeded identically on cloned virtual machines, code that copies a record along with its ID, or a column that truncates the value.
UUIDs are unique, not secret. A version 4 UUID from a cryptographic generator is hard to guess, but other versions embed timestamps or hashes of known inputs. Do not treat an ID as an access token unless you know exactly how it was generated.
Storing and using UUIDs
- Use a native type where one exists. PostgreSQL's
uuidtype and aBINARY(16)column take 16 bytes; the same value as text takes 36. - Normalize case and format at the boundary. Compare
F47AC10B...withf47ac10b...as strings and they differ, although they are the same UUID. - Validate the shape with a pattern such as
^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$in case-insensitive mode. The regex cheat sheet explains each piece.
Generating UUIDs
Most platforms have a built-in generator: crypto.randomUUID() in JavaScript, uuid.uuid4() in Python, uuidgen on macOS and Linux, New-Guid in PowerShell, UUID.randomUUID() in Java.
When you need a handful for test fixtures, seed data or a config file, the UUID Generator produces version 4 UUIDs in batches of 1 to 100, with options for uppercase and for removing the hyphens, which gives the 32-character form some GUID fields expect. It generates version 4 only. For IDs that double as security tokens, generate them in your application with the platform's cryptographic API instead.
Try it
Open the UUID Generator, choose how many you need, and copy them out. Look at the first digit of the third group in each one: it is always 4, and the first digit of the fourth group is always 8, 9, a or b, exactly as the structure above describes.