Skip to main content

UUID v7 Generator

Generate UUIDv7 — time-ordered, sortable identifiers defined by RFC 9562. Built for modern database primary keys, distributed systems, and event logs.

Your V7 UUID

V7
019e0899-00e7-72bc-873c-485647607b48

What is UUID v7?

UUID v7 is one of three new UUID versions introduced by RFC 9562 (May 2024), which obsoletes RFC 4122. While v4 places randomness in all available bits and v6 reorders the legacy v1 layout, v7 was designed from the ground up for the most common modern need: a unique identifier that also sorts by the time it was created.

A UUID v7 looks like 01890a5d-ac96-774b-bcce-b302099a8057. The third group always begins with 7 (the version nibble). Two v7 UUIDs generated milliseconds apart will sort in creation order — a property v4 fundamentally cannot provide.

UUID v7 bit layout

UUID v7 layout (RFC 9562 §5.7)
 0                   1                   2                   3
 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                           unix_ts_ms                          |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|          unix_ts_ms           |  ver  |       rand_a          |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|var|                        rand_b                             |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                            rand_b                             |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

48 bits  unix_ts_ms   - milliseconds since 1970-01-01 UTC
 4 bits  ver          - version, always 0111 (= 7)
12 bits  rand_a       - random
 2 bits  var          - variant, always 10
62 bits  rand_b       - random

Why UUID v7 wins on database performance

Most relational databases store rows sorted by primary key in a B-tree (Postgres uses heap + index, MySQL InnoDB uses a clustered index — the analysis applies to both). When the primary key is a random v4 UUID, every insert lands on a random page, evicting cold pages from the buffer pool and forcing rewrites that dirty those pages. As the table grows beyond RAM, write performance collapses.

A v7 UUID's leading bits change roughly once per millisecond and monotonically increase, so consecutive inserts almost always land on the rightmost leaf page — the same hot-page pattern an auto-increment BIGINT enjoys. Public benchmarks from EnterpriseDB, Maciej Walkowiak, and others have shown 2-10x throughput improvements for bulk inserts when switching v4 → v7 on indexed tables that have outgrown RAM.

When NOT to use UUID v7

UUID v7 leaks creation time. Anyone with a v7 UUID can extract the millisecond timestamp the value was generated. That makes v7 a poor fit for:

  • Security tokens. Use UUID v4 or a dedicated 256-bit token format.
  • Public IDs that should not reveal ordering. A customer seeing two v7 IDs can determine which user signed up first.
  • Capabilities or share links. Anywhere unguessability matters more than sortability.

How to generate a UUID v7 in your code

JavaScript / TypeScript
import { v7 as uuidv7 } from 'uuid';

const id = uuidv7();
// → '01890a5d-ac96-774b-bcce-b302099a8057'
npm install uuid — version 9+ supports v7.
Python
# Python 3.13+ has uuid7() built-in:
import uuid
id = uuid.uuid7()

# Earlier Python: pip install uuid7
from uuid7 import uuid7
id = uuid7()
Java
// JDK 21+ proposal JDK-8357251 adds UUID.randomUUIDv7().
// For now use a library like com.fasterxml.uuid:java-uuid-generator:
import com.fasterxml.uuid.Generators;
UUID id = Generators.timeBasedEpochGenerator().generate();
Go
import "github.com/google/uuid"

id, err := uuid.NewV7()
if err != nil { /* ... */ }
fmt.Println(id.String())
github.com/google/uuid added v7 in 2024.
Rust
use uuid::Uuid;

let id = Uuid::now_v7();
println!("{}", id);
Add `uuid = { version = "1", features = ["v7"] }` to Cargo.toml.
PostgreSQL
-- Postgres 18+:
SELECT uuidv7();

-- Earlier versions: generate in app, or use:
CREATE EXTENSION IF NOT EXISTS pg_uuidv7;  -- third-party
SELECT uuid_generate_v7();
Use as primary key default: id uuid PRIMARY KEY DEFAULT uuidv7().

Frequently asked questions

UUID v7 is a 128-bit identifier defined by RFC 9562 (May 2024). It encodes a 48-bit Unix epoch millisecond timestamp in its first 48 bits, followed by 4 version bits, 12 random bits, 2 variant bits, and 62 random bits. Because the timestamp is most-significant, sorting v7 UUIDs lexically also sorts them by creation time.