Skip to main content

UUID Version 7 Generator

UUIDv7 • Time-Ordered (RFC 9562)
00000000-0000-0000-0000-000000000000
Enclose:

Generate Bulk UUIDs

Max 5,000
Formatting & Structure

Ready

No batch active
Modern Standard (2024)RFC 9562 Section 5.7

UUID Version 7 Technical Specification (RFC 9562)

UUIDv7 combines a 48-bit Unix timestamp (in milliseconds) with 74 bits of cryptographically random entropy and sequence data. It was standardized in RFC 9562 (published 2024) specifically to solve database index degradation and page fragmentation caused by traditional random UUIDv4 keys.

Bitfield Layout & Binary Structure

Detailed 128-bit byte distribution according to RFC standards.

Field NameBit RangePurpose & Description
unix_ts_ms48 bits (0-47)Big-endian unsigned Unix Epoch timestamp in milliseconds.
ver4 bits (48-51)Constant binary value 0111 (identifies Version 7).
rand_a12 bits (52-63)Random entropy or sub-millisecond sequence counter.
var2 bits (64-65)Constant binary value 10 (RFC 4122/9562 standard variant).
rand_b62 bits (66-127)Cryptographically secure pseudorandom entropy bits.

Advantages & Strengths

  • Naturally ordered by creation time; ideal for database clustered B-Tree indexes (PostgreSQL, MySQL, SQLite).
  • Eliminates random disk I/O, cache line eviction, and B-Tree page splits during high-volume inserts.
  • Includes 74 bits of entropy per millisecond, preventing collisions even across distributed microservices.
  • Human-readable timestamp can be decoded directly without secondary database lookup.
!

Considerations & Trade-offs

  • Leaks approximate creation time (millisecond resolution) if exposed in public URLs.
  • Requires monotonic counter tracking when generating multiple IDs within the exact same millisecond.

Best Recommended For

  • Primary keys in relational and document databases (PostgreSQL, MySQL InnoDB, CockroachDB, MongoDB).
  • Event streaming, audit logs, and message broker event IDs (Kafka, RabbitMQ, SQS).
  • Time-series and chronological ledger tracking.

When to Avoid

  • Security reset tokens or secret session cookies where timestamp must remain confidential.
  • Systems requiring sequential counter integers without any timestamp overhead.

Code Implementation Examples

Native Node.js / BrowserTypeScript / Node.js
// Browser / Node 22+ with Web Crypto
function generateUUIDv7(): string {
  const bytes = new Uint8Array(16);
  crypto.getRandomValues(bytes);
  const now = Date.now();

  bytes[0] = (now / 0x10000000000) & 0xff;
  bytes[1] = (now / 0x100000000) & 0xff;
  bytes[2] = (now / 0x1000000) & 0xff;
  bytes[3] = (now / 0x10000) & 0xff;
  bytes[4] = (now / 0x100) & 0xff;
  bytes[5] = now & 0xff;

  bytes[6] = (bytes[6] & 0x0f) | 0x70; // version 7
  bytes[8] = (bytes[8] & 0x3f) | 0x80; // variant

  return [...bytes].map((b, i) =>
    (i === 4 || i === 6 || i === 8 || i === 10 ? '-' : '') + b.toString(16).padStart(2, '0')
  ).join('');
}
Python (uuid6 / 3.14+)Python
import uuid6

new_id = uuid6.uuid7()
print(new_id) # e.g. 018e6a12-8921-71e4-8a4e-128a9b345678
PostgreSQL FunctionPostgreSQL
-- PostgreSQL 13+ native UUIDv7 generator
CREATE OR REPLACE FUNCTION uuid_generate_v7()
RETURNS uuid AS $$
DECLARE
  v_time numeric = extract(epoch FROM clock_timestamp()) * 1000;
  v_bytes bytea = gen_random_bytes(16);
BEGIN
  v_bytes = set_byte(v_bytes, 0, (v_time::bigint >> 40)::int);
  v_bytes = set_byte(v_bytes, 1, (v_time::bigint >> 32)::int);
  v_bytes = set_byte(v_bytes, 2, (v_time::bigint >> 24)::int);
  v_bytes = set_byte(v_bytes, 3, (v_time::bigint >> 16)::int);
  v_bytes = set_byte(v_bytes, 4, (v_time::bigint >> 8)::int);
  v_bytes = set_byte(v_bytes, 5, (v_time::bigint)::int);
  v_bytes = set_byte(v_bytes, 6, (get_byte(v_bytes, 6) & 15) | 112);
  v_bytes = set_byte(v_bytes, 8, (get_byte(v_bytes, 8) & 63) | 128);
  RETURN encode(v_bytes, 'hex')::uuid;
END;
$$ LANGUAGE plpgsql VOLATILE;

Frequently Asked Questions

Why switch from UUIDv4 to UUIDv7?
UUIDv4 inserts randomly across your entire B-Tree index, causing severe page splits and high disk fragmentation. UUIDv7 appends sequentially in chronological order, allowing up to 4x faster insertion throughput while retaining full 128-bit uniqueness.
Is UUIDv7 backwards-compatible with UUIDv4 columns?
Yes. Both UUIDv4 and UUIDv7 use the identical 128-bit canonical format (32 hex digits with 4 hyphens). You can store UUIDv7 identifiers in existing PostgreSQL UUID, MySQL BINARY(16), or SQLite TEXT/BLOB columns without schema migration.
Can two UUIDv7 values collide if generated in the same millisecond?
No. In addition to the millisecond timestamp, UUIDv7 incorporates 74 bits of cryptographic entropy and sub-millisecond sequencing, allowing billions of unique keys per millisecond without collision risk.