UUID v4 vs UUID v7: which one should you use?
Both are 128-bit identifiers defined by RFC 9562 and printed as the familiar 36-character string. The difference is where the bits come from, and that decides how they behave as keys.
The bit layout
A UUID is 16 bytes. Six bits are fixed: four carry the version, two carry the variant. The remaining 122 are free.
- v4 spends all 122 bits on randomness.
- v7 spends 48 bits on a Unix timestamp in milliseconds and the other 74 on randomness.
What that changes
| v4 | v7 | |
|---|---|---|
| Random bits | 122 | 74 |
| Sorts by creation time | No | Across milliseconds yes, within one millisecond no |
| Reveals when it was created | No | Yes, to the millisecond |
| Insert behaviour in a B-tree | Scattered — every insert can touch a different page | Appends near the end |
| Typical use | Opaque identifiers, tokens, request IDs | Primary keys, log and event IDs |
The index behaviour is the reason v7 exists. A random primary key scatters inserts across the whole index, so each insert touches more pages than an increasing key would. A v7 key is close to a timestamp, so new rows land near the end of the index.
Reading a v7
The timestamp is the first 48 bits, which is the first 12 hexadecimal characters of the string:
01a149db-ad80-71b1-a1bd-a450971ce86f
~~~~~~~~~~~~
01a149dbad80 = 1792240496000 ms
= 2026-10-17 12:34:56 UTC
The third group starts with the version, 7. The fourth group
starts with the variant, one of 8, 9,
a or b. Everything else is random.
The ordering caveat
RFC 9562 allows a monotonic counter in the random field, but it is optional. A generator that fills those bits randomly — including the one on this site — gives no defined order to two values created in the same millisecond. If you need strict ordering at that resolution, add a sequence column or use a design with an explicit counter.
Uniqueness is statistical
Nothing guarantees that a UUID has never been generated before. For v4, the chance of a collision after n values is about n2 / 2123. A billion values gives roughly 9 × 10-20. That is far below the chance of a hardware failure corrupting the row, but it is a probability, not a proof — and it assumes a sound random source.
Privacy
A v7 embeds the moment it was created. If the value is exposed — in a URL, a log line, a screenshot, a support ticket — it also exposes when the record was made, and by extension the rate at which records are created. A v4 reveals none of that. For identifiers that leave your system, that is a reason to prefer v4.
Which to choose
- Primary key of an insert-heavy table — v7.
- Public identifier, session token, correlation ID — v4.
- Identifier that must not reveal time — v4.
- An existing v4 system — keep it. Switching buys index locality only when the UUID is the clustered primary key.
Generate either with the UUID generator. For timestamps in other units, see Epoch Time.