UUID v4 vs v7: Random IDs or Time-Ordered IDs

Both are 128-bit identifiers written as 36 characters. A v4 UUID is 122 random bits, so new values land anywhere in a sorted index. A v7 UUID starts with a 48-bit Unix timestamp in milliseconds followed by random bits, so values sort roughly by creation time. RFC 9562 recommends v7 over the older time-based v1 and v6.

v4 and v7 compared
UUID v4UUID v7
Made from122 random bits48-bit millisecond timestamp plus 74 random bits
Sort orderRandomRoughly by creation time
Reveals creation timeNoYes, to the millisecond
Built into browserscrypto.randomUUID()No, use a few lines of code or a library
Good forTokens, public IDs, anything that must not leak timingDatabase primary keys, logs, event IDs

Updated

Picking a UUID version is mostly a database question. Random v4 keys scatter inserts across an index, while v7 keys arrive in order, the same way an auto-increment number would, without needing a central counter. Generate both kinds below, then paste any UUID to see its version and, for v7, the moment it was made.

Generate and decode

Generate v4, Generate v7

Press a button to make a UUID. It is created in your browser and never sent anywhere.

Paste a UUID to decode

019a2b3c-4d5e-7f60-8a1b-2c3d4e5f6a7b

All eight UUID versions

UUID versions in RFC 9562
VersionBuilt fromNotes
1Gregorian time plus a node ID, often the MAC addressLegacy; prefer v7
2DCE Security version, not specified by RFC 9562Rare
3Name-based, MD5 hash of a namespace and a nameSame input, same UUID
4Random or pseudorandom bitsThe common default
5Name-based, SHA-1 hash of a namespace and a namePreferred over v3
6v1's timestamp reordered so values sort by timePrefer v7 for new systems
7Unix Epoch time in milliseconds plus random bitsSortable keys
8Custom layout chosen by the implementationExperimental or vendor formats

Code you can copy

UUID v4 in the browser or Node

Built in to current browsers on secure pages and to Node 19 and later through the global crypto object. Returns a 36 character string.

const id = crypto.randomUUID();
console.log(id); // e.g. "3f6c1b2a-9d4e-4c8b-a1f0-7e2d5b9c6a13"

UUID v7 without a library

Writes the current time into the first six bytes, then sets the version and variant bits. The rest stays random.

function uuidv7() {
  const b = crypto.getRandomValues(new Uint8Array(16));
  const ms = Date.now();
  for (let i = 0; i < 6; i++) b[i] = Math.floor(ms / 2 ** (8 * (5 - i))) & 0xff;
  b[6] = (b[6] & 0x0f) | 0x70; // version 7
  b[8] = (b[8] & 0x3f) | 0x80; // RFC 9562 variant
  const h = Array.from(b, (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);
}

Read the time from a v7 UUID

The first 12 hex digits are milliseconds since 1970, so parsing them gives a Date.

function v7Date(uuid) {
  const ms = parseInt(uuid.replace(/-/g, "").slice(0, 12), 16);
  return new Date(ms);
}

v7Date("019a2b3c-4d5e-7f60-8a1b-2c3d4e5f6a7b").toISOString();

Why ordering matters for databases

Most databases keep primary keys in a B-tree. When each new key is larger than the last, inserts go to the end of the tree and recently written pages stay in memory. Random keys force writes into pages all over the tree, which means more page splits and more disk reads as a table grows.

RFC 9562 lists this locality problem as one of its reasons for adding time-ordered versions, and v7 is the one it points new systems toward.

Reading the layout

In the text form, the 13th hex digit (the first of the third group) is the version, so a v7 UUID always has a 7 there and a v4 has a 4. The first digit of the fourth group is 8, 9, a or b for the standard variant.

For v7, the 12 digits before the version are the timestamp. Everything after the version digit, apart from the two variant bits, is random.

How it works

  1. Press a generate button to make a fresh v4 or v7 UUID in your browser.
  2. Copy it, or paste any UUID into the decoder box.
  3. The decoder reads the version digit and the variant bits, and for v7 turns the first 48 bits into a date and time.
  4. Generate a few v7 values in a row and notice how their first characters barely change.

Tips

Frequently asked questions

What is the difference between a GUID and a UUID?

None that matters in practice. RFC 9562 defines UUIDs and notes they are also known as GUIDs, Globally Unique Identifiers, the name Microsoft tools use. A GUID generated by .NET or SQL Server is a UUID in the same 36 character format.

Is UUID v7 an official standard?

Yes. RFC 9562, published in 2024, defines versions 6, 7 and 8 alongside the older ones and obsoletes RFC 4122, the document that described UUIDs before.

Is UUID v7 unique enough?

Each millisecond has 74 random bits to spread across, which is plenty for almost any system. Collisions would need enormous numbers of IDs created in the very same millisecond.

Can I get the creation time back from a v7 UUID?

Yes. The first 12 hex digits are the Unix time in milliseconds. Paste one into the decoder above to see it as a date, or parse those digits as a hexadecimal number yourself.

Should I switch existing v4 keys to v7?

Usually not. Both are valid UUIDs and can live in the same column, so new rows can use v7 while old rows keep their v4 values. Rewriting primary keys means rewriting every foreign key that points at them.

Related: UUID generator, Unix timestamp converter, Hash generator, Base64 encoder and decoder.

Start for free or see all free tools. We.Inc is free to start, with no card needed. AI building is on paid plans from $20 a month.

More in Guides

We.Inc is an AI-powered website builder you can resell under your own brand. Launch a branded client dashboard, bill on Stripe Connect, and deliver AI-generated websites in minutes. White-label plans start at $99 a month for 25 client sites, with a 7-day free trial and no per-site fees.

Product

Who It's For

Features

Resources

Company

View Sitemap