Database Systems & Architecture • Published October 1, 2026 • Updated October 2, 2026 • 17 min read

UUIDv4 vs UUIDv7: Time-Ordered Keys, 128-Bit Entropy, and Database Index Performance

For over two decades, the 128-bit Universally Unique Identifier version 4 (UUIDv4)—standardized in 2005 under RFC 4122—served as the default primary key standard across distributed software systems. By relying on 122 bits of cryptographically secure pseudorandom entropy, UUIDv4 eliminated database key coordination, preventing auto-increment ID collision risks across multi-region microservices.

However, as database tables scaled into hundreds of millions of rows, architects encountered an insidious bottleneck: B-Tree index degradation. The uniform randomness of UUIDv4 forces database storage engines (such as PostgreSQL B-Trees, MySQL InnoDB clustered indexes, and MongoDB wiredTiger indexes) to insert records at completely random physical leaf locations. This results in heavy index page splitting, buffer cache thrashing, and high disk I/O.

In 2024, the IETF ratified RFC 9562 ("New UUID Formats"), introducing UUIDv7. UUIDv7 combines a 48-bit Unix epoch millisecond timestamp with 74 bits of entropy, unlocking chronological sorting while retaining global uniqueness. In this technical deep dive, we compare UUIDv4 and UUIDv7 bit architectures, evaluate B-Tree indexing benchmarks, analyze collision mathematics, and demonstrate generation via the Collabsource UUID Generator.

Advertisement
Responsive In-Article Ad Unit

The Database Dilemma: Why UUIDv4 Breaks B-Tree Indexes

Relational databases store primary keys inside balanced tree (B-Tree or B+Tree) indices. In clustered index engines like MySQL InnoDB, the entire data row is physically co-located on disk according to the primary key's sort order.

The Mechanics of Index Page Splitting

When sequential auto-incrementing integers or time-ordered keys are inserted, the database writes sequentially to the rightmost leaf page of the B-Tree. When a page fills to its capacity (typically 8KB or 16KB), the engine cleanly allocates a new page at the end of the index without disturbing existing pages.

Conversely, because UUIDv4 keys are uniformly distributed across the entire 128-bit address space, every incoming row has an equal probability of belonging to any page in the entire database:

  1. Cache Eviction: Because insertion targets are random, the database engine can no longer keep active index pages warm in RAM (buffer pool). It must constantly page index blocks from SSD to memory.
  2. 50% Page Splitting: When a random insertion lands on a full page, the engine must split the page in half, move 50% of the rows to a newly allocated page, and update parent tree pointers.
  3. Storage Bloat: As a consequence of continuous 50/50 page splitting, UUIDv4 indexes typically run at only 50–60% storage density, doubling the memory required to hold the index.
DATABASE B-TREE INSERTION ARCHITECTURE UUIDv4 Random Splitting vs. UUIDv7 Right-Edge Sequential Append UUIDv4: Random Leaf Insertion Page 1 Split! Page 3 Massive RAM Cache Misses Index density drops to ~50% UUIDv7: Sequential Right Append Page 1 Page 2 Append Hot Leaf Stays in Memory Index density stays >95%
Figure 1: Comparison between random B-Tree page splits under UUIDv4 vs clean right-edge appends under UUIDv7.

RFC 9562 UUIDv7 Bit Layout & Structure

UUIDv7 solves the indexing problem by prefixing a big-endian timestamp to the identifier:

Field Name Bit Length Purpose & Binary Content
unix_ts_ms 48 bits Big-endian unsigned integer measuring Unix epoch milliseconds (valid until the year 10889 AD).
ver 4 bits Fixed version identifier: binary 0111 (hexadecimal 0x7).
rand_a 12 bits Sub-millisecond fraction, monotonic sequence counter, or pseudorandom bits.
var 2 bits RFC 4122 / 9562 variant flag: binary 10xx.
rand_b 62 bits Cryptographically secure pseudorandom entropy.

Generate High-Entropy UUIDv4 and UUIDv7 Identifiers

Generate single or bulk cryptographically secure RFC 9562 UUIDs directly inside your browser.

Launch UUID Generator →

128-Bit Entropy & Collision Probability Mathematics

A frequent concern when adopting time-ordered keys is whether shortening the random entropy from 122 bits (v4) to 74 bits (v7) elevates the risk of duplicate key generation in distributed microservices.

Under the mathematical principle of the Birthday Problem, the probability \(p\) of at least one collision occurring when generating \(n\) identifiers across an entropy pool of \(N = 2^{74}\) possibilities is approximated by:

\(p(n) \approx 1 - e^{-\frac{n^2}{2N}} \approx \frac{n^2}{2 \cdot 2^{74}} = \frac{n^2}{2^{75}}\)

Because the 74-bit entropy resets every single millisecond:

  • To reach a 1 in a billion (0.0000001%) chance of collision, a distributed cluster would need to generate 8.6 million UUIDv7s within the exact same millisecond (8.6 billion IDs per second).
  • Generating 100,000 IDs per millisecond (100 million requests/sec) yields a collision probability of only \(2.6 \times 10^{-13}\).
COLLISION PROBABILITY CURVE PER MILLISECOND UUIDv7 Millisecond-Bound Entropy Ceiling 1 ID/ms 10,000 IDs/ms 1 Million IDs/ms 100M IDs/ms 10^6 / ms → p < 10^-11
Figure 2: Probability of ID collision per millisecond window as concurrent cluster throughput scales.

Client-Side JavaScript Implementation (RFC 9562)

While modern browsers natively support crypto.randomUUID() for UUIDv4, UUIDv7 can be computed in lightweight client-side JavaScript using the Web Crypto API:

// Pure client-side RFC 9562 UUIDv7 generator
function generateUUIDv7() {
  const bytes = new Uint8Array(16);
  crypto.getRandomValues(bytes);

  // 1. Fill 48-bit Unix timestamp (milliseconds)
  const now = Date.now();
  bytes[0] = (now / 0x10000000000) & 0xff;
  bytes[1] = (now / 0x100000000) & 0xff;
  bytes[2] = (now / 0x1000000) & 0xff;
  bytes[3] = (now / 0x10000) & 0xff;
  bytes[4] = (now / 0x100) & 0xff;
  bytes[5] = now & 0xff;

  // 2. Set Version 7 (0111xxxx)
  bytes[6] = (bytes[6] & 0x0f) | 0x70;

  // 3. Set Variant 1 (10xxxxxx)
  bytes[8] = (bytes[8] & 0x3f) | 0x80;

  // 4. Format into canonical 8-4-4-4-12 hex string
  const hex = Array.from(bytes).map(b => b.toString(16).padStart(2, '0')).join('');
  return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
}

Frequently Asked Questions

UUIDv7 embeds a 48-bit millisecond Unix timestamp in its most significant bits. This makes values naturally time-ordered (k-sortable), allowing relational databases like PostgreSQL and MySQL to perform sequential, append-mostly B-Tree index insertions rather than random leaf insertions that trigger costly index fragmentation and page splits.
UUIDv7 is standardized under IETF RFC 9562 ('New Universally Unique Identifier Formats'), which supersedes the historical RFC 4122 standard and introduces modern UUID versions 6, 7, and 8.
UUIDv4 provides 122 bits of pseudorandom entropy. UUIDv7 allocates 48 bits to the timestamp and retains 74 bits of entropy (version/variant bits aside) per millisecond. Generating 1 billion UUIDv7 identifiers within a single millisecond has a collision probability of less than 1 in 100 billion, providing absolute collision safety in distributed systems.
Because UUIDv7 begins with the millisecond timestamp of its creation, anyone who inspects the ID can determine the exact date and time the record was created. If timestamp obscurity is a security requirement, an unindexed random UUIDv4 or encrypted identifier should be used instead.

Summary & Architecture Recommendations

For high-throughput distributed applications, UUIDv7 represents the ideal balance between autonomous decentralized key creation and high-efficiency database index caching. Transitioning new table schemas from UUIDv4 to UUIDv7 eliminates B-Tree page splits and ensures long-term database scalability.

CS

Collabsource Database Engineering Team

Distributed systems architects specializing in high-throughput database storage engines, index tuning, and RFC standards.