Skip to main content

Blog · Developer Tools

Why UUID v4 Collisions Almost Never Happen

Published · 2 min read

People hear "random" and expect collisions eventually. The numbers say otherwise: the universe ends first.

Every developer eventually wonders: if UUIDs are random, won’t two eventually match? The question deserves better than "trust me"; the arithmetic behind the answer is one of probability’s most satisfying party tricks.

What v4 actually contains

A UUID v4 packs 122 random bits (128 minus fixed version and variant markers) drawn from cryptographic randomness. That is 2^122 possible values: around 5.3 undecillion, a number with 36 zeros that exceeds grains-of-sand comparisons by embarrassing margins.

Randomness quality matters: cryptographic generators make every value effectively independent. Weaker randomness (counter-based, timestamp-heavy schemes from other ID families) changes the calculus, which is why "UUID" specifically means the v4 random flavour here.

The birthday paradox cuts both ways

Collisions arrive earlier than intuition suggests (the famous birthday paradox: 23 people yield 50% shared-birthday odds among 365 days). Applied to UUIDs, matching becomes 50% likely only near 2^61 generated IDs: about 2.7 quintillion.

For scale: generating a billion UUIDs per second, sustained since the Big Bang, would still be orders of magnitude short. The paradox makes collisions surprisingly probable in small pools and cosmically improbable in this one.

Engineering honesty: almost never is not never

Duplicate handling remains good practice for reasons beyond UUID maths: buggy generators, truncated storage (storing UUIDs as 32-bit ints guarantees grief), and manual data surgery all produce duplicates that pure chance never would.

Unique constraints cost nothing and catch implementation failures rather than probability failures. The professional stance: rely on the mathematics, constrain against the engineering.

When alternatives fit better

Random UUIDs trade sortability for independence; database indexes fragment under unordered inserts at extreme scale. Time-ordered variants (ULID, Snowflake-style IDs) sacrifice some collision headroom for chronological ordering that databases love.

Choosing between them is a systems decision, not a correctness one: v4’s collision odds are never the deciding factor, because they are never the bottleneck.

UUID v4 collisions live in the same probability neighbourhood as meteor strikes precisely aimed at your laptop: nonzero, newsworthy, and unworthy of engineering attention. The 122-bit buffer absorbs every realistic generation volume humanity can mount.

Generate freely, index uniquely anyway, and reserve worry for failure modes that actually visit: bad randomness sources and careless storage, which constraints catch and chance does not.

Frequently asked questions

Have there ever been reported UUID v4 collisions?
Documented cases trace to broken randomness (seeded PRNGs, virtualisation clock issues), not chance. With proper crypto randomness, no verified natural collision exists.
Are shorter IDs safe if I generate fewer?
They can be within bounded populations, but bounds drift as systems grow. UUIDs remove ceiling-watching from your job description for negligible storage cost.
All guides →
All questions →
All articles →