Back to Guides
Database Engineering & IETF RFC Specs

UUID v4 vs. UUID v7: The Architectural Shift in Primary Key Design

For decades, software engineers relied on UUID v4 (random identifiers) for primary keys in distributed microservices. However, the introduction of time-ordered UUID v7 in draft RFC 9562 revolutionized database indexing performance.

At-a-Glance Feature Comparison

Specification DimensionUUID v4 (RFC 4122)UUID v7 (New RFC Standard)
Ordering MechanicsCompletely Random (122 bits of entropy)Time-ordered (48-bit Unix timestamp + 74-bit entropy)
B-Tree Index ImpactHigh fragmentation, severe cache misses & page splitsZero B-Tree fragmentation, sequential append inserts
Collision ProbabilityExtremely low (1 in 2¹²²)Negligible (Sub-millisecond monotonicity protection)
Distributed GenerationNo central coordination requiredNo central coordination required

The Problem with UUID v4 in Relational Databases

Relational databases like PostgreSQL (B-Tree indexes) and MySQL InnoDB (Clustered Primary Key indexes) store table data ordered by the primary key. When a row with a random UUID v4 key is inserted:

  • The database engine must traverse the entire B-Tree to locate the random leaf node where the key belongs.
  • If the target 8KB page is full, the engine performs an expensive page split, shuffling half the row references to a new disk block.
  • As tables grow into millions of records, primary key indexes no longer fit in RAM (Buffer Pool), causing massive I/O bottlenecks and disk thrashing.

How Time-Ordered UUID v7 Fixes Database Performance

UUID v7 embeds a 48-bit Big-Endian Unix epoch timestamp (in milliseconds) into the most significant bits of the 128-bit structure:

0 1 2 3

0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1

+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

| unix_ts_ms |

+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

| unix_ts_ms | ver (0111) | rand_a |

+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

|var| rand_b |

+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

Because higher-order bits increase monotonically with time, incoming database inserts append naturally to the end of the B-Tree index (similar to auto-incrementing integers), preserving contiguous memory allocation and keeping cache lines warm.

Verdict & Migration Recommendation

For all modern distributed backend systems, microservices, and web platforms, UUID v7 is the superior choice for primary keys. Use UUID v4 only when you specifically want un-sortable, obfuscated identifiers that hide creation timestamps.