UUID v7 Generator NEW

Generate time-ordered UUIDs with millisecond precision (RFC 9562)

Click "Generate UUID v7" to start

Bulk UUID v7 Generation

What is UUID v7?

UUID version 7 is a new UUID format standardized in RFC 9562 (May 2024). It combines a Unix timestamp with random data, creating UUIDs that are naturally sorted by creation time.

UUID v7 Structure

tttttttt-tttt-7xxx-yxxx-xxxxxxxxxxxx

Where:
- t = Unix timestamp in milliseconds (48 bits)
- 7 = Version identifier
- x = Random data
- y = Variant (8, 9, a, or b)

UUID v7 vs UUID v4 for Databases

UUID v7

Time-ordered. New records append to index. Minimal page splits. Better write performance.

UUID v4

Random. Causes index fragmentation. More page splits. Higher write overhead.

When to Use UUID v7

  • Database primary keys - significantly better B-tree index performance
  • Event sourcing - natural chronological ordering
  • Distributed systems - sortable across nodes without coordination
  • Audit logs - timestamp is embedded in the ID
  • Time-series data - records naturally ordered by creation time

Generate UUID v7 in Code

JavaScript (using this implementation)

function uuidv7() {
  const timestamp = Date.now();
  const timestampHex = timestamp.toString(16).padStart(12, '0');

  const randomBytes = new Uint8Array(10);
  crypto.getRandomValues(randomBytes);

  // Set version (7) and variant bits
  randomBytes[0] = (randomBytes[0] & 0x0f) | 0x70;
  randomBytes[2] = (randomBytes[2] & 0x3f) | 0x80;

  const randomHex = Array.from(randomBytes)
    .map(b => b.toString(16).padStart(2, '0'))
    .join('');

  return `${timestampHex.slice(0,8)}-${timestampHex.slice(8)}-${randomHex.slice(0,4)}-${randomHex.slice(4,8)}-${randomHex.slice(8)}`;
}

Python (uuid6 library)

from uuid6 import uuid7
new_uuid = uuid7()
print(new_uuid)

PostgreSQL

-- Using pgcrypto extension
CREATE EXTENSION IF NOT EXISTS pgcrypto;

-- Or use uuid-ossp with custom function
-- Many ORMs now support UUID v7 natively

Extracting Timestamp from UUID v7

function extractTimestamp(uuid) {
  const hex = uuid.replace(/-/g, '').slice(0, 12);
  const timestamp = parseInt(hex, 16);
  return new Date(timestamp);
}

// Example:
// extractTimestamp("018e5e5e-5e5e-7xxx-xxxx-xxxxxxxxxxxx")
// Returns: Date object

Frequently Asked Questions

What is UUID v7?

UUID v7 is a new UUID format defined in RFC 9562 that combines a Unix timestamp (milliseconds) with random data. UUIDs are time-ordered, making them ideal for database primary keys as they maintain chronological order and improve index performance.

Why is UUID v7 better for databases?

UUID v7 improves database performance because its time-ordered nature means new records are always appended to the end of B-tree indexes. With UUID v4, random values cause index page splits and fragmentation, leading to slower writes and larger indexes.

Should I migrate from UUID v4 to v7?

Consider migrating if you use UUIDs as database primary keys and experience performance issues. UUID v7 offers better write performance and smaller indexes. However, UUID v4 is fine for most applications and has wider library support.

Can UUID v7 timestamp be extracted?

Yes, UUID v7 contains a Unix timestamp in the first 48 bits. You can extract the creation time from any UUID v7. This is useful for sorting, auditing, and debugging but means UUID v7 leaks timing information.

Copied to clipboard!