UUID v4 vs UUID v7
Both are 128-bit identifiers standardized in RFC 9562 with identical 36-character string representations. They differ in how those 128 bits are populated: pure randomness in v4 versus a 48-bit Unix millisecond timestamp prefix followed by 74 random bits in v7. Generate both formats in UUID v4 & v7 Generator or inspect existing keys in UUID Converter.
| UUID v4 | UUID v7 | |
|---|---|---|
| Bit layout | 122 random bits + 4 version bits (0100) + 2 variant bits (10) | 48-bit Unix epoch ms timestamp + 4 version bits (0111) + 2 variant bits (10) + 74 random/counter bits |
| Lexicographical sorting | Random order; sorting by UUID string or bytes has no chronological meaning | Monotonically increasing by creation millisecond; byte order matches chronological order |
| B-tree primary key impact | Inserts hit random leaf pages, causing mid-tree page splits, ~50% page fragmentation, and high buffer-pool churn | Inserts append sequentially to the rightmost B-tree leaf page, matching auto-increment integer locality |
| Collision entropy | 122 bits globally across all time | 74 bits within the same single millisecond |
| Metadata leakage | Zero: reveals nothing about when or where the identifier was created | Exposes exact creation timestamp (to the millisecond) in the first 12 hex characters |
| Database compatibility | Native 16-byte uuid type in PostgreSQL, BINARY(16) in MySQL | Drop-in wire and storage compatible with every 128-bit UUID column and validator |
When to choose v7 vs v4
Use UUID v7 for relational database primary keys, event-log IDs, and high-write tables in PostgreSQL, MySQL (InnoDB), or SQLite. Keeping new inserts clustered at the end of the B-tree avoids random disk I/O once the index outgrows memory. Read our UUID v4 vs UUID v7 Database Indexing Guide for benchmark mechanics and SQL migration patterns.
Use UUID v4 when the identifier acts as an unguessable capability token (such as a password-reset link, invite code, or public resource slug where disclosing the exact millisecond a record was created is undesirable).