UUID Versions
UUID (Universally Unique Identifier) has several versions, each with different properties.
Version Overview
| Version | Based On | Sortable | Use Case |
|---|
| v1 | Timestamp + MAC | Partially | Legacy systems |
|---|
| v4 | Random | No | General purpose |
|---|
| v5 | Namespace + Name | No | Deterministic IDs |
|---|
| v7 | Timestamp + Random | Yes | Modern databases |
|---|
UUID v4 (Random)
The most commonly used version. 122 random bits.
xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
^ ^
| Variant (8, 9, a, or b)
Version (4)
Example: 550e8400-e29b-41d4-a716-446655440000
Pros:
- Simple to generate
- No coordination needed
- Privacy (no embedded info)
- Not sortable
- Poor database index performance
UUID v7 (Timestamp-based)
Newest version. Timestamp prefix + random suffix.
tttttttt-tttt-7xxx-yxxx-xxxxxxxxxxxx
^^^^^^^^ ^^^^
Unix timestamp (ms)
Example: 0188a5f5-c308-7b99-b5c0-7b5c1c1c1c1c
Pros:
- Sortable by creation time
- Better database indexing
- Still globally unique
- Reveals creation time
- Slightly newer (less library support)
When to Use What
Use v4 when:- You don't need sorting
- Privacy is important
- Simple random IDs are fine
- Database performance matters
- You need chronological ordering
- Building time-series data
Collision Probability
UUID v4 has 2^122 possible values. The probability of collision:
- After 1 billion UUIDs: ~0.00000000000000001%
- You'd need 2.71 × 10^18 UUIDs for 50% collision chance
- That's generating 1 billion UUIDs per second for 85 years