UUIDs Explained: UUID v4 vs UUID v7, Standards, and Database Indexing
Everything developers need to know about Universally Unique Identifiers: RFC 9562 standards, UUID v4 vs v7, database B-Tree index performance, and collision math.
1. The Anatomy of a 128-Bit Universally Unique Identifier (RFC 9562)
A Universally Unique Identifier (UUID), standardized under IETF RFC 9562 (superseding RFC 4122), is a 128-bit label used to uniquely identify information across computer systems without requiring central coordination or database locks.
In canonical textual representation, a UUID is written as 32 hexadecimal characters divided into five groups separated by hyphens (8-4-4-4-12 format), totaling 36 characters: `f47ac10b-58cc-4372-a567-0e02b2c3d479`. The 13th hexadecimal character indicates the UUID version (e.g., '4' for UUID v4, '7' for UUID v7), while the 17th character indicates the UUID variant.
2. UUID v4: Random Generation and Collision Probability Math
UUID v4 is generated entirely from pseudorandom or cryptographically random numbers. Out of the 128 total bits, 6 bits are reserved for version and variant metadata, leaving 122 bits of pure entropy (2^122 possible values, or approximately 5.3 x 10^36 unique IDs).
What is the probability of a collision? Based on the Birthday Paradox, if you generated 1 billion UUID v4 identifiers every single second for 100 consecutive years, the probability of creating a single duplicate would still be less than 0.00000000006. For virtually all software architectures, UUID v4 collisions can be treated as mathematically impossible.
3. The Hidden Cost of UUID v4 in B-Tree Database Indexes
Despite their ubiquity, using UUID v4 as a primary key in relational databases (PostgreSQL, MySQL InnoDB, SQL Server) introduces severe performance penalties as tables grow beyond millions of rows.
Relational databases organize primary keys using B-Tree index structures. Because UUID v4 values are completely random, new records are inserted at random locations throughout the B-Tree leaf pages. This causes constant 'page splits', destroys memory locality, and forces the database engine to perform expensive disk I/O because index pages cannot remain cached in RAM. Insert throughput on large tables can drop by 50% to 80% when using UUID v4 compared to sequential keys.
4. UUID v7: Time-Ordered Monotonic Identifiers for Modern Databases
Published in RFC 9562, UUID v7 resolves the B-Tree index fragmentation problem while retaining all the distributed benefits of UUIDs. A UUID v7 embeds a 48-bit UNIX millisecond timestamp at the beginning of the identifier, followed by version bits and 74 bits of random entropy.
UUID v7 Structure:
[48-bit Millisecond Timestamp] - [4-bit Version (7)] [12-bit Sub-ms Counter] - [2-bit Variant] [62-bit Random Bits]
Because UUID v7 values naturally sort chronologically, database inserts append sequentially to the right side of B-Tree indexes, eliminating page fragmentation and restoring near-identical insert performance to auto-incrementing integers.
5. Comparing UUIDs to ULID, NanoID, and Auto-Incrementing BigInts
- Auto-incrementing BigInt (1, 2, 3...): Maximizes database insert speed and uses only 8 bytes of storage, but leaks business metrics (competitors can deduce total orders placed) and cannot be generated client-side.
- NanoID: Uses a customizable URL-friendly alphabet and smaller character lengths (e.g. 21 chars), popular in frontend applications.
- ULID (Universally Unique Lexicographically Sortable Identifier): A predecessor to UUID v7 using Crockford's Base32 encoding (26 characters).
- UUID v7: The official IETF standardized format that offers native 128-bit UUID database column compatibility while providing time-ordered performance.
6. Generating Cryptographically Strong UUIDs in Modern JavaScript
Modern browsers and Node.js (v14.17+) natively support cryptographically secure UUID v4 generation without external npm packages:
// Native browser & Node.js UUID v4 generator
const id = crypto.randomUUID();
console.log(id); // e.g., '36b8f84d-df4e-4d49-b662-bcde71a8764f'
WebUtil's UUID Generator allows you to instantly generate single or bulk batches of UUID v4 and UUID v7 identifiers directly in your browser with zero server roundtrips.
Recommended Articles & Guides
How to Format JSON Properly: A Complete Guide
Learn how to format JSON data correctly with best practices, common patterns, and troubleshooting tips for developers.
Common JSON Errors and How to Fix Them
A comprehensive guide to the most frequent JSON errors developers encounter and step-by-step solutions to fix them.
URL Encoding and Decoding: What Every Developer Should Know
Understand how URL encoding works, why it matters for web development, and how to properly encode and decode URLs.