Why Is UUID v7 Better Than UUID v4 for Database Primary Keys?
UUID v7 is significantly better for database primary keys because it begins with a 48-bit millisecond timestamp, making records sortable in chronological order. This preserves B-tree index locality and eliminates the severe page fragmentation and cache thrashing caused by completely random UUID v4 values.
Detailed Technical Explanation
Standard UUID v4 consists of 122 completely random bits. When used as a clustered primary key in databases like PostgreSQL or MySQL (InnoDB), inserting a new row forces the database engine to place the record at a completely random location within the table's B-tree index structure. As tables grow larger than RAM, this causes frequent disk reads/writes (B-tree node splits) and destroys write throughput. Standardized in RFC 9562, UUID v7 encodes a 48-bit Unix timestamp in its most significant bits, followed by sub-millisecond precision and random bits. New keys are naturally sequential, appending cleanly to the end of index leaf pages like an auto-incrementing integer while retaining global uniqueness.
Technical Example & Code
Comparing internal bit layouts: timestamp ordering vs random dispersion.
// 1. UUID v4 (Completely Random - Fragments B-Tree Indexes):
018e3a2b-9f4a-4e29-9a7f-1d8f3c7e4a1b -> Random insert location
f47ac10b-58cc-4372-a567-0e02b2c3d479 -> Jump to end of index
3b241101-e2bb-4255-8caf-4136c508a0cb -> Jump to middle of index (forces node split!)
// 2. UUID v7 (Monotonically Ordered by Millisecond Timestamp):
// [48-bit Unix Timestamp (ms)]-[ver/rand]-[var/rand]-[random]
018e3a2b-9f4a-7182-849a-e274a10f92b1 -> 2026-10-06 00:30:00.001
018e3a2b-9f5b-7893-9031-c4581f4a9b20 -> 2026-10-06 00:30:00.015 (Appends sequentially!)
018e3a2b-9f6c-7bc1-bf02-124987ba4512 -> 2026-10-06 00:30:00.032 (Zero page fragmentation!)Why This Is the Case (Underlying Architectural Rationale)
"Relational database storage engines (such as InnoDB) cluster the entire table row data alongside the primary key index pages. Sequential keys ensure that write operations only mutate the current trailing leaf page currently in memory cache, maintaining optimal O(1) buffer pool cache utilization. Random UUID v4 keys force the storage engine to read random index pages from disk for every insert, degrading throughput by up to 80% on large datasets."
Common Misconception vs Reality
"UUID v7 is predictable, so an attacker can guess other users' UUIDs easily."
While the first 48 bits encode the timestamp, UUID v7 still contains 74 bits of cryptographically secure randomness, meaning there are 18 quintillion possible UUIDs per millisecond, making collision or enumeration attacks practically impossible.
Related Curious-Techie Resources
UUID v4 vs UUID v7: Technical Comparison →
Comprehensive benchmark of B-tree index fragmentation, throughput, and sortability.
Interactive ToolUUID Generator & Validator Tool →
Generate RFC 9562 compliant UUID v4 and UUID v7 identifiers in browser memory.
Developer ReferenceUUID Versions Reference & RFC 9562 →
Related Questions in the Q&A Knowledge Base
Yes, Base64 encoding increases data size by approximately 33.3% (a 4:3 ratio). For every 3 bytes of raw binary data, Base64 produces 4 ASCII characters, plus up to 2 bytes of padding characters (`=`).
Changing a single character (or even a single bit) changes approximately 50% of the output SHA-256 hash digest because of the cryptographic Avalanche Effect. Secure hash algorithms use iterative rounds of bitwise rotations, non-linear boolean operations, and modular addition to diffuse input bits across the entire internal state.
Yes, standard JSON Web Tokens (JWS) are signed, not encrypted. The header and payload are merely Base64URL-encoded strings that anyone can decode and read in plaintext without knowing the secret key.
Écosystème Technique Associé et Prochaines Étapes
Articles Techniques Associés
What Is Base64 Encoding?
Direct definition of 6-bit chunking and binary-to-text ASCII conversion.
What Is a UUID (Universally Unique Identifier)?
128-bit distributed identifiers and the evolution from v4 to v7.
UUID v4 vs UUID v7: Database Primary Key Performance
Random entropy compared to timestamp-ordered B-Tree index alignment.
Laboratoires Pratiques de Diagnostic
Spécifications et Références Développeur
Poursuivre l’Apprentissage
UUID Versions & Specifications Reference Matrix
Compare the internal bit allocations of UUID v4 and time-ordered UUID v7.