UUID v4 or v7: which one for your primary keys
Both are 128 bits and look alike, but they hold different things: v4 is all random, v7 starts with its creation time. That difference decides how your database indexes behave and what anyone can read from your IDs.
The bit layout of v4 and v7
A UUID is a 128-bit number written as 32 hexadecimal digits in groups of 8-4-4-4-12. RFC 9562 (May 2024), which replaced RFC 4122, defines the two versions as follows. Bit 0 is the leftmost bit.
| Bits (length) | v4 | v7 |
|---|---|---|
| 0–47 (48) | random | Unix time (ms) |
| 48–51 (4) | version 0100 | version 0111 |
| 52–63 (12) | random | random/counter |
| 64–65 (2) | variant 10 | variant 10 |
| 66–127 (62) | random | random |
So v4 carries 122 random bits, while v7 carries 48 bits of time and 74 bits for the rest. In the text form, the first digit of the third group is the version, and the first digit of the fourth group is always 8, 9, a or b, because the two variant bits are 10. The time in a v7 can be read straight from the first 12 digits:
01a1fbd4-f500-7c3a-9e21-5b8d40f1a6c7
0x01a1fbd4f500 = 1795226400000 ms
= 2026-11-21 02:00:00 UTC
= Nov 20, 2026 9:00 PM EST
That number is a Unix timestamp in milliseconds; the Unix timestamp guide explains how to read one.
Why v7 helps database indexes
Primary keys normally live in a B-tree index, which keeps keys in sorted order. In MySQL's InnoDB, the table rows themselves are stored in primary key order. RFC 9562 section 2.1 names the problem with v4: values created in succession are not close to each other in the index, so each new row lands on a random page. Full pages split, half-empty pages multiply, and the part of the index that has to stay in memory approaches the whole index.
A v7 generated later is always larger than one generated in an earlier millisecond, so new rows go to the right-hand edge of the index, as with an auto-increment number. The difference is that the application can create the ID before it talks to the database, for example to put it on a queue or use it as an idempotency key. Byte order and lowercase hex text order are the same, so sorting the text form works too.
Within one millisecond, the order depends on the implementation. RFC 9562 section 6.2 offers three methods: a counter after the timestamp, random data that also acts as a counter, or extra sub-millisecond precision. Python uses a 42-bit counter, PostgreSQL uses a sub-millisecond fraction, and the Node.js documentation says randomUUIDv7() is not guaranteed to be strictly increasing.
Collision odds
For n version 4 UUIDs, the chance that any two are equal is roughly n² ÷ (2 × 2122):
| Number of v4 UUIDs | Chance of a duplicate |
|---|---|
| 1 million | 9.4 × 10−26 |
| 1 billion | 9.4 × 10−20 |
| 1 trillion | 9.4 × 10−14 |
| 2.7 × 1018 | 50% |
A v7 can only collide with another v7 from the same millisecond. With 74 random bits, 1,000 UUIDs per millisecond from separate generators give odds of about 2.6 × 10−17 per millisecond, or roughly 8 × 10−7 after a full year at that rate. Generators that spend some bits on a counter have fewer random bits, but the counter rules out collisions within that generator. All of this assumes the random bits come from a cryptographically secure generator, as RFC 9562 section 6.9 recommends. UUIDs built with Math.random don't qualify.
When not to expose a v7
Anyone who sees a v7 can read its creation time to the millisecond, as in the example above. On account, order or document IDs that appear in public URLs, that reveals when an account was opened or when a purchase happened. RFC 9562 section 8 calls embedded timestamps a very small attack surface, and adds that UUIDs should not be assumed hard to guess and must not be used as security capabilities, meaning identifiers whose mere possession grants access. A sensible split: v7 as the internal primary key, v4 for the ID you show outside. Password reset and invitation links need a dedicated random token, not a UUID.
Generating UUIDs in code
JavaScript. In browsers, crypto.randomUUID() is only available on HTTPS pages. On Node.js 14.17 to 18, import it from node:crypto, because the global crypto object only became available without a flag in Node.js 19.
// v4: browsers (HTTPS), Node.js 19+
crypto.randomUUID();
// v7: Node.js 24.16+ or 26.1+
import { randomUUIDv7 }
from 'node:crypto';
randomUUIDv7();
// v7 in browsers: npm package uuid 10+
import { v7 } from 'uuid';
v7();
Python. uuid7() arrived in Python 3.14, and the time attribute of a v7 holds Unix milliseconds.
import uuid
uuid.uuid4()
uuid.uuid7() # Python 3.14+
uuid.uuid7().time # Unix milliseconds
PostgreSQL. Store IDs in a uuid column (16 bytes), not as 36-character text. gen_random_uuid() has been in core PostgreSQL since version 13, uuidv7() since version 18. uuid_extract_timestamp() appeared in version 17 for v1 only, and version 18 added v7.
SELECT gen_random_uuid(); -- v4, PG 13+
SELECT uuidv7(); -- PG 18+
CREATE TABLE orders (
id uuid PRIMARY KEY DEFAULT uuidv7(),
total numeric NOT NULL
);
SELECT uuid_extract_timestamp(id)
FROM orders; -- v7: PG 18+
Generating and inspecting UUIDs on swift.id
- Open the UUID Generator & Validator and choose Version 4 (random) or Version 7 (time-ordered).
- Fill in How many (1-1000), then set the format if needed: Uppercase, No hyphens or Braces { }. A thousand v7 UUIDs made in one go still come out in order, because the tool uses a 12-bit counter.
- Press Copy or Download .txt, or Generate new to replace them all.
- Paste any UUID into Inspect a UUID to see its version and variant and, for v1, v6 and v7, its creation time, including the Unix milliseconds you can check in the Unix Timestamp Converter.
Questions
Can v4 and v7 live in the same column?
uuid column accepts every version. Existing rows stay scattered across the index; only new rows go in order.Is a v7 easier to guess than a v4?
Why aren't v7 UUIDs from two servers perfectly ordered?
How much space does a UUID take in the database?
uuid type stores 16 bytes. As text, the standard 36-character form needs more than twice that, and its index grows with it.