Calculating ID collision risk with the birthday paradox
In one line: The collision formula, IDs needed per bit width, and the real numbers for 1,000 IDs per second over ten years.
A collision means the same value appears twice. With a value space of B = 2^b and N independent, uniform draws, the chance of at least one repeat is approximately:
p ≈ 1 − exp(−N² / 2B) 50% collision: N ≈ 1.177 × √B 1% collision: N ≈ 0.141 × √B 10⁻⁹ collision: N ≈ 4.5 × 10⁻⁵ × √B
The √B baseline is what intuition misses. At 122 bits, B is about 5.3 × 10^36 and a 50% collision needs roughly 2.7 × 10^18 IDs. The right mental model is not “128 bits never collide” but “risk grows with the square of the volume”.
IDs needed per bit width
| Random bits | 50% collision | 1% collision | 10⁻⁹ collision |
|---|---|---|---|
| 32 | 7.7 × 10^4 | 9.3 × 10^3 | 2.9 × 10^3 |
| 62 | 2.5 × 10^9 | 3.0 × 10^8 | 9.6 × 10^4 |
| 74 | 1.6 × 10^11 | 1.9 × 10^10 | 6.1 × 10^6 |
| 80 | 1.3 × 10^12 | 1.6 × 10^11 | 4.9 × 10^7 |
| 122 | 2.7 × 10^18 | 3.3 × 10^17 | 1.0 × 10^14 |
| 128 | 2.2 × 10^19 | 2.6 × 10^18 | 8.3 × 10^14 |
A realistic scenario
At 1,000 IDs per second for ten years, N ≈ 3.16 × 10^11. The risk by format is:
| Format | Random bits | Collision chance over ten years |
|---|---|---|
| UUIDv7 (without the counter) | 74 | about 93% |
| ULID | 80 | about 4.0% |
| UUIDv4 | 122 | about 9.3 × 10^-15 |
| KSUID | 128 | about 1.1 × 10^-16 |
The 74 bits of UUIDv7 are fine at tens or hundreds per second, but at 1,000 per second for a decade the risk climbs to 93%. In practice, node prefixes, shard keys and UNIQUE constraints detect or avoid collisions; the mathematical probability and the operational defence are different things.
Assumptions behind the numbers
- Independent and uniform: the random source is assumed perfect. A CSPRNG is close enough, but reused seeds or cloned VMs break the assumption.
- N is cumulative: it is the total generated in the period. A million a day for ten years is 3.6 billion.
- Time-ordered IDs use the same formula: the random part of UUIDv7 and ULID is still random, so the birthday effect applies to bursts in the same millisecond or second.
- Collisions must be detected: use a UNIQUE index or a lookup before insert, and generate a new ID when one collides.
The calculator on this page takes bits, rate and period and returns N, the probability, and the IDs and time needed for each target probability. It uses BigInt so the 122-bit scale is shown accurately.
Decode an ID you just made and inspect its bit structure.
Open ULIDLabFrequently asked questions
Is a 1% collision chance safe?
No. One percent means one collision in a hundred runs. Production systems usually target 10^-9 or lower, and places where a duplicate is an incident, such as payments or bookings, need a lower target plus UNIQUE constraints.
What happens if I double the bits?
Doubling B roughly halves the collision probability for the same N. But probability grows with N², so as volume grows, more bits alone may not be enough.
Is 74 bits enough for UUIDv7?
At hundreds per second, yes. If you accumulate thousands per second for years, absorb collisions with node prefixes, shard keys and UNIQUE constraints, or choose a format with a larger random field. Run your real rate and period through the calculator.
Sources
Read next
- UUIDv4 vs UUIDv7: which one, and whenBit layout, sorting habits, index impact and the pitfalls of monotonic mode, compared for real systems.
- ULID structure: why 26 characters sort by timeThe 48-bit timestamp, 80 random bits, Crockford Base32, monotonic ULIDs and the details that trip people up.
- NanoID: designing entropy with alphabet and lengthHow the default 64-character alphabet and 21 characters produce 126 bits, where short IDs become risky, and the traps in custom alphabets.