UUID v4, v5, and v7 Guide: Performance, Collisions & Database Indexing
Deep dive into UUID versions (v4, v5, and the new RFC 9562 v7). Learn why UUID v7 is replacing v4 for database primary keys and B-Tree indexing.
Interactive Offline UUID Generator
Live Interactive SandboxGenerates RFC 4122 compliant UUIDs locally using the browser's crypto.getRandomValues API. Test the preset input below or customize it before launching into the full workspace.
Universally Unique Identifiers (UUIDs) are 128-bit identifiers standardized under RFC 4122 and recently updated under RFC 9562. They allow distributed distributed architectures to generate unique IDs without centralized coordination.
However, choosing the wrong UUID version can severely degrade database performance.
1. The Anatomy of a 128-Bit UUID
A UUID is rendered as 32 hexadecimal characters across five hyphenated blocks:
f47ac10b-58cc-4372-a567-0e02b2c3d479
xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx
M(13th character): Represents the UUID Version (e.g.4for random,7for Unix Epoch time-sorted).N(17th character): Represents the Variant (e.g.8,9,a, orbfor RFC standard).
2. UUID Versions Compared
| Version | Mechanism | Predictable? | Ideal Use Case |
|---|---|---|---|
v1 |
MAC Address + Timestamp | Yes (privacy risk) | Legacy systems (deprecated for public web). |
v3 |
MD5 Hash of Namespace + Name | Deterministic | Idempotent ID generation where MD5 is acceptable. |
v4 |
122 bits of pure Cryptographic Entropy | No | Ephemeral tokens, session keys, non-database IDs. |
v5 |
SHA-1 Hash of Namespace + Name | Deterministic | Deterministic IDs where duplicate inputs must match. |
v7 |
48-bit Unix Timestamp (ms) + 74 bits Random | Monotonic | Modern Database Primary Keys (PostgreSQL, MySQL). |
3. The UUID v4 Database Indexing Problem
When using UUID v4 as a primary key in relational databases:
- Every new row has an unpredictable, random hash.
- In a B-Tree index, the new row must be inserted into an arbitrary memory page on disk.
- Once the index exceeds available RAM, this causes massive page splits, cache thrashing, and high disk write latency.
How UUID v7 Solves This
UUID v7 stores a 48-bit millisecond Unix timestamp in the most significant bits:
┌──────────────────────────────┬──────────────┬──────────────────────────────┐
│ 48-bit Unix Timestamp (ms) │ 12-bit Rand │ 62-bit Entropy │
│ (Sequential Ordering) │ + Ver (7) │ + Variant │
└──────────────────────────────┴──────────────┴──────────────────────────────┘
Because the timestamp comes first, UUID v7 rows are inserted monotonically at the end of the B-Tree index, delivering performance nearly identical to traditional auto-incrementing integers while retaining distributed generation safety!
Live Tool: UUID / ULID / NanoID Generator
Client-Side EngineGenerate cryptographically secure UUIDv4, ULID, and NanoID strings in bulk.