Understanding UUIDv7: Time-Sortable UUIDs
The modern UUID format with embedded timestamps for better database performance
UUIDv7 is a new universally unique identifier format defined in RFC 9562 (May 2024). Unlike random UUIDs (v4), UUIDv7 embeds a Unix timestamp in milliseconds, making the identifiers time-sortable while maintaining global uniqueness.
UUIDv7 Structure
How UUIDv7 Works
A UUIDv7 consists of 128 bits organized as follows:
Bits 0-47Unix timestamp in milliseconds since epochBits 48-51Version indicator (always 7)Bits 52-63Random or counter data for sub-millisecond orderingBits 64-65Variant indicator (RFC 4122 format)Bits 66-127Random data for uniquenessWhy Use UUIDv7?
Time-Sortable
Natural chronological ordering without additional timestamp columns
Better Index Performance
Sequential inserts reduce B-tree fragmentation in databases
Globally Unique
No coordination needed between distributed systems
Embedded Timestamp
Extract creation time directly from the ID itself
UUIDv7 vs. Other UUID Versions
| Version | Sortable | Timestamp | Best For |
|---|---|---|---|
| UUIDv1 | Partially | Yes (100ns) | Legacy systems |
| UUIDv4 | No | No | Random IDs |
| UUIDv7 | Yes ✓ | Yes (ms) ✓ | Database PKs |
| ULID | Yes | Yes (ms) | UUID alternative |
Database Performance Benefits
When using random UUIDs (v4) as primary keys, each new record is inserted at a random position in the B-tree index, causing:
- ✗Index page splits and fragmentation
- ✗Poor cache utilization
- ✗Slower write performance over time
UUIDv7 solves this because new records are always appended near the end of the index (they're time-ordered), similar to auto-incrementing integers but with the benefits of UUIDs.
Extracting Timestamps from UUIDv7
function extractTimestamp(uuidv7) {
const hex = uuidv7.replace(/-/g, '').slice(0, 12);
return parseInt(hex, 16);
}
const uuid = '01936b2e-1e85-7000-8000-000000000000';
const timestamp = extractTimestamp(uuid);
console.log(new Date(timestamp));
// Output: 2024-12-01T10:30:45.000Zfrom datetime import datetime
def extract_timestamp(uuidv7: str) -> int:
hex_str = uuidv7.replace('-', '')[:12]
return int(hex_str, 16)
uuid = '01936b2e-1e85-7000-8000-000000000000'
timestamp = extract_timestamp(uuid)
print(datetime.fromtimestamp(timestamp / 1000))
# Output: 2024-12-01 10:30:45Generating UUIDv7 in Code
import { randomUUID } from 'crypto';
// Node.js 20+ supports UUIDv7 natively
const uuid = crypto.randomUUID({ type: 'v7' });# pip install uuid7
from uuid_extensions import uuid7
uuid = uuid7()
print(str(uuid))-- PostgreSQL 17+ supports UUIDv7
SELECT gen_random_uuid_v7();When to Use UUIDv7
✓ Ideal For
- Database primary keys — especially high-write scenarios
- Distributed systems — no coordination needed
- Event sourcing — natural event ordering
- Audit logs — embedded timestamp in every ID
- APIs — client-generated, chronological IDs
⚠ Consider Alternatives
- Privacy concerns — IDs reveal creation time (use UUIDv4)
- 64-bit constraint — use Snowflake IDs instead
- Human-readable — use NanoID or custom schemes
Try Our UUIDv7 Converter
Extract timestamps from UUIDv7 identifiers and convert to human-readable dates.
Related Articles
What is Unix Time?
A complete guide to epoch timestamps and why developers use them