UUIDs (Universally Unique Identifiers) are 128-bit values used to identify resources across distributed systems without requiring a central authority. They appear as 36-character strings in the format 550e8400-e29b-41d4-a716-446655440000. This guide covers every UUID version, collision math, database implications, and modern alternatives.
UUID Structure
Every UUID is 128 bits (16 bytes) displayed as 32 hex digits in five groups separated by hyphens: xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx. The M digit indicates the version (1-7). The N digit indicates the variant (usually 8, 9, a, or b for RFC 4122 UUIDs).
UUID Version Comparison
| Version | Based On | Sortable | Unique Without State | Best For |
|---|---|---|---|---|
| v1 | Timestamp + MAC address | Partially | Yes (uses hardware ID) | Legacy systems |
| v3 | MD5 hash of namespace + name | No | Deterministic (same input = same UUID) | Reproducible IDs from names |
| v4 | Random (122 random bits) | No | Yes | General purpose (most common) |
| v5 | SHA-1 hash of namespace + name | No | Deterministic | Reproducible IDs (preferred over v3) |
| v7 | Unix timestamp ms + random | Yes (chronological) | Yes | Databases (sortable primary keys) |
UUID v4: The Default Choice
UUID v4 generates 122 random bits (6 bits are reserved for version and variant). It is the most widely used version because it requires no state, no clock, and no hardware ID. Every modern language has a built-in v4 generator.
UUID v7: The Database-Friendly Version
UUID v7 (RFC 9562, 2024) embeds a Unix timestamp in the first 48 bits followed by random data. This makes v7 UUIDs naturally sortable by creation time — critical for database performance. B-tree indexes work efficiently because new records are always inserted at the end rather than scattered randomly across the index.
Collision Probability
UUID v4 provides 122 bits of randomness. Using the birthday paradox formula:
- After 1 billion UUIDs: collision probability is ~0.000000000000000001 (10⁻¹⁸)
- After 2.7 quintillion (2.7 x 10¹⁸) UUIDs: 50% chance of one collision
- At 1 billion UUIDs per second, it takes 86 years to reach 50% collision probability
For all practical purposes, UUID v4 collisions do not happen. The probability is lower than being hit by a meteorite.
UUID vs Alternatives
| Format | Length | Sortable | Encoding | Bits |
|---|---|---|---|---|
| UUID v4 | 36 chars | No | Hex + hyphens | 128 |
| UUID v7 | 36 chars | Yes | Hex + hyphens | 128 |
| ULID | 26 chars | Yes | Crockford Base32 | 128 |
| CUID2 | 24 chars | Yes | Base36 | ~128 |
| nanoid | 21 chars (default) | No | URL-safe Base64 | 126 |
| Snowflake ID | 18-19 digits | Yes | Decimal integer | 64 |
Database Primary Key Recommendations
- UUID v7 or ULID for distributed systems needing sortable keys without coordination
- UUID v4 when sort order does not matter and you want maximum simplicity
- Auto-increment integers for single-database applications where sequential IDs are fine
- Snowflake IDs for high-throughput systems needing compact, sortable, 64-bit IDs
Generate UUID v4 identifiers instantly with the WizlyTools UUID Generator.