root/learning-hub/data-serialization/uuid-v4-v5-v7-explained
← Back to Learning Hub
DATA & SERIALIZATION7 min readIntermediate100% Client-Side Verified

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.

#UUID#Databases#PostgreSQL#Architecture#Performance#Data

Interactive Offline UUID Generator

Live Interactive Sandbox
100% Client-Side

Generates 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.

Batch Quantity:
Ready for advanced parsing, syntax error highlighting & bulk export?
Open in Full Workspace (UUID Generator)

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. 4 for random, 7 for Unix Epoch time-sorted).
  • N (17th character): Represents the Variant (e.g. 8, 9, a, or b for 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:

  1. Every new row has an unpredictable, random hash.
  2. In a B-Tree index, the new row must be inserted into an arbitrary memory page on disk.
  3. 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 Engine

Generate cryptographically secure UUIDv4, ULID, and NanoID strings in bulk.

Launch Tool Workspace

Frequently Asked Questions (FAQ)

To have a 50% chance of a single collision, you would need to generate 1 billion UUIDs per second continuously for approximately 85 years (about 2.71 x 10^18 UUIDs).