UUIDv4 vs UUIDv7: which one, and when
In one line: Bit layout, sorting habits, index impact and the pitfalls of monotonic mode, compared for real systems.
UUIDv4 and UUIDv7 are both 128-bit UUIDs and share the same text form, xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx. The difference is what those 128 bits contain. v4 spends almost all of them on randomness; v7 puts a millisecond timestamp in the first 48 bits.
Bit layout
| Field | UUIDv4 | UUIDv7 |
|---|---|---|
| 0–47 | random_a (48) | unix_ts_ms (48) |
| 48–51 | version = 4 | version = 7 |
| 52–63 | random_b (12) | rand_a (12) |
| 64–65 | variant = 10 | variant = 10 |
| 66–127 | random_c (62) | rand_b (62) |
v4 carries 122 bits of entropy. v7 carries 74 bits (12 + 62), and in monotonic mode rand_a becomes a 12-bit counter, leaving 62 bits of pure randomness. The standard allows that counter so values generated in the same millisecond stay ordered.
Ordering and index locality
v4 is fully random, so lexicographic order has nothing to do with creation order. As a primary key it inserts at random positions in a B-Tree, causing page splits and cache misses and lowering bulk-insert throughput. v7 puts the first 48 bits in time order, so values from the same period land at the end of the index. Inserts become append-heavy, which helps both InnoDB and PostgreSQL.
The trade-off: v7 exposes its creation time. Anyone holding an ID can tell when a user signed up and estimate the issue rate. If that matters, use v4 or a random prefix.
Pitfalls of monotonic mode
A monotonic UUIDv7 increments a counter inside the same millisecond. When the counter passes 4095 it moves to the next millisecond, and if the clock steps backwards it keeps the last timestamp. Ordering is guaranteed inside one process, but not across nodes generating at the same time; use a per-node prefix or a separate shard key.
017f22e2-79b0-7cc3-98c4-dc0c0c07398f ts=0x017f22e279b0 (2022-02-22 19:22:22 UTC) ver=7 rand_a=0xcc3 var=RFC rand_b=0x98c4dc0c0c07398f
How to choose
- UUIDv7: primary keys in new systems, log and event IDs, large tables that need ordering. It stays a 16-byte binary value and works with standard UUID tooling.
- UUIDv4: tokens that must hide creation time, public identifiers, small tables where ordering does not matter.
- Either: store as
uuidorBINARY(16), not 36-character text. Text more than doubles index size.
If you need a readable text format, see the ULID and KSUID guides. The decoder on this site tells v1, v4, v6 and v7 apart and shows the version and variant bits.
Decode an ID you just made and inspect its bit structure.
Open ULIDLabFrequently asked questions
Is UUIDv7 more secure than UUIDv4?
Not in the sense of secrecy. v4 is 122 bits of randomness; v7 has 74 random bits and a predictable timestamp for the rest. Use v4 for unguessable tokens, and v7 when collision resistance plus ordering is the goal.
What happens above 4096 IDs in one millisecond?
In monotonic mode the timestamp advances by 1 ms when the counter overflows, so values keep increasing and may run slightly ahead of the wall clock. At millions per second, design the counter range and node separation together.
Which PostgreSQL type should store a v7?
The uuid type is the default and stores it as binary. For very large indexes you can also split it into two bigint columns or use bytea. Whatever you choose, avoid storing the 36-character text form.
Sources
- RFC 9562 — Universally Unique IDentifiers (UUIDs)
- RFC 4122 — A UUID URN Namespace (previous standard)
- PostgreSQL — UUID data type
Read next
- ULID structure: why 26 characters sort by timeThe 48-bit timestamp, 80 random bits, Crockford Base32, monotonic ULIDs and the details that trip people up.
- NanoID: designing entropy with alphabet and lengthHow the default 64-character alphabet and 21 characters produce 126 bits, where short IDs become risky, and the traps in custom alphabets.
- KSUID: the 2014 epoch and 27 Base62 charactersFour bytes of epoch seconds, a 128-bit payload, Base62 ordering, and how KSUID compares with ULID and UUIDv7.