Developer Tools
The Complete Guide to UUIDs
Learn about UUIDs, their format, versions, collision probability, and when to use them as database keys.
A UUID is a 128-bit value used to uniquely identify information. Represented as 32 hex digits in 5 groups (8-4-4-4-12): 550e8400-e29b-41d4-a716-446655440000.
UUID Versions
- v1: Timestamp + MAC address. Reveals machine identity.
- v4: Random. Most common. Simple and secure.
- v5: Name-based using SHA-1. Deterministic.
- v7: Time-ordered. Database-friendly.
Collision Probability
UUID v4 has 2^122 possible values. You need ~2.71 × 10^18 UUIDs for a 50% collision chance. At 1 billion per second, that's 85 years.
UUIDs as Database Keys
Great for distributed systems (no coordination needed). But random UUIDs cause index fragmentation. Solutions: UUID v7 (time-ordered) or store as binary(16).
UUID vs Auto-Increment
Auto-increment: simpler, storage-efficient, but reveals sequence. UUIDs: globally unique, unpredictable, works across distributed systems.
Frequently Asked Questions
How unique is a UUID?
Practically infinitely unique. You'd need quintillions of UUIDs for a realistic collision chance.
UUID or auto-increment?
Auto-increment for simple apps. UUIDs for distributed systems or when ID predictability is a security concern.
UUID v4 vs v7?
v4 is random. v7 is time-ordered, better for databases because new UUIDs sort in insertion order.
