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

    01936b2e-1e85-7000-8000-000000000000
    48-bit timestamp (ms)
    Version (7)
    Variant (8, 9, a, b)
    Random bits
    Did you know?ISO 8601 is the international standard for date formatsLearn more

    How UUIDv7 Works

    A UUIDv7 consists of 128 bits organized as follows:

    Bits 0-47Unix timestamp in milliseconds since epoch
    Bits 48-51Version indicator (always 7)
    Bits 52-63Random or counter data for sub-millisecond ordering
    Bits 64-65Variant indicator (RFC 4122 format)
    Bits 66-127Random data for uniqueness

    Why 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

    VersionSortableTimestampBest For
    UUIDv1PartiallyYes (100ns)Legacy systems
    UUIDv4NoNoRandom IDs
    UUIDv7Yes ✓Yes (ms) ✓Database PKs
    ULIDYesYes (ms)UUID alternative
    Did you know?The Y2038 problem affects 32-bit systemsLearn more

    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

    1Remove the hyphens from the UUID
    2Take the first 12 hexadecimal characters
    3Convert from hexadecimal to decimal
    4Result is a Unix timestamp in milliseconds
    JavaScript
    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.000Z
    Python
    from 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:45

    Generating UUIDv7 in Code

    Node.js 20+
    import { randomUUID } from 'crypto';
    
    // Node.js 20+ supports UUIDv7 natively
    const uuid = crypto.randomUUID({ type: 'v7' });
    Python
    # pip install uuid7
    from uuid_extensions import uuid7
    
    uuid = uuid7()
    print(str(uuid))
    PostgreSQL 17+
    -- 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