Backend & Databases 6 min read June 8, 2026

A Developer's Guide to UUIDs: Understanding v1, v4, and the New v7

Deep dive into Universally Unique Identifiers (UUIDs). Learn the structural differences between v1 (time-based), v4 (random), and the newly standardized v7 (time-ordered), and why v7 is the new standard for primary keys.

If you have ever designed a database schema or a distributed system, you have faced the decision of how to identify your records. Historically, auto-incrementing integers (like 1, 2, 3...) were the default. However, in modern distributed systems, auto-incrementing keys create massive headaches: they require a central database coordinator to prevent duplicate keys, and they leak business intelligence via URL parameters (e.g., a competitor can determine how many orders you process daily by registering two accounts and comparing the order IDs).

To solve this, developers moved to **UUIDs (Universally Unique Identifiers)**. But not all UUIDs are created equal. Let's break down the mechanics of the three most popular variants—v1, v4, and the modern v7—and examine their performance implications on B-Tree indexing.

1. Version 1 (v1): The Time & Node Variant

UUID v1 is generated using a combination of the current system timestamp (specifically, the number of 100-nanosecond intervals since October 15, 1582) and the generating computer's MAC address (acting as the "node ID").

  • Structure: 60 bits for the timestamp, 48 bits for the MAC address, and 20 bits for clock sequence / version metadata.
  • Pro: High degree of uniqueness and sequential order when generated on a single machine.
  • Con: Privacy leaks. Because the MAC address is baked into the string, anyone holding the UUID can determine which machine generated it, posing a security risk. Additionally, timestamp bits are stored in a shuffled order (low-middle-high), which ruins chronological sorting at the byte level.

2. Version 4 (v4): The Random Variant

UUID v4 is the most widely used identifier today. It discards timestamps and hardware addresses, replacing them entirely with pseudo-random or cryptographically secure random numbers.

  • Structure: 122 bits of random data, with 6 bits reserved for the variant and version markers.
  • Pro: Completely anonymous and leaks no metadata. Exceedingly simple to generate locally.
  • Con: Index fragmentation. Because v4 UUIDs are completely random, they lack any ordering. When inserted as primary keys in B-Tree indexed database tables (like PostgreSQL's `pg_index` or MySQL's InnoDB), they force random write operations across the index leaf pages, resulting in a phenomenon known as B-Tree page splits. This leads to heavy disk I/O, low caching efficiency, and index bloat.
The B-Tree Index Problem: When inserting random UUID v4 values, the database must write to random physical blocks on disk. If a target index page is full, the database must split the page in half, move half the keys to a new page, and insert the new row. For write-heavy tables, this degrades throughput dramatically.

3. The Solution: Version 7 (v7)

Standardized in RFC 9562, UUID v7 combines the best of both worlds: the chronological sequence of v1 and the high entropy/randomness of v4. It does this using a **Unix timestamp layout**.

  • Structure: The first 48 bits store a millisecond-precision Unix timestamp. The remaining 74 bits consist of random entropy (along with version markers).
  • Sortability: Because the timestamp is in the most significant bits (leftmost), UUID v7 strings are naturally lexicographically and chronologically sortable. A newer record will always have a lexicographically larger key than an older record.
  • Database Friendliness: Because keys are sequential, inserts occur at the "right side" of B-Tree indexes. The database writes to the same leaf page until it is full, then naturally moves to a new page. This eliminates page splits, reduces index fragmentation to near-zero, and allows write-heavy inserts to execute at speeds close to auto-incrementing integers.

Summary: Which UUID Should You Choose?

For almost all modern database schemas and API designs, **UUID v7 is the superior choice**. It preserves privacy, maintains collision safety, and ensures high-performance database inserts. Use UUID v4 only when you explicitly want keys to be non-sequential (for example, to prevent users from guessing previous/subsequent transaction records in a sequence).

Advertisement