Universally Unique Identifiers (UUIDs) serve as standard 128-bit values for identifying resources across distributed software systems without requiring a central coordinating authority. For years, UUID v4 (RFC 4122) has been the default choice. However, the publication of RFC 9562 introduced UUID v7, specifically designed to address database indexing performance issues inherent in pure random UUIDs.
Direct Answer: UUID v4 is composed of 122 bits of pure pseudo-randomness, making it ideal for client-generated security tokens or sensitive IDs where creation timestamps must remain hidden. However, inserting UUID v4 keys into B-Tree database indexes (such as PostgreSQL or MySQL InnoDB) causes severe page splitting and random I/O thrashing. UUID v7 solves this by embedding a 48-bit Unix millisecond timestamp at the front of the 128-bit payload followed by 74 random/sequence bits. This creates monotonically increasing, time-ordered identifiers that preserve high-throughput B-Tree append performance while maintaining global uniqueness.
1. Bit Allocation & Internal Structure Comparison
Understanding the exact 128-bit structural layout of both RFC specifications:
UUID v4 Layout (128 bits total):
[ 32 bits Random ] - [ 16 bits Random ] - [ 4-bit Ver (0100) + 12-bit Random ] - [ 2-bit Var (10) + 14-bit Random ] - [ 48 bits Random ]
UUID v7 Layout (128 bits total):
[ 48-bit Unix Timestamp (ms) ] - [ 12-bit Sub-ms / Sequence ] - [ 4-bit Ver (0111) + 12-bit Random ] - [ 2-bit Var (10) + 62-bit Random ]
- UUID v4 (Pure Random):
- Version bits: Set to
0100(Version 4). - Variant bits: Set to
10(RFC 4122 variant). - Randomness: 122 cryptographically secure random bits (2ΒΉΒ²Β² β 5.3 Γ 10Β³βΆ unique combinations).
- Version bits: Set to
- UUID v7 (Time-Ordered):
- Timestamp: 48-bit big-endian unsigned integer representing Unix epoch timestamp in milliseconds (valid until year 10889 AD).
- Version bits: Set to
0111(Version 7). - Randomness / Counter: 74 remaining bits dedicated to sub-millisecond sequence counters and cryptographically random data.
To generate v4 UUIDs, formatted IDs, or inspect identifier formats, use the UUID Generator.
2. Database B-Tree Indexing: Page Splits & Cache Locality
The primary driver for adopting UUID v7 over UUID v4 is relational database performance:
- Random Insertion Thrashing (UUID v4): B-Tree indexes sort keys sequentially. Because UUID v4 values are completely random, inserting new rows requires writing data into random leaf pages across the disk. Once index size exceeds available RAM (buffer pool), the database is forced to read and write random 16KB index pages from disk, causing massive I/O bottlenecks.
- Sequential Append Efficiency (UUID v7): Because UUID v7 begins with a 48-bit timestamp, newly generated IDs are naturally sorted in ascending chronological order. New index entries are consistently appended to the rightmost leaf page of the B-Tree (similar to auto-incrementing integers), maximizing CPU cache hits and eliminating page splitting.
3. Head-to-Head Comparison Matrix
| Feature / Metric | UUID v4 (RFC 4122) | UUID v7 (RFC 9562) |
|---|---|---|
| Primary Design Focus | Unpredictable Random Uniqueness | Time-Ordered Database Friendly Identifier |
| Timestamp Component | None | 48-bit Unix Epoch Timestamp (ms precision) |
| Randomness Bits | 122 Bits | 74 Bits (Random + Sub-ms counter) |
| Sortability | Unsorted / Random | Lexicographically Sortable by Creation Time |
| B-Tree Index Impact | High fragmentation & random I/O thrashing | High locality, sequential append optimization |
| Privacy / Security | Hides creation time (Zero metadata leak) | Exposes creation timestamp in the first 48 bits |
| Collision Risk | Virtually Zero ($2^{122}$) | Virtually Zero ($2^{74}$ per millisecond) |
4. When to Use Which Identifier Standard
- Use UUID v7 when:
- Setting primary keys on database tables with high insert throughput (e.g., logs, transactions, user records, orders).
- You require sortable IDs without creating a separate
created_atindex.
- Use UUID v4 when:
- Generating public session tokens, password reset keys, or API tokens where revealing the precise creation timestamp poses a security risk.
- Identifiers are stored in memory or key-value stores (e.g., Redis) where B-Tree page splitting does not apply.
Developers converting Unix timestamps or generating cryptographic hashes can utilize the Unix Timestamp Converter, JSON Formatter, and Hash Generator.
5. Frequently Asked Questions (FAQs)
Can UUID v7 values collide if two are generated in the exact same millisecond?
Collision is still virtually impossible. UUID v7 dedicates 74 bits to random and sub-millisecond sequence data even when the 48-bit timestamp is identical, giving roughly 2^74 possible combinations within that same millisecond β a vastly larger space than any realistic single-millisecond generation rate.
Does using UUID v7 as a primary key leak sensitive information?
Yes, partially. Because the first 48 bits directly encode the record's creation timestamp in milliseconds, anyone who can see a UUID v7 value can determine roughly when that row was created. For public-facing identifiers like password reset tokens or session IDs where hiding creation time matters, UUID v4 remains the safer choice.
Can an existing table with UUID v4 primary keys be migrated to UUID v7 without downtime?
Not simply by changing the ID generation function going forward β existing UUID v4 rows remain randomly ordered in the index regardless of new insert patterns, so the B-Tree fragmentation problem persists for old data until it's rewritten. Most teams either accept mixed fragmentation for historical rows and let it fade as old data is archived, or perform a full table rebuild with new UUID v7 keys during a planned migration window.
References: IETF RFC 9562 (Universally Unique IDentifiers - UUID); IETF RFC 4122 (A Universally Unique IDentifier - UUID URN Namespace, obsoleted by RFC 9562).