Are UUIDs Really Unique? Mathematical Explanation

Published:

Introduction

The question "Are UUIDs really unique?" is fundamental to understanding UUID reliability. While technically not guaranteed unique, UUIDs are designed to make collisions astronomically unlikely.

The Mathematics of UUID Uniqueness

UUID v4 (Random) Probability

UUID v4 uses 122 random bits (6 bits are reserved for version and variant). The total number of possible UUIDs is:

2^122 = 5.3 Ɨ 10^36

This is an astronomically large number - more than the number of atoms on Earth!

Collision Probability

The probability of a collision follows the birthday paradox. For n UUIDs generated:

  • Probability of at least one collision ā‰ˆ 1 - e^(-n²/2Ɨ2^122)
  • To have a 50% chance of collision, you'd need to generate approximately 2.71 Ɨ 10^18 UUIDs

Real-World Scenarios

How Many UUIDs Can Be Generated Safely?

  • 1 billion UUIDs: Collision probability ā‰ˆ 0.0000000000000000000000000001%
  • 1 trillion UUIDs: Still virtually zero collision risk
  • 1 quadrillion UUIDs: Collision probability remains negligible

Why UUIDs Are Considered Unique

UUIDs are "practically unique" because:

  • The collision probability is lower than hardware failure rates
  • You'd need to generate billions of UUIDs per second for millions of years to see a collision
  • The risk is lower than other failure modes (network errors, disk failures, etc.)

Conclusion

While UUIDs aren't mathematically guaranteed unique, they're unique for all practical purposes. The collision probability is so low that it's safer to assume uniqueness than to worry about collisions.