What Is a UUID?
A UUID is a 128-bit value written as 32 hexadecimal digits in five groups, such as 3f2504e0-4f89-11d3-9a0c-0305e82c3301. It is designed to be unique without a central authority deciding the numbers.
The format
Thirty-two hexadecimal digits in the pattern 8-4-4-4-12, separated by hyphens. That is 128 bits, or 16 bytes.
The hyphens are cosmetic. Systems that store UUIDs often strip them, and both forms appear in databases and APIs.
Generate version 4 UUIDs in the browser.
Open the UUID Generator →What the digits mean
The first digit of the third group is the version. A 4 there means a version 4 UUID, which is random. A 7 means version 7, which is time-ordered.
The first digit of the fourth group is the variant, which identifies the layout. In practice it is 8, 9, a or b for standard UUIDs, which is why that position looks constrained.
Version 4: random
122 of the 128 bits are random, with 4 bits for the version and 2 for the variant. The chance of a collision is negligible for any realistic number of identifiers.
It needs a good random source. A weak random number generator undermines the whole guarantee, which is why the platform generator should be used rather than a hand-rolled one.
Version 7: time-ordered
Version 7 puts a millisecond timestamp first, followed by randomness. That makes the identifiers sort roughly in creation order, which is much friendlier to database indexes than random values.
For a new system with high insert volume, version 7 often performs better than version 4 because inserts land at the end of the index rather than scattering across it.
Other versions
| Version | Based on | Notes |
|---|---|---|
| 1 | Timestamp and MAC address | Can leak hardware identifiers |
| 3 | MD5 of a name | Deterministic, so the same name gives the same UUID |
| 4 | Random | The common default |
| 5 | SHA-1 of a name | Deterministic, better hash than v3 |
| 7 | Timestamp plus random | Sortable, good for databases |
Versions 3 and 5 are for the case where the same input should always produce the same identifier, such as a namespace plus a name.
UUID versus auto-increment
An integer key is smaller, sorts naturally and is easy to read. It also requires the database to allocate it, which complicates distributed writes and reveals how many records exist.
A UUID can be generated anywhere without coordination, which is its main advantage. The cost is 16 bytes instead of 4 or 8, and a less friendly value in URLs.
Storage advice
Store as a native UUID type where the database has one. Storing as text uses more space and compares more slowly.
If you must store as text, drop the hyphens and use a fixed-width column. Mixed formats in one column make lookups unreliable.
Common mistakes
- Using a weak random source to generate version 4 values.
- Assuming UUIDs cannot collide at all.
- Using version 4 as a primary key in a very high-insert table without checking index performance.
- Storing UUIDs as variable-length text.
- Treating a UUID as a secret or a token.
Are UUIDs secret?
No. A UUID is an identifier, not a credential. If a resource should only be reachable by its owner, check authorisation rather than relying on the identifier being unguessable.
Version 1 UUIDs are partly predictable because they contain a timestamp, which is one reason not to use them as access tokens.
Generating them
Every language has a built-in generator. In Python it is uuid.uuid4(). In JavaScript, crypto.randomUUID() in modern runtimes. In the browser, use the platform function rather than Math.random.
For version 7, check whether your platform supports it; support arrived later than for version 4 and is still uneven.
UUIDs in URLs
A UUID in a URL is long and unreadable. For public pages, a slug is better; for internal identifiers, a UUID is fine and avoids revealing how many records exist.
If a URL contains a UUID, treat it as an identifier that must still be checked against permissions, not as a secret that protects the resource.
Frequently asked questions
How many characters is a UUID?
36 with hyphens, 32 without.
Can two UUIDs be the same?
In theory yes. In practice the probability is negligible for version 4 values from a good random source.
Which version should I use?
Version 4 for most cases, version 7 when you want sortable identifiers in a database.