The Complete Developer's Guide to UUIDs and GUIDs
UUIDs are fundamental to distributed systems and modern databases. This guide covers everything developers need to know about generating and using unique identifiers.
Understanding Unique Identifiers
UUIDs (Universally Unique Identifiers) and GUIDs (Globally Unique Identifiers) are 128-bit numbers used to uniquely identify information in computer systems. While technically identical, UUID is the term used in the official RFC specification, while GUID is Microsoft's terminology.
Why UUIDs Matter
In distributed systems, databases, and modern architectures, the need for unique identifiers that can be generated independently across different systems is critical:
- Database primary keys that don't conflict when merging data
- Distributed systems where nodes generate IDs without coordination
- API resources that need permanent, unique identifiers
- Session and transaction tracking across microservices
- File and object naming in cloud storage
The Format
A UUID is a 128-bit number represented as 32 hexadecimal digits, displayed in five groups separated by hyphens:
xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx
Where:
- M indicates the UUID version (1-5)
- N indicates the variant (typically 8, 9, A, or B for RFC 4122)
UUID Versions Explained
Version 1: Timestamp + MAC Address
Version 1 UUIDs combine the current timestamp with the MAC address of the generating computer.
Structure:
- 60-bit timestamp (100-nanosecond intervals since October 15, 1582)
- 14-bit clock sequence (for uniqueness if clock moves backward)
- 48-bit node ID (typically MAC address)
- Time-based: Yes
- Sortable: Partially (not naturally due to byte ordering)
- Privacy: Low (exposes MAC address and generation time)
- Collision risk: Very low
- Generation: Can be sequential from same machine
- Legacy systems requiring timestamp ordering
- Systems where generation source tracking is beneficial
- Single-machine batch processing
- Exposes MAC address (privacy/security risk)
- Reveals generation time
- Not ideal for user-facing IDs
Version 4: Random
Version 4 UUIDs are generated using random or pseudo-random numbers.
Structure:
- 122 random bits
- 4 version bits
- 2 variant bits
- Time-based: No
- Sortable: No
- Privacy: High (reveals nothing)
- Collision risk: 2.71 quintillion UUIDs for 50% collision chance
- Generation: Random, unpredictable
Use Cases:
- Database primary keys
- API resource identifiers
- Session IDs
- General-purpose unique identification
- Generating 1 billion UUIDs per second
- Would take 85 years for 50% collision probability
- In practice, collisions are negligible
Version 5: SHA-1 Hash
Version 5 generates UUIDs by hashing a namespace identifier and name using SHA-1.
Characteristics:
- Time-based: No
- Deterministic: Yes (same input = same output)
- Collision risk: Lower than V3 (SHA-1 > MD5)
- Generation: Reproducible
Version 7: Unix Timestamp + Random
RFC 9562 introduced Version 7, optimized for modern needs:
- 48-bit Unix timestamp (milliseconds)
- 74 random bits
- Naturally sortable by time
- No privacy concerns
Choosing the Right UUID Version
Decision Matrix
| Requirement | Recommended Version |
|---|---|
| General purpose | Version 4 |
| Time-sortable | Version 7 |
| Deterministic from name | Version 5 |
| Legacy compatibility | Version 1 |
| Maximum randomness | Version 4 |
| Database optimization | Version 7 |
Database Considerations
UUID v4 in Databases:
- Random distribution
- Poor index locality (B-tree fragmentation)
- Higher storage than integer IDs
- Good for security (unpredictable)
- Sequential-ish ordering
- Better index performance
- Natural time ordering
- Recommended for new applications
UUID Best Practices
Storage
Storage Size Comparison:
| Format | Size |
|---|---|
| String (with hyphens) | 36 bytes |
| String (without hyphens) | 32 bytes |
| Binary | 16 bytes |
Validation
UUID regex pattern validates the format: 8-4-4-4-12 hexadecimal digits with version 1-7 and variant 8, 9, A, or B.
URL-Safe UUIDs
Remove hyphens for compact representation, or use Base64 encoding (22 characters) for even shorter URLs.
Nil UUID
The nil (or empty) UUID consists of all zeros: 00000000-0000-0000-0000-000000000000
Use for representing "no value" or as a placeholder.
UUID vs. Alternatives
Auto-Increment IDs
Advantages of Auto-Increment:
- Smaller storage (4-8 bytes)
- Natural ordering
- Simple to understand
- Better index performance (traditionally)
- Requires central coordination
- Reveals count/order information
- Difficult to merge databases
- Sequential = predictable
ULIDs
ULID (Universally Unique Lexicographically Sortable Identifier):
- 128-bit compatible with UUID
- Lexicographically sortable
- Case-insensitive
- No special characters
- 26 characters (Crockford Base32)
NanoID
- URL-safe by default
- Customizable length
- 21 characters default
- Smaller than UUID
- Not UUID-compatible
Comparison Table
| Feature | UUID v4 | UUID v7 | ULID | Snowflake | NanoID |
|---|---|---|---|---|---|
| Size | 128-bit | 128-bit | 128-bit | 64-bit | Variable |
| Sortable | No | Yes | Yes | Yes | No |
| Time info | No | Yes | Yes | Yes | No |
| Collisions | ~0 | ~0 | ~0 | Coordinated | ~0 |
| URL-safe | No | No | Yes | Yes | Yes |
Security Considerations
When UUIDs Are Not Enough
UUIDs should not be considered secrets:
- V1 exposes MAC address and time
- V4 randomness depends on entropy source
- Guessing a specific UUID is hard, but checking if one exists may not be
- Authentication/authorization checks
- Rate limiting
- Additional secret tokens
- Audit logging
Timing Attacks
UUID v1 and v7 expose generation time. Consider this when choosing UUID versions for sensitive applications.
ToolPop UUID Generator
Generate UUIDs instantly with ToolPop:
- Choose version (v1, v4, v5, or v7)
- Generate single or bulk UUIDs
- Copy or download results
- Validate existing UUIDs
Features
- Multiple UUID versions
- Bulk generation (up to 1000)
- Format options (standard, compact, base64)
- Validation tool
- Timestamp extraction (v1, v7)
Conclusion
UUIDs are foundational to modern distributed systems. Understanding the different versions and their characteristics helps you make informed decisions:
- Use UUID v4 for general-purpose, random identifiers
- Use UUID v7 for sortable, time-ordered identifiers
- Use UUID v5 for deterministic generation from names
- Consider alternatives like ULID or NanoID for specific requirements
Generate UUIDs instantly with ToolPop's UUID Generator—supporting all versions with bulk generation and validation.
Try Our Free Tools
Put these tips into practice with our free online tools. No signup required.
Explore Tools