UUID v4 vs UUID v7: Which Unique Identifier Should You Use?
Compare UUID version 4 and version 7. Understand the difference between cryptographically random and time-sorted ordered IDs.
UUID v4 (Random)
Option ALaunch the live utility to execute calculations, format payloads, or inspect outputs locally in your browser.
UUID v7 (Time-Sorted)
Option BLaunch the live utility to run parallel benchmarks, test parameters, or process data offline.
๐ Feature Comparison Matrix
| Specification / Feature | UUID v4 (Random) | UUID v7 (Time-Sorted) |
|---|---|---|
| Generation Basis | 122 bits of pure entropy (randomness) | Unix timestamp (48 bits) + random entropy |
| Time Sortable | โ No | โ Yes |
| Database Indexing Speed | Slower (causes fragmentation) | Faster (sequential insertion order) |
| Exposes Creation Date | โ No | โ Yes |
| Standard Specification | RFC 4122 | RFC 9562 (Latest Spec) |
๐ก Verdict & Architectural Recommendation
Use UUID v7 for database primary keys where sort order and index performance matter. Use UUID v4 for general-purpose tokens where timestamps shouldn't be exposed.
๐ฏ When to Use UUID v4 vs UUID v7
Use UUID v4 When:
- โขChronological ordering is irrelevant or undesirable
- โขMaximum randomness and unpredictability are required
- โขYou need to avoid exposing creation timestamps for security reasons
- โขWorking with legacy systems that expect version 4 UUIDs
- โขGenerating general-purpose tokens, API keys, or session identifiers
Use UUID v7 When:
- โขUsing UUIDs as database primary keys for optimal index performance
- โขBuilding distributed systems requiring time-ordered event IDs
- โขWorking with time-series data where chronological sorting matters
- โขMinimizing database index fragmentation and improving query speed
- โขBuilding modern applications where sortability provides architectural benefits
๐ Common Use Cases & Performance Impact
1. Database Primary Keys
UUID v7's timestamp-first design creates sequential values that align with B-tree index structure, dramatically reducing page splits and improving write performance.
2. Distributed Event Logging
In distributed systems, UUID v7 provides natural time-ordering across multiple servers without centralized coordination, while v4 requires additional timestamp fields.
3. Security Tokens & API Keys
Use UUID v4 for security tokens where exposing creation timestamps could leak sensitive information about user activity patterns or system behavior.
๐ Migrating from UUID v4 to v7
Existing UUID v4 values remain valid and can coexist with new v7 UUIDs during migration. Here's the recommended migration strategy:
Update ID Generation Code
Switch new record creation to generate UUID v7 instead of v4
Support Mixed Versions
Ensure application logic handles both v4 and v7 UUIDs during transition period
Rebuild Database Indexes
Recreate indexes to optimize for sequential v7 inserts and reduce fragmentation
Monitor Performance
Track insert performance, index size, and query speeds to measure improvement
Optional: Backfill Legacy Data
Consider converting old v4 IDs only if sortability of historical data is critical
โ Frequently Asked Questions
What is the difference between UUID v4 and UUID v7?โผ
UUID v4 uses 122 bits of pure random entropy with no time-based component, making it completely unpredictable but unsortable. UUID v7 uses a 48-bit Unix millisecond timestamp prefix followed by 74 random bits, making it lexicographically sortable and optimized for database indexes while maintaining uniqueness.
When should I use UUID v4?โผ
Use UUID v4 when chronological ordering is irrelevant, maximum randomness is required, you need to avoid exposing creation timestamps, or you're working with legacy systems that expect version 4 UUIDs. It's ideal for general-purpose tokens, API keys, and session identifiers.
When should I use UUID v7?โผ
Use UUID v7 for database primary keys, distributed event IDs, time-series data, and modern applications requiring sortability. UUID v7 provides better database index performance through sequential insertion order, reduced index fragmentation, and improved query efficiency for time-range lookups.
Can I migrate from UUID v4 to UUID v7?โผ
Yes, but it requires application logic changes and database index rebuilds. Existing v4 UUIDs remain valid and can coexist with new v7 UUIDs. Migration typically involves: updating ID generation code, rebuilding indexes for optimal performance, and handling mixed-version scenarios during transition.
Why is UUID v7 better for database performance?โผ
UUID v7's timestamp-first design creates naturally sequential values that align with B-tree index structure, reducing page splits and index fragmentation. This results in faster insertions, better cache locality, smaller index sizes, and improved query performance compared to random UUID v4 values that cause scattered writes.