No. Yes. It depends on what you mean by 'guaranteed.'
UUIDs are not guaranteed unique in the mathematical sense. They are probabilistically unique — the probability of a collision is so small that it is effectively zero for any practical system. But it is not actually zero. And if you generate enough UUIDs, it becomes non-negligible. Here is the math.
What 'Unique' Actually Means
A UUID v4 has 122 bits of randomness (the other 6 bits are fixed for the version and variant). That means there are 2^122 possible v4 UUIDs — about 5.3 × 10^36. That is a 5 followed by 36 zeros.
For comparison, the observable universe has about 10^80 atoms. The number of possible UUIDs is smaller, but still absurdly large.
Uniqueness is not about the total number of UUIDs, though. It is about the birthday paradox: how many UUIDs do you need to generate before two of them match?
The Birthday Paradox for UUIDs
The birthday paradox says: in a group of 23 people, there is a 50% chance two share a birthday. The same math applies to UUIDs.
For UUID v4 with 122 bits of randomness, the 50% collision threshold is approximately 2^61 UUIDs. That is about 2.3 × 10^18 — 2.3 quintillion UUIDs.
To put that in perspective: - Generating 1 billion UUIDs per second, it would take 73 years to reach the 50% threshold. - If every person on Earth generated 1 billion UUIDs per second, you would still need thousands of years. - The threshold is so high that no real system will ever approach it.
The Probability at Realistic Scales
Let us look at realistic numbers. If you generate 103 trillion UUIDs (10^14), the probability of at least one collision is about 1 in 10^18. That is one in a quintillion.
| UUIDs generated | Collision probability | |----------------|----------------------| | 103 (10^3) | 10^-33 | | 103 million (10^7) | 10^-25 | | 103 billion (10^11) | 10^-17 | | 103 trillion (10^14) | 10^-9 | | 2.3 quintillion (2×10^18) | 50% |
At any scale a real system will ever see, the collision probability is indistinguishable from zero.
Why UUIDs Are Not 'Guaranteed' Unique
The RFC 4122 specification does not guarantee uniqueness. It says UUIDs are 'unique across space and time' with 'very high probability.' The spec is careful with its language because:
1. **v4 UUIDs are random.** Two random numbers can match. The probability is low, but it is not zero. 2. **v1 UUIDs depend on the clock.** If two machines have the same MAC address (rare but possible with virtualization) and their clocks are wrong, they can generate the same v1 UUID. 3. **Bad random number generators.** If your CSPRNG is broken, your UUIDs are not random, and collisions become likely. This has happened — some Linux kernels had a bug where urandom returned predictable output early in boot.
The Real Collision Risk: Bad RNG
The mathematical collision risk is negligible. The practical collision risk is not from math — it is from broken random number generators.
Real-world UUID collisions have happened. In 2015, a bug in the V8 JavaScript engine caused Math.random() to produce predictable output. Systems that used Math.random() to generate UUIDs (instead of crypto.randomUUID()) produced collisions.
The lesson: use a cryptographically secure random number generator. In Node.js, that is crypto.randomUUID(). In Python, uuid.uuid4() uses os.urandom(). In Java, UUID.randomUUID() uses SecureRandom. These are safe.
Do not use Math.random(), random.random() without a secure source, or any custom RNG to generate UUIDs. That is where real collisions come from.
What Happens If a Collision Occurs
If two UUIDs collide, the consequences depend on where they are used: - **Database primary key**: The second insert fails with a constraint violation. Your system errors. This is the best case — the collision is caught. - **API key**: Two different users get the same API key. One user can access the other's account. This is a security incident. - **Session token**: Two sessions share a token. One user sees the other's session. This is a security incident. - **File storage**: Two files get the same name. One overwrites the other. Data loss.
For security-sensitive uses, add a uniqueness check at the database level (a unique constraint). The UUID is your first line of defense. The constraint is your second.
How Unique Is a UUID, Really?
Unique enough. For any system that will ever exist in practice, a v4 UUID generated with a secure RNG will not collide. The probability is lower than the probability of a cosmic ray flipping a bit in your server's memory and causing a data corruption. You do not engineer for cosmic ray bit flips (though some do). You do not need to engineer for UUID collisions.
But you should: - Use a secure RNG (never Math.random()) - Add unique constraints in your database as a safety net - Handle constraint violations gracefully (retry with a new UUID)
The v1 Collision Edge Case
v1 UUIDs have a different collision risk. They are time-based, so two UUIDs generated at the same time on the same machine should not collide — the spec includes a clock sequence that increments if the clock goes backward. But if two machines have the same MAC address (common in virtual machines that clone MACs), and their clocks are in sync, they can generate the same v1 UUID.
The fix: ensure unique MAC addresses, or use v4 for systems where you do not control the hardware.
Summary
UUIDs are not guaranteed unique. They are probabilistically unique, and the probability of collision at any realistic scale is indistinguishable from zero. The real risk is not the math — it is broken RNGs and v1 clock/MAC issues. Use v4 with a secure RNG, add database constraints, and sleep well.
Generate UUIDs with a secure RNG at our UUID generator. For the version differences, read UUID v1 vs v4 vs v5. For real-world use cases, see when to use UUIDs.