Skip to main content
ToolNara
developer7 min read2026-09-14

UUID v4 vs v7: Cryptographic Randomness, RFC 9562 & Collision Math

An architectural analysis comparing pure random UUID v4 with time-ordered UUID v7 for database primary keys and B-Tree index performance.

RJRana JawadPrincipal Systems Architect
UUIDCryptographyDatabasePostgreSQLRFC 9562

Key takeaways

  • UUID v4 provides 122 bits of cryptographically secure random entropy. Collision probability depends on the number of identifiers generated and is extremely low for normal application workloads.
  • Inserting purely random UUID v4 keys into PostgreSQL or MySQL causes massive B-Tree index fragmentation and memory page cache misses.
  • UUID v7 embeds a 48-bit Unix epoch millisecond timestamp at the front, creating natural time-ordered database clustering while retaining 74 bits of random entropy.
  • Modern web browsers can generate cryptographically secure UUID v4 strings natively via crypto.randomUUID() without external npm packages.

Companion utility

Try UUID / GUID Generator Online (Bulk v4 Cryptographic)

Use the browser utility for the same task, then return here for the implementation details.

Open tool

1. What Makes UUID v4 Truly Unique?

A UUID (Universally Unique Identifier) is a 128-bit value standardized under RFC 9562. Version 4 UUIDs are generated using cryptographically secure pseudorandom numbers (CSPRNG). Out of the 128 bits: - 6 bits are reserved for metadata (4 bits indicate Version 4, and 2 bits specify the RFC variant). - 122 bits represent pure, unadulterated randomness. The 122 random bits create a large identifier space. Collision probability rises with the number of generated values, so applications should still avoid treating identifiers as a substitute for a database uniqueness constraint.

2. Generating Cryptographic UUIDs Directly in the Browser

Years ago, developers had to bundle third-party npm packages like 'uuid' to generate identifiers in frontend code. Today, all modern browsers natively support the Web Crypto API, providing high-entropy hardware random generation with zero dependencies.

typescriptTested reference
// Native Web Crypto API (supported in all modern browsers and Node 16+)
const id = crypto.randomUUID();

console.log(id);
// Example: "9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d"

3. Why We Switched from UUID v4 to UUID v7 for Database Primary Keys

Random UUID v4 primary keys can scatter inserts across a B-Tree index. UUID v7 places a Unix timestamp at the front, which can improve locality for time-ordered workloads. The actual improvement depends on database, key width, workload, cache behavior, and the benchmark method, so measure the target system instead of assuming a fixed percentage.

Advertisement
Advertisement placement reserved for editorial content
RJ

Rana Jawad

Principal Systems Architect at ToolNara. The guides focus on browser runtime behavior, standards, and engineering trade-offs.

View author profile

Related guides

Back to all guides