UUIDs (Universally Unique Identifiers) are 128-bit values used to identify resources without a central authority. With 5.3×10³⁶ possible values, the chance of collision is effectively zero. But not all UUID versions are created equal — choosing the right one matters for performance, security, and sortability.
UUID Format
A UUID is a 128-bit value displayed as 32 hexadecimal digits in five groups separated by hyphens:
550e8400-e29b-41d4-a716-446655440000
- The 13th character (first digit of the third group) indicates the version (4 in this case)
- The 17th character (first digit of the fourth group) indicates the variant (8, 9, a, or b for RFC 9562)
UUID Versions Compared
| Version | Source | Sortable | Privacy | Use Case |
|---|---|---|---|---|
| v1 | Timestamp + MAC address | Partially | ❌ Leaks MAC | Legacy systems (avoid in new code) |
| v3 | MD5 hash of namespace + name | No | ✅ | Deterministic IDs from names |
| v4 | Random (122 bits) | No | ✅ | General purpose (most popular) |
| v5 | SHA-1 hash of namespace + name | No | ✅ | Deterministic IDs (prefer over v3) |
| v7 | Unix timestamp + random | ✅ Yes | ✅ | Database PKs (recommended) |
UUIDv4: The Random Standard
UUIDv4 generates 122 bits of cryptographically random data (6 bits are reserved for version and variant):
- 5.3 × 10³⁶ possible values
- No timestamp, no MAC address, no sequence — pure randomness
- Supported natively in every major language and database
- Ideal for tokens, session IDs, correlation IDs, and any non-sortable identifier
UUIDv7: The New Recommended Standard
Defined in RFC 9562 (2024), UUIDv7 combines a 48-bit Unix timestamp (millisecond precision) with 74 bits of randomness:
- Time-ordered: UUIDs generated later are lexicographically greater
- Database-friendly: Sequential inserts don't fragment B-tree indexes
- Timestamp extractable: You can recover the creation time from the UUID
- Still unique: 74 random bits provide 1.9 × 10²² unique values per millisecond
💡 Recommendation: For new applications, use UUIDv7 as your default. It provides the uniqueness of UUIDv4 with the performance benefits of sequential identifiers. Fall back to UUIDv4 only when you specifically need non-time-correlated randomness.
UUID Collision Probability
For UUIDv4 with 122 random bits (birthday problem):
| UUIDs Generated | Collision Probability |
|---|---|
| 1 billion (10⁹) | ~1 in 10¹⁹ (0.00000000000000001%) |
| 1 trillion (10¹²) | ~1 in 10¹³ |
| 1 quadrillion (10¹⁵) | ~1 in 10⁷ |
| 2.71 × 10¹⁸ | 50% (this is 86 years at 1B/second) |
UUID vs. Auto-Increment IDs
| Factor | UUID | Auto-Increment |
|---|---|---|
| Uniqueness scope | Global (no coordination needed) | Single database table |
| Storage size | 16 bytes (binary) | 4-8 bytes |
| Index performance | v4: poor (random), v7: good (sorted) | Excellent (sequential) |
| Security | Non-guessable | Sequential (enumerable) |
| Distributed systems | Works without coordination | Requires sequence server |
| Merge/migration | No conflict risk | Requires re-numbering |
Database Storage Tips
- Store as BINARY(16), not VARCHAR(36) — saves 20 bytes per row and indexes 2× faster
- PostgreSQL: Use the native
uuidtype (stored as 16 bytes internally) - MySQL: Use
BINARY(16)withUUID_TO_BIN()andBIN_TO_UUID() - Use UUIDv7 for primary keys to maintain index locality