A UUID v4 has 122 random bits, not 128 — and that is the least of it

Six of the 128 bits are not random, and you can prove it in twenty lines

A UUID v4 is usually described as “128 random bits”. It is not. The specification pins the version and variant fields, and the rest is random. Rather than take that on authority, generate 200,000 of them and count how often each bit position comes out as 1:

import { randomUUID } from 'node:crypto';

const N = 200000, bits = new Array(128).fill(0);
for (let i = 0; i < N; i++) {
  const hex = randomUUID().replace(/-/g, '');
  for (let b = 0; b < 128; b++) {
    const nib = parseInt(hex[b >> 2], 16);
    if ((nib >> (3 - (b & 3))) & 1) bits[b]++;
  }
}

Result: exactly 6 positions are constant across all 200,000 samples, leaving 122 that vary.

Bit positions Fixed value Meaning
48, 49, 50, 51 0100 version = 4
64, 65 10 variant = RFC 4122

The remaining 122 bits came out as 1 between 49.73% and 50.25% of the time — a tight band around a half, which is what you want to see and what you should check before trusting any generator.

Those six bits are why a v4 UUID always has a 4 as the first character of the third group, and one of 8, 9, a or b as the first character of the fourth:

f81d4fae-7dec-41d0-a765-00a0c91e6bf6
                   ↑    ↑
              version  variant

So the keyspace is 2¹²², not 2¹²⁸. That is a factor of 64 smaller — and, as the next section shows, completely irrelevant.

The collision maths, and the approximation that stops working

With 122 random bits, the birthday approximation for a small probability is p ≈ n² / 2¹²³:

UUIDs generated Approximate collision probability
10⁶ 9.40 × 10⁻²⁶
10⁹ 9.40 × 10⁻²⁰
10¹² 9.40 × 10⁻¹⁴

A trillion UUIDs — more than any single system will ever mint — carries a collision probability of about one in ten trillion. For practical purposes v4 collisions are not a risk worth engineering against, and the 122-versus-128 distinction changes nothing about that conclusion.

One caveat on the formula, because it is easy to push past its validity. That approximation only holds while p is small. Feed it n = 2.71 × 10¹⁸ and it returns 0.691, which is wrong in the sense of being past where the approximation is meaningful — the true 50% point is around n = 1.1774 × √(2¹²²) ≈ 2.7 × 10¹⁸. The approximation and the real curve agree on the order of magnitude there and nowhere beyond it. If you ever need a number near the bound, use the exact expression rather than extending the shortcut.

The honest summary: v4 collisions are a solved problem. Stop thinking about them. The reason v4 causes trouble in practice has nothing to do with uniqueness.

Where it actually hurts: every insert lands somewhere different

Use a v4 UUID as a primary key and you have chosen a value with no relationship to insertion order. In a B-tree index — which is what a primary key is in MySQL InnoDB, SQL Server and most others — that has a consequence you can simulate without a database.

Model an index as pages holding 100 keys each. Insert 100,000 keys and count how many distinct pages have been touched after each 10,000:

After inserts Random (v4) keys Sequential keys
10,000 1000 100
20,000 1000 200
50,000 1000 500
100,000 1000 1000

There are 1000 pages in total. Random keys have touched all 1000 of them within the first 10,000 inserts — 10% of the data has already dirtied 100% of the index. Sequential keys touch 100 pages and grow linearly, because each insert goes at the end, next to the previous one.

That is a simulation of page distribution, not a database benchmark — no buffer pool, no WAL, no fsync, no page splits. It demonstrates one specific mechanism and should not be read as a throughput prediction. But that mechanism is the one that matters, and it produces three real costs:

Write amplification. A page must be read, modified and written. If a page holds 100 rows and you touch it once per 100 inserts, the cost amortises. If every insert lands on a different page, each one is a separate read-modify-write of a full page.

Buffer pool thrash. Sequential keys keep the working set to the few pages at the end of the index, which stay cached. Random keys mean the hot set is the entire index. Once the index exceeds RAM, insert performance falls off a cliff — and it falls off precisely when the table has grown enough to matter.

Page splits and fragmentation. Appending at the end fills pages neatly. Inserting into the middle splits pages that are already full, leaving both halves partly empty, so the index grows larger than its data warrants and the fill factor degrades over time.

None of this shows up in development, because development tables fit in memory. It shows up in production, after months, as “inserts got slow and nobody changed anything”.

The storage cost nobody mentions

Beyond index behaviour, there is a plain size cost that compounds through every secondary index.

A UUID is 16 bytes. A 64-bit integer is 8. That 2× sounds mild until you remember where primary keys end up: in InnoDB, every secondary index stores the primary key as its row pointer, so the primary key’s width is paid again in each secondary index.

Stored as a string rather than binary it is worse. The canonical form is 36 characters — 32 hex digits plus 4 hyphens. As CHAR(36) that is 36 bytes against 8, a 4.5× increase, repeated in every secondary index, and every comparison is a 36-byte string comparison rather than a single integer compare. If you are going to use UUIDs as keys, storing them as BINARY(16) rather than CHAR(36) is the single cheapest improvement available.

What to do instead, in order of how much you have to change

Keep a sequential primary key and put the UUID beside it. An auto-increment or identity column as the clustered key, the UUID as a unique secondary column exposed in your API. The index keeps its locality, external identifiers stay unguessable, and the only cost is one extra unique index. This requires no new dependency and no new ID format, which is why it is first.

Use a time-ordered UUID instead. UUID v7 puts a 48-bit millisecond timestamp in the high bits, so values generated close in time sort close together — the index locality of sequential keys with the decentralised generation of UUIDs. The same reasoning drives ULID and Snowflake-style IDs. The UUID generator on this site produces v4 only, which is the right default when the output is an opaque identifier rather than a database key, and the wrong one if you are about to make it a clustered primary key.

Be aware of what time-ordering gives away. A v7 value leaks its creation time, and two of them reveal how far apart two records were created. For a public identifier that may be exactly what you did not want — the standard reason to use a random identifier in the first place is to stop people enumerating or timing your records. This is a genuine trade, not a free upgrade: v7 buys index locality by giving up the opacity v4 had.

The short version

Measured on Node v22.17.1 / macOS 26.5.2 using crypto.randomUUID(). The bit-distribution check is worth running against whatever generator you actually use — a generator seeded from a weak source still produces values that look like UUIDs, and the 49.73%–50.25% band above is the cheapest way to notice that it does not behave like one.

UUID GeneratorGenerate RFC 4122 version 4 UUIDs online, in bulk, using your browser's cryptographic random source. Also validates and identifies existing UUIDs.Open the tool →