UUID Guide: Types and Generation

What Is a UUID?

A UUID (Universally Unique Identifier) is a 128-bit value used to uniquely identify information in computer systems. Also known as a GUID (Globally Unique Identifier) in Microsoft ecosystems, UUIDs are designed to be unique across all systems without requiring a central authority to coordinate the assignment.

A typical UUID looks like this: 550e8400-e29b-41d4-a716-446655440000. The 32 hexadecimal digits are displayed in five groups separated by hyphens, following the pattern 8-4-4-4-12.

UUIDs are everywhere in modern software: database primary keys, API identifiers, session tokens, distributed system message IDs, and file identifiers. Their ubiquity stems from a crucial property: you can generate them independently on any machine and be virtually certain they will not collide with UUIDs generated anywhere else.

UUID Versions Explained

The UUID standard defines several versions, each using a different generation strategy:

Version 1 (Time-based): Combines a timestamp with the MAC address of the generating machine. This ensures uniqueness by tying the ID to a specific time and place. The downside is that it leaks information about when and where it was created.

Version 2 (DCE Security): A variant of Version 1 that incorporates POSIX user and group IDs. Rarely used in practice and not widely supported.

Version 3 (Name-based, MD5): Generates a UUID from a namespace identifier and a name by hashing them with MD5. The same namespace and name always produce the same UUID, making it deterministic and reproducible.

Version 4 (Random): Generated from random or pseudo-random numbers. This is the most commonly used version because it is simple, requires no coordination, and does not leak any information. The probability of collision is astronomically low.

Version 5 (Name-based, SHA-1): Like Version 3 but uses SHA-1 instead of MD5 for hashing. Preferred over Version 3 when deterministic generation from a name is needed, since SHA-1 is a stronger hash function.

Version 7 (Time-ordered): A newer addition that embeds a Unix timestamp in the first 48 bits, followed by random data. This provides the benefits of random UUIDs while maintaining chronological sortability, making it excellent for database primary keys.

Choosing the Right UUID Version

Your use case determines which version to choose:

UUID vs Other Identifier Formats

UUIDs are not the only option for unique identifiers. Here is how they compare:

UUIDs in Databases

When using UUIDs as database primary keys, consider these trade-offs:

Advantages:

Challenges:

Version 7 UUIDs address the index fragmentation problem by being time-ordered while retaining the other benefits of UUIDs.

UUID Validation and Formatting

A valid UUID matches the regex pattern:

^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$

Conventions and best practices for UUID formatting:

A UUID generator is useful for quickly creating test IDs, populating seed data, or generating identifiers for configuration files.

Security Considerations

UUIDs are identifiers, not security tokens:

For secure token generation, use dedicated functions like crypto.randomUUID() in modern JavaScript or secrets.token_urlsafe() in Python, which are backed by cryptographic random sources.

Try our free UUID Generator — no signup required.

Explore all free tools on CalcHub

Browse Tools