The short answer
Use UUID v7 for new database keys and UUID v4 when the identifier must reveal nothing. Both are 128-bit identifiers in the same 36-character form, both are defined by RFC 9562 (2024), and both are practically unique. The difference is the first 48 bits: in v7 they are the creation time in Unix milliseconds, so new ids sort after old ones; in v4 everything except 6 fixed bits is random.
How the bits are laid out
| UUID v4 | UUID v7 | |
|---|---|---|
| Example | f47ac10b-58cc-4372-a567-0e02b2c3d479 | 018bcfe5-6800-7a1c-9f2b-3c4d5e6f7a8b |
| First 48 bits | random | Unix time in ms (018bcfe56800 = 2023-11-14 22:13:20 UTC) |
| Version digit | 4 | 7 |
| Random bits | 122 | 74 (62 of them after the variant) |
| Sort order | none | by creation time |
| Leaks | nothing | when the record was created |
The 13th hexadecimal digit is the version, and the 17th (8, 9, a or b) marks the RFC variant. Generate either kind with the UUID v7 generator or the UUID v4 generator.
Why v7 is better for databases
Most databases store primary keys in a B-tree index. Random v4 keys land on random pages of that index, so every insert touches a different page, the cache stops helping and the index fragments. Time-ordered v7 keys are appended near the end, like an auto-increment number, which keeps inserts fast and the index compact, while still letting any service create ids without asking the database. PostgreSQL 18 added a built-in uuidv7() function; in other systems a library or this site's generator does the job.
When v4 is still the right choice
- Public identifiers whose creation time should stay private (invite links, password-reset ids). For secrets, a random token from the token generator is even better.
- Systems that already use v4 everywhere: mixing versions works, but gives no ordering benefit.
- Tables small enough that index locality does not matter.
What about ULID?
A ULID has the same idea as v7, a 48-bit millisecond timestamp followed by 80 random bits, written in 26 characters of Crockford Base32. It predates v7; if your stack has a native UUID type, v7 fits it without conversion. To read the timestamp of a v7 or a ULID, convert the first 48 bits with the date converter, as explained in Unix timestamps.
FAQ
Can two UUID v4 collide?
In theory yes, in practice no: with 122 random bits you would need about 2.7 × 10^18 ids for a 50% chance of a single collision. A broken random generator is a far bigger risk than the math.
Does UUID v7 reveal anything?
Yes, the creation time to the millisecond. Avoid it where that matters, such as ids of medical records shown to users.
Are UUID v7 ids strictly increasing?
They increase with time, and RFC 9562 allows using the 12 bits after the version as a counter to keep order inside the same millisecond. Ids made on different machines in the same millisecond have no guaranteed order.
Can I store a UUID in 16 bytes instead of 36 characters?
Yes. Use the database's native type (uuid in PostgreSQL, BINARY(16) in MySQL); the text form is only for display.