← ALL BLOG POSTS

UUID v4 vs v7: Which One Should You Actually Use?

UUID v4 is random; UUID v7 is time-ordered. That one difference decides whether your database index stays fast or slowly fragments. Learn to read both formats, decode a v7 timestamp by hand, and pick the right version for primary keys, public IDs, and tokens.

by AI Trend WireDaily AI tools, news and tested workflows — the team behind the KRIYANO blog.

If you have ever generated a UUID, it was almost certainly a version 4: 122 bits of pure randomness, no information encoded, globally unique by brute-force probability. It works everywhere, and for most uses it is still the right choice.

But in May 2024, RFC 9562 standardised a new version — UUID v7 — that keeps the same 128-bit format while encoding a timestamp in the leading bits. The result is an ID that is still globally unique, but also sorts by creation time. That single change fixes the one real weakness of v4: what it does to your database index.

Here is how to tell them apart, how to decode one by hand, and the simple rule for choosing.

How to read any UUID

Every UUID is 32 hex characters in a 8-4-4-4-12 pattern. Two characters tell you the version:

  • The first character of the third group is the version. 4 means v4, 7 means v7.
  • The first character of the fourth group is the variant. For both v4 and v7 it is 8, 9, a, or b.

Compare these two real examples:

v4: bcdd6aaa-b400-4fb2-83f3-3c67f6920b61
v7: 01a236d7-2880-75f5-bb5c-ab3d7dc98cc4

The v4 has a 4 starting the third group (4fb2); the v7 has a 7 (75f5). That is the whole identification trick — you can spot the version of any UUID in a log file at a glance.

What is inside a UUID v4

A v4 UUID is 122 bits of cryptographically random data, plus 6 fixed bits for the version and variant. Nothing in it means anything: no timestamp, no machine, no sequence. The chance of two randomly generated v4 UUIDs colliding is about 1 in 5.3×10³⁶, so for practical purposes you can generate them anywhere — on a laptop, in a browser, on ten servers at once — without coordinating.

That randomness is also its only flaw. A database index is a B-tree, and B-trees love keys that arrive in order. Every new v4 primary key lands at a random position in the index, so the database constantly splits pages, rebalances the tree, and touches pages scattered across disk instead of appending to the end. At small scale you will never notice. At millions of rows you will: index pages sit half-empty, inserts touch more disk, and your buffer cache stops helping because the working set is the whole index instead of its tail. Buildkite reported a 50% drop in write-ahead log generation after switching from random UUIDs to time-ordered ones, and community benchmarks of 10-million-row inserts in PostgreSQL put v4 roughly 20–40% slower than sequential IDs.

What is inside a UUID v7

A v7 UUID keeps the same shape, but the first 48 bits are a Unix timestamp in milliseconds:

Bits Field Value
48 unix_ts_ms Unix timestamp, millisecond precision
4 ver 0111 (7)
12 rand_a Random data (or sub-millisecond precision)
2 var 10 (RFC 9562 variant)
62 rand_b Random data

That is 74 random bits in total — fewer than v4’s 122, but still far more than enough: two IDs generated in the same millisecond would need to collide on all 74 bits. Because the timestamp occupies the most significant bits, v7 values sort lexicographically by creation time. New rows append to the end of the index, almost like an auto-incrementing integer, while staying globally unique and generatable on any machine without coordination.

Decode a v7 by hand

Take the v7 example from above:

01a236d7-2880-75f5-bb5c-ab3d7dc98cc4

The first 12 hex characters are the timestamp: 01a236d72880. Convert that from hex to decimal and you get 1796216400000 — a Unix timestamp in milliseconds, which is 2026-12-02 08:00:00 ET. You can check that yourself with the Kriyano Timestamp Converter, or in one line of code:

uuid_v7 = "01a236d7-2880-75f5-bb5c-ab3d7dc98cc4"
ts_ms = int(uuid_v7.replace("-", "")[:12], 16)
print(ts_ms)  # 1796216400000
const uuidV7 = "01a236d7-2880-75f5-bb5c-ab3d7dc98cc4";
const tsMs = parseInt(uuidV7.replaceAll("-", "").slice(0, 12), 16);
console.log(new Date(tsMs).toISOString()); // 2026-12-02T13:00:00.000Z

That is the real debugging payoff of v7: the creation time of any row is sitting in its ID. No separate created_at column needed to answer “when was this created?”, and sorting by ID is sorting by time.

Generating v7 in practice

Support arrived fast after standardisation. PostgreSQL 18 has native uuidv7():

CREATE TABLE orders (
  id uuid PRIMARY KEY DEFAULT uuidv7(),
  ...
);

Most UUID libraries now support v7 too — the uuid package for JavaScript, google/uuid for Go, and the uuid crate for Rust among them. If your stack does not have one yet, the construction is simple enough to write by hand:

function uuidv7() {
  const ts = BigInt(Date.now());
  const rand = new Uint8Array(10);
  crypto.getRandomValues(rand);
  const b = new Uint8Array(16);
  b[0] = Number((ts >> 40n) & 0xffn); b[1] = Number((ts >> 32n) & 0xffn);
  b[2] = Number((ts >> 24n) & 0xffn); b[3] = Number((ts >> 16n) & 0xffn);
  b[4] = Number((ts >> 8n) & 0xffn);  b[5] = Number(ts & 0xffn);
  b[6] = 0x70 | (rand[0] & 0x0f); b[7] = rand[1];
  b[8] = 0x80 | (rand[2] & 0x3f); b[9] = rand[3];
  for (let i = 0; i < 6; i++) b[10 + i] = rand[4 + i];
  const h = [...b].map((x) => x.toString(16).padStart(2, "0")).join("");
  return `${h.slice(0, 8)}-${h.slice(8, 12)}-${h.slice(12, 16)}-${h.slice(16, 20)}-${h.slice(20)}`;
}

One honest note: the Kriyano UUID Generator currently generates v4 UUIDs in your browser — handy for the cases below, but not a v7 source. Use your database or a library for v7 until the tool adds it.

When v4 still wins

Time-ordering is not free. A v7 leaks the approximate creation time of whatever it identifies, just like an auto-incrementing integer does. That rules it out in three cases:

  • Public IDs. If the ID appears in a URL, an API response, or a shared link, v7 tells anyone roughly when the record was created. Competitors can estimate your growth; attackers can narrow brute-force windows. Use v4 (or an opaque random token) for anything user-facing.
  • Short-lived tokens and nonces. Session tokens, password-reset links, and one-off nonces do not live in a database index, so v7 buys nothing and the timestamp is pure information leakage. Random v4 is the right default.
  • Schemas you do not control. Third-party systems, legacy databases, and libraries that only generate v4 are not worth fighting. v4 works fine everywhere; v7’s advantage only matters where you own the table.

There is also a v6 in RFC 9562 — a reordered v1 for systems already using v1 timestamps that need sortability. If you are not already on v1, ignore it; v7 is the version for new work.

The decision, in one table

UUID v4 UUID v7
Random bits 122 74
Sortable by time No Yes
Database index performance Poor at scale Near-sequential
Reveals creation time No Yes (approx.)
Best for Public IDs, tokens, nonces Primary keys, event logs, audit trails
Standard RFC 4122 RFC 9562 (2024)
Support Universal Growing, now in Postgres 18 and major libraries

The rule of thumb: if the ID is a database primary key in a table that grows over time, and you control the schema, use v7. For everything else — public identifiers, short-lived tokens, anything where the creation time should stay private — v4 is still the right default.

What about your existing v4 keys?

Do not rewrite them. Migrating a primary key across tables, foreign keys, and application code is a real project with real downtime risk, and the payoff only matters at a scale most tables never reach. Use v7 for new tables, and let the old ones keep working. A column can hold both versions happily — the database does not care, and your future self will thank you for not doing a pointless migration.

One last distinction worth keeping straight: an ID and a credential are different things. A UUID identifies a record; it is not a secret and not a token. If the string you are handling is actually a credential — something that grants access — the safety rules are different, as our guide to decoding a JWT safely explains. Never treat a UUID as a password, and never put a real token where an ID belongs.

uuiddatabasesdeveloper-tools