Sub topic 6
How likely is a collision? The birthday bound
This chapter assumes you know what a UUID v4 is (a random 128-bit identifier with 122 random bits), can read basic Python, and understand powers of two. By the end you can estimate the chance that random IDs collide and decide whether a given ID size is safe.
Why collisions arrive sooner than you expect
Random IDs need no coordination. Two nodes pick values independently and hope they never match. The question is how often they do.
A common mistake is to compare the number of IDs to the size of the space. With 122 random bits there are 2^122 possible values, so it feels like you need about 2^122 IDs before a clash. In fact the danger comes from pairs. With n IDs there are about n²/2 pairs, and any one pair might match.
This is the birthday problem. In a room of 23 people, the chance that two share a birthday is already above 50%, even though there are 365 possible birthdays.
The birthday bound
For n IDs drawn uniformly at random from a space of 2^b values, the probability p of at least one collision is approximately:
p ≈ n² / (2 · 2^b)
This holds while p is small, which is the case you care about for IDs. Each of the roughly n²/2 pairs matches with probability 1/2^b, and you add those chances up.
The 50% point is near n ≈ 1.17 · 2^(b/2). The square root is the key: a space of b bits gives you collision risk at about b/2 bits worth of IDs.
def collision_probability(n, bits):
# Birthday-bound approximation: p ~ n^2 / (2 * 2^bits), valid while p is small.
return n * n / (2 * 2 ** bits)
# A UUID v4 has 122 random bits.
print(collision_probability(10**9, 122)) # about 9.4e-20One billion UUID v4 values give a collision chance of about 9.4e-20. That is far below the rate of hardware faults, so you can treat it as zero for practical purposes.
Comparing ID sizes
The same function shows why size matters. Try it with different bit counts.
def collision_probability(n, bits):
return n * n / (2 * 2 ** bits)
for bits in (32, 64, 122):
for n in (10**6, 10**9):
p = collision_probability(n, bits)
print(f"{bits:>3} bits, {n:>13,} IDs: p = {p:.3g}")The results follow the formula directly:
| Random bits | 1 million IDs | 1 billion IDs |
|---|---|---|
| 32 | above 1 (the formula no longer applies; a collision is near certain) | above 1 (same) |
| 64 | about 2.7e-8 | about 0.027 |
| 122 | about 9.4e-26 | about 9.4e-20 |
For 32 bits the formula gives values above 1, which cannot be a probability. This is the "valid while p is small" limit in action. When the formula returns a number near or above 1, read it as "collisions are expected" and not as a precise value.
Where the bound does not protect you
The calculation assumes the random bits are uniform and independent. Real systems break this in a few ways:
- Weak random sources. A generator seeded from the clock, or one that is copied with the same state into several processes (for example after a fork or a VM snapshot restore), repeats values. The math then says nothing.
- Fewer random bits than you think. UUID v7 spends 48 bits on a timestamp and keeps only 74 random bits (after the 4-bit version and 2-bit variant fields in a 128-bit value). Collisions can only happen between IDs in the same millisecond, so the useful n is the number of IDs per millisecond, not the total.
- Truncation. Cutting a UUID to 8 or 12 characters for a short link shortens the space. Apply the formula to the bits you keep.
A practical rule
Pick a collision probability you can accept over the lifetime of the system, then solve for the bits. Doubling the number of IDs multiplies the risk by four, and each extra bit halves it. For a target of 1e-15 with 10^10 IDs you need 2^b above 10^20 / (2 · 10^-15), which is about 5 · 10^34, so at least 115 bits. This is why 122-bit UUIDs are comfortable and 64-bit random IDs are not, once you reach billions of rows.
If the required bit count is larger than you can afford, use a scheme that adds structure, such as a worker ID and a sequence, so that uniqueness is guaranteed by construction and not by luck.
Exercise
A service generates 64-bit random IDs and expects to create 4 billion of them (4 · 10^9). Use the birthday bound to estimate the collision probability, and say whether the result is acceptable.
Show answer
p ≈ n² / (2 · 2^64) = (1.6 · 10^19) / (2 · 1.845 · 10^19) ≈ 0.43. The formula is no longer very accurate at this size, but the exact form 1 - exp(-0.43) is about 0.35. Either way, a collision is likely, so 64 random bits are not acceptable. Use at least 122 random bits, or a structured scheme such as Snowflake.
Exercise
Why does a UUID v7 collision depend on IDs per millisecond and not the total number of IDs?
Show answer
The 48-bit timestamp prefix differs between IDs created in different milliseconds, so those IDs cannot match. Only IDs created within the same millisecond share a prefix and compete on the remaining 74 random bits.