All articles

UUID v4 vs UUID v7: choosing an ID for your database

· 3 min read

A UUID is a 128-bit identifier, usually written as 36 characters: 3f2b8c1e-9a4d-4e7b-b1c2-5d6e7f8a9b0c. Any system can create one without asking a central server, which is why they're popular as primary keys in distributed systems and APIs. Two versions dominate today: version 4, which is random, and version 7, which starts with a timestamp.

UUID v4: random

A v4 UUID is 122 random bits plus 6 bits that mark the version and variant. You can spot it by the 4 that starts the third group:

3f2b8c1e-9a4d-4e7b-b1c2-5d6e7f8a9b0c
              ^

Every modern runtime can generate one with a cryptographically secure generator:

crypto.randomUUID(); // browsers (secure contexts) and Node.js 19+; earlier Node: require("node:crypto").randomUUID()
import uuid
uuid.uuid4()
SELECT gen_random_uuid();  -- PostgreSQL 13+

Because the bits are random, v4 IDs reveal nothing about when or where they were made.

UUID v7: time-ordered

Version 7, standardized in RFC 9562, puts a 48-bit Unix timestamp in milliseconds at the front, followed by random bits:

0190a5b2-7c3e-7d41-9f2a-6b8c1d2e3f40
└─ ms timestamp ─┘ ^
                   version 7

IDs created later sort after IDs created earlier, both as bytes and as strings. That property is the whole point.

Generating v7 depends on your stack: PostgreSQL 18 adds a built-in uuidv7() function, Python 3.14 adds uuid.uuid7(), and in JavaScript the uuid package provides v7():

import { v7 as uuidv7 } from "uuid";
uuidv7(); // '0190a5b2-7c3e-7d41-9f2a-6b8c1d2e3f40' (example)

On older versions, use a library for your language.

Why ordering matters for indexes

Most databases store primary keys in a B-tree index. With random v4 keys, each new row lands at a random position in that index. The database has to load and modify pages scattered across the whole index, split pages that are full, and keep far more of the index in memory to stay fast.

With v7 keys, new rows go to the end of the index, like an auto-incrementing integer. Inserts touch the same few pages, which stay in cache, and pages fill completely before new ones are added.

How much this matters depends on scale:

  • A table with thousands or a few million rows that fits in memory: you're unlikely to notice.
  • Large, write-heavy tables where the index no longer fits in memory: random keys mean more disk reads per insert and a larger index, and the difference can be significant.

Measure on your own database and workload; published benchmarks vary widely with hardware and settings.

What each version reveals

UUID v4 UUID v7
Creation time Hidden Visible to the millisecond
Insert order in indexes Random Sequential
Sortable by creation No Yes
Standard RFC 9562 (and RFC 4122) RFC 9562
Native generation Everywhere Newer runtimes, or a library

The timestamp in v7 is a real trade-off. If your public URLs contain order or user IDs, a v7 ID tells anyone when that record was created, and a series of them can reveal how fast you're growing. For those, v4 or a separate public ID is safer.

Alternatives

Auto-increment integers

Smallest (4 or 8 bytes) and fastest, but they need the database to assign them, they leak your record count, and they make merging data from separate databases harder. Many systems use an integer primary key internally and a UUID for anything exposed in URLs.

ULID

ULIDs solve the same problem as v7: a 48-bit timestamp followed by randomness, written as 26 Crockford Base32 characters (01ARZ3NDEKTSV4RRFFQ69G5FAV). They predate v7, but they aren't UUIDs, so they don't fit a native uuid column. For new projects, v7 gives the same ordering in the standard format.

Snowflake-style IDs

64-bit IDs combining a timestamp, a machine ID and a sequence, as used by X (Twitter) and Discord. Compact and ordered, but you need to assign machine IDs and keep clocks sane.

Which should you use?

  • New primary keys in a busy table: UUID v7, stored in a native uuid column.
  • Public identifiers that shouldn't reveal timing: UUID v4.
  • Test data, fixtures, correlation IDs, idempotency keys: v4 is simplest and available everywhere.
  • Single database, internal only, maximum compactness: an auto-increment integer is still fine.

Whichever you pick, generate IDs with a cryptographically secure source. Math.random() isn't one, for the same reasons it shouldn't be used to generate passwords.

Frequently asked questions

Can two UUID v4 values collide?

In theory yes, but with 122 random bits the probability is negligible for any realistic number of IDs. A duplicate key error almost always means the same UUID was reused by a bug, not a collision.

Is UUID v7 standardized?

Yes. RFC 9562, published in 2024, defines versions 6, 7 and 8 and replaces RFC 4122.

Does UUID v7 reveal anything?

Its first 48 bits are the creation time in milliseconds, so anyone who sees the ID can tell when the record was created. Use v4 for IDs where that matters.

Should I store UUIDs as strings?

Use the database's native type where it exists, such as uuid in PostgreSQL. It takes 16 bytes instead of 36 and compares faster. In MySQL, BINARY(16) is the usual choice.

Further reading