Understanding UUIDs
A UUID (Universally Unique Identifier) is a 128-bit number used to uniquely identify information in computer systems. The probability that a UUID will be duplicated is not zero, but it is close enough to zero to be negligible.
UUIDs are standardized by the Open Software Foundation (OSF) as part of the Distributed Computing Environment (DCE). They are also documented in RFC 4122.
UUID Structure
A standard UUID is a 32-character hexadecimal string, displayed in five groups separated by hyphens, in the form 8-4-4-4-12 for a total of 36 characters (32 alphanumeric characters and 4 hyphens).
- The
Mposition indicates the UUID version (for v4, this is always '4'). - The
Nposition indicates the variant (for v4, this is one of '8', '9', 'a', or 'b').
Why Use Version 4?
Version 4 UUIDs are completely random. Unlike Version 1 (which includes a timestamp and MAC address), v4 UUIDs do not reveal any information about the time they were created or the machine that created them. This makes them ideal for:
- Privacy: No hardware address or timestamp is encoded.
- Simplicity: Can be generated anywhere without coordination.
- Performance: Fast to generate with modern cryptographic libraries.
Common Use Cases
- Database Keys: Primary keys in distributed databases (like Cassandra or DynamoDB) or when using ORMs.
- Session IDs: Unique tokens for user sessions in web applications.
- File Names: Preventing filename collisions in uploaded files.
- API Keys: Generating unique access tokens (though specialized formats are often preferred).
- Transaction IDs: Tracking requests across microservices.