What a UUID is
A UUID (universally unique identifier) is a 128-bit value used to name things without a central registry. Any system can make one and be confident no other system made the same one. It is usually written as 32 hex digits in five groups: 8-4-4-4-12.
UUIDs are defined in RFC 9562, published in 2024, which replaced RFC 4122. The new RFC added versions 6, 7 and 8 and gave clearer guidance on which version to use.
This tool generates the two versions most systems need today: version 4, which is random, and version 7, which starts with a timestamp so that new ids sort after old ones.
Anatomy of a UUID
Two fields are fixed in every RFC 9562 UUID. The version sits in the first hex digit of the third group. The variant sits in the top bits of the fourth group; for these UUIDs the first digit of that group is always 8, 9, a or b.
xxxxxxxx-xxxx-4xxx-Nxxx-xxxxxxxxxxxx version 4
xxxxxxxx-xxxx-7xxx-Nxxx-xxxxxxxxxxxx version 7
N is 8, 9, a or b (the RFC 9562 variant)Version 4: random
All bits except the 6 fixed version and variant bits are random: 122 random bits. Nothing about the UUID reveals when or where it was made.
Version 7: time-ordered
| Bits | Field | Content |
|---|---|---|
| 48 | unix_ts_ms | Milliseconds since 1970-01-01 UTC, big-endian |
| 4 | ver | The version, 7 |
| 12 | rand_a | Random, or a counter for ordering within a millisecond |
| 2 | var | The variant, binary 10 |
| 62 | rand_b | Random |
Because the timestamp comes first, sorting version 7 UUIDs as strings or bytes sorts them by creation time. The first 12 hex digits are the time, so you can read when an id was made.
Why version 7 matters for databases
Random version 4 UUIDs land at random places in a B-tree index. Every insert touches a different page, which spreads writes, fills the cache with pages that are rarely reused and fragments the index as it grows.
Version 7 UUIDs arrive in roughly increasing order, so inserts go to the end of the index, like an auto-incrementing number. You keep the benefits of UUIDs, such as making ids on the client and merging data from several systems, without the indexing cost.
The trade-off is privacy. A version 7 UUID tells anyone who sees it when the record was created, to the millisecond. Use version 4 for ids that appear in URLs or tokens where that matters.
How this tool generates UUIDs
- Random bits come from
crypto.getRandomValues, the browser’s cryptographically secure generator. - Version 4 sets the version and variant bits on 16 random bytes, as RFC 9562 section 5.4 describes.
- Version 7 takes the current Unix time in milliseconds and 74 random bits.
- When several version 7 UUIDs fall in the same millisecond, each one adds a random step to the previous random bits instead of drawing fresh ones. This is method 2 in RFC 9562 section 6.2, and keeps a batch in creation order.
- If the step would overflow the random field, the timestamp moves forward by one millisecond.
Formats: lowercase with hyphens is the canonical form. Uppercase, no hyphens, braces and the urn:uuid: prefix are offered for systems that expect them. All represent the same value.
How to use the generator
- Choose version 4 for random ids or version 7 for time-ordered ids.
- Choose how many: one, five, ten or fifty.
- Choose a format. Use lowercase with hyphens unless a system requires otherwise.
- Copy one UUID, or all of them at once.
- For version 7, check the time shown under the list; it is decoded from the first UUID.
UUIDs in identity systems
Identity systems use UUIDs widely, mostly as stable identifiers that never change when names do.
Keycloak object ids
Keycloak gives users, clients, roles, groups and many other objects a random UUID as their internal id. These ids appear in admin REST API paths, such as /admin/realms/acme/users/<id>, and in realm exports.
Note the difference between a client’s internal id and its client id. The client id is the name you choose, such as web-app, used in OAuth requests. The internal id is a UUID, used in admin API paths.
The sub claim
Keycloak puts the user’s id in the sub claim of access and ID tokens. Applications should link their own records to sub and the issuer, not to the username or email, because those can change.
Token and request ids
The jti claim gives a token a unique id, and APIs often use UUIDs as request or correlation ids in logs. Version 4 suits these, since a token id should not reveal more than it must.
Ids you import
When migrating users into Keycloak, you can keep their existing ids by setting each user’s id in a realm import file, which preserves the sub your applications already store. Generate new ids only for records that never had one.
Common mistakes
- Using a UUID as a secret. UUIDs are identifiers. They are logged, shown in URLs and shared freely. A password reset token or API key needs a value made for secrecy, such as one from the client secret generator.
- Generating UUIDs with a non-cryptographic random source. Some older libraries used weak generators, which made collisions and guessing far more likely.
- Storing UUIDs as 36-character strings in large tables. A native UUID type or 16 bytes of binary is less than half the size.
- Comparing UUIDs case-sensitively.
A1B2…anda1b2…are the same UUID; RFC 9562 says to output lowercase and accept either case. - Assuming version 7 UUIDs from different machines are strictly ordered. Ordering holds within one generator; across machines it follows their clocks.
How unlikely is a collision?
With 122 random bits, version 4 collisions are not a practical concern. By the birthday bound, there is an even chance of a single collision only after about 2.7 × 10^18 UUIDs.
Version 7 has fewer random bits per millisecond, 74, but the timestamp separates ids made at different times. Collisions would need two generators to pick the same 74 bits in the same millisecond.
Real collisions come from bugs: a cloned virtual machine reusing a random state, a fixed seed, or code that copies an id instead of making a new one.
Best practices
- Use version 7 for database primary keys and version 4 for public-facing ids.
- Store UUIDs in a native type or as 16 bytes.
- Output lowercase with hyphens; accept any case when reading.
- Never use a UUID as a password, token or key.
- Use your language’s standard UUID library or the browser’s
crypto.randomUUID()for version 4 in code.
Generating UUIDs in code
For one-off ids this page is quick. In software, use your platform’s built-in generator rather than writing your own.
// Browsers and Node.js: version 4
crypto.randomUUID()
# Python: version 4
python3 -c "import uuid; print(uuid.uuid4())"
# Command line on macOS and most Linux systems: version 4
uuidgenSupport for version 7 is newer and varies by language and database version, so check your platform’s documentation. If you add a library for it, choose one that follows RFC 9562 and uses a cryptographic random source.