🍋
Menu
Comparison Beginner 2 min read 314 words

UUID and GUID Generation: Formats, Versions, and Use Cases

UUIDs provide unique identifiers across distributed systems without a central authority. Understanding the different versions helps you choose the right one for your use case.

Key Takeaways

  • A Universally Unique Identifier (UUID) is a 128-bit value formatted as 32 hexadecimal digits with hyphens: `550e8400-e29b-41d4-a716-446655440000`.
  • UUIDv4 is generated from random or pseudo-random numbers.
  • UUIDv7 (defined in RFC 9562, 2024) embeds a Unix-millisecond timestamp in the first 48 bits, followed by random data.
  • Use UUIDv7 for new database primary keys

What Is a UUID

A Universally Unique Identifier (UUID) is a 128-bit value formatted as 32 hexadecimal digits with hyphens: 550e8400-e29b-41d4-a716-446655440000. The probability of two randomly generated UUIDs colliding is approximately 1 in 2¹²² — effectively zero.

UUID Versions

Version Method Use Case
v1 Timestamp + MAC address Unique across time and space
v3 MD5 hash of name + namespace Deterministic from input
v4 Random General-purpose (most common)
v5 SHA-1 hash of name + namespace Deterministic (more secure than v3)
v7 Timestamp + random (RFC 9562) Sortable, database-friendly

UUIDv4: The Default Choice

UUIDv4 is generated from random or pseudo-random numbers. It is the most widely used version because it requires no external input and is simple to implement. Every major language has a built-in UUIDv4 generator.

UUIDv7: The Modern Alternative

UUIDv7 (defined in RFC 9562, 2024) embeds a Unix-millisecond timestamp in the first 48 bits, followed by random data. This makes UUIDv7 naturally sortable by creation time — a significant advantage for database primary keys where B-tree indexes benefit from sequential inserts.

UUID vs Auto-Increment ID

Aspect UUID Auto-Increment
Uniqueness scope Global Per-table
Predictability Not guessable Sequential, guessable
Size 16 bytes 4-8 bytes
Index performance Worse (random v4) / Good (v7) Best (sequential)
Distributed generation No coordination needed Requires central authority

Practical Tips

  • Use UUIDv7 for new database primary keys
  • Use UUIDv4 for tokens, session IDs, and API keys
  • Use UUIDv5 when you need deterministic IDs from known inputs (e.g., hashing a URL)
  • Store UUIDs as 16-byte binary in databases, not as 36-character strings