UUID v4 is 122 random bits formatted as 36 characters. UUID v7 keeps 48 bits for a Unix millisecond timestamp and uses the rest for randomness. Both are valid RFC 9562 identifiers. The choice matters when the identifier becomes a primary key, a sort key or a public value.
The layout
| Version | Bits used for time | Random bits | Sorts by creation time |
|---|---|---|---|
| v4 | 0 | 122 | No |
| v7 | 48 (milliseconds) | 74 | Roughly, yes |
| v1 | 60 (100 ns since 1582) | 14 + node | Yes, but leaks MAC by default |
| ULID | 48 (milliseconds) | 80 | Yes, 26 characters |
A v4 UUID has no structure beyond the version and variant bits. Two IDs generated one millisecond apart are unrelated in lexicographic order. A v7 UUID starts with the timestamp, so a list of v7 values sorted as strings is also sorted by creation time, at least to the millisecond.
Why time ordering helps databases
B-tree indexes store keys in order. Inserting random keys spreads writes across the tree, causing page splits and cache misses. Inserting increasing keys appends to the rightmost page, which is cheaper. This is the same reason auto-increment integers are fast, without the predictability of a sequence.
The benefit is workload-dependent. For a small table or an in-memory store, the difference is negligible. For a high-write table with a UUID primary key, v7 can reduce write amplification noticeably. Benchmark with your storage engine; do not assume.
What v7 leaks
A v7 UUID reveals the approximate creation time to anyone who can read it. That may be fine for an order ID or a log correlation ID. It may not be fine for a public identifier where creation time is sensitive, such as an account ID that could reveal when a user signed up. Randomness in the low bits prevents guessing the exact value, but the timestamp is readable.
v4 leaks nothing. If the identifier is public and creation time is confidential, v4 is the safer default.
Monotonicity within a millisecond
Two v7 UUIDs generated in the same millisecond share the timestamp prefix. Their order depends on the random tail. If you need strict monotonicity within a millisecond, the generator must add a counter or use the “monotonic random” method from RFC 9562. Most simple generators do not.
This matters for event ordering only if you rely on the identifier as a total order. Use a separate sequence or timestamp with higher resolution for that requirement.
Interoperability
- PostgreSQL has native
uuid; both v4 and v7 fit. - MySQL
BINARY(16)orCHAR(36)works; v7's sortability applies to both. - JavaScript can generate v4 with
crypto.randomUUID(); v7 usually needs a library or a small implementation. - Some APIs validate version 4 specifically. Check before switching.
- ULID is not a UUID and may fail a UUID validator.
When to choose which
Choose v4 when:
- The ID is public and creation time should not leak.
- You need the widest library support.
- The table is small or reads dominate.
- You do not control the schema and a strict UUID v4 format is expected.
Choose v7 when:
- The ID is a primary key in a write-heavy table.
- You want identifiers that sort by creation time for pagination or debugging.
- Leaking millisecond-level creation time is acceptable.
- Your database and libraries handle non-v4 UUIDs.
Choose ULID when:
- You want a shorter, case-insensitive, sortable identifier.
- The consumer accepts a 26-character Crockford base32 string.
- You value 80 random bits over UUID interoperability.
A note on “UUID v4 is insecure”
v4 is not insecure because it is random; it is secure precisely because it is random. The security question is whether an identifier is used as an authorization token. It should not be. Use a separate, high-entropy secret with a defined lifetime, and check permissions server-side.
Both versions solve identity, not authorization. Pick based on ordering and leakage, then keep the security decisions separate.
Generate and compare both in the UUID v4 and UUID v7 tools, or read the side-by-side v4 vs v7 comparison.