Understanding UUID Versions
A Universally Unique Identifier (UUID), also known as a GUID in Microsoft ecosystems, is a 128-bit label standardized in RFC 4122 and updated in RFC 9562 (2024). It is represented as 32 hexadecimal characters in five hyphenated groups (8-4-4-4-12).
Comparison of UUID Versions
| Version | Generation Basis | Monotonic / Sortable | Best Use Case |
|---|---|---|---|
| UUID v4 | 122 bits of pseudo-randomness | No (Random distribution) | General primary keys, session IDs |
| UUID v7 | Unix Epoch MS timestamp + random bits | Yes (Sequential by time) | Database indexes (PostgreSQL, MySQL B-Trees) |
| Nil UUID | All 128 bits set to zero | N/A | Placeholder / default initializations |
Why UUID v7 is replacing UUID v4 for Databases
Random UUIDs (v4) cause severe index fragmentation in B-Tree based database engines (like PostgreSQL, MySQL InnoDB) because new keys insert unpredictably all across the tree. UUID v7 solves this by placing a millisecond-precision Unix timestamp in the high-order bits, providing natural time-sortability and high database write throughput.