What is a UUID?
A UUID (Universally Unique Identifier) is a 128-bit identifier that is guaranteed to be unique across space and time. UUIDs are standardized by the Open Software Foundation (OSF) as part of the Distributed Computing Environment (DCE) and by RFC 4122.
UUIDs are typically represented as 32 hexadecimal digits, displayed in 5 groups separated by hyphens in the form 8-4-4-4-12 for a total of 36 characters (32 alphanumeric characters and 4 hyphens).
Example: 550e8400-e29b-41d4-a716-446655440000
UUID Versions
The UUID specification defines multiple versions, each with different generation mechanisms:
Version 1 (Time-based)
Generated using the current timestamp and the MAC address of the computer. Contains the MAC address of the generating computer and a timestamp.
Version 2 (DCE Security)
Similar to version 1, but includes a local domain number. Rarely used in practice.
Version 3 (Name-based, MD5)
Generated by hashing a namespace identifier and name using MD5.
Version 4 (Random)
Generated using random or pseudo-random numbers. This is the most commonly used version due to its simplicity and lack of identifying information.
Version 5 (Name-based, SHA-1)
Generated by hashing a namespace identifier and name using SHA-1.
UUID Structure
A standard UUID consists of 5 groups in the format xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx where:
- The first 3 bits of the 7th byte indicate the UUID version
- The first 1-3 bits of the 9th byte indicate the UUID variant
- The remaining bits are specific to each UUID version
Common Uses
UUIDs are used in many contexts, including:
- Database primary keys
- Distributed system identifiers
- Session IDs in web applications
- Transaction IDs
- File names and identifiers
- Content addressing in distributed systems
Collision Probability
The probability of duplicate UUIDs is extremely low. For randomly generated UUIDs (version 4), the probability of finding a duplicate within 103 trillion UUIDs is only 1 in a billion.