A Universally Unique Identifier (UUID) is a 128-bit label used for identifying information in computer systems. Standardized by RFC 4122, a UUID is conventionally represented as a 36-character string containing 32 hexadecimal digits and four hyphens, structured as 8-4-4-4-12. For example: 550e8400-e29b-41d4-a716-446655440000.
The primary advantage of UUIDs is that they can be generated independently across distributed systems without relying on a central database or an auto-incrementing integer sequence. This decentralized generation guarantees that multiple microservices can create records simultaneously without the risk of generating overlapping primary keys.
How UUID Version 4 is generated
While there are several versions of UUIDs based on timestamps, MAC addresses, or cryptographic hashing, UUID version 4 is overwhelmingly the most common format in modern software. A UUIDv4 is generated almost entirely using random or pseudo-random numbers.
Out of the 128 bits, 6 bits are strictly reserved to indicate the UUID's variant and version. The 13th hexadecimal character must be a '4' (indicating version 4), and the 17th character must be '8', '9', 'a', or 'b' (indicating the RFC 4122 variant). The remaining 122 bits are filled with cryptographically secure random data, providing 2^122 possible unique identifiers.
The mathematical reality of collisions
Because UUIDv4 relies on randomness rather than coordination, there is a theoretical possibility that the same UUID could be generated twice, an event known as a collision. However, the sheer size of the 122-bit random space makes collisions virtually impossible in practice. The total number of possible UUIDv4 combinations is approximately 5.3 × 10^36 (5.3 undecillion).
To contextualize this probability using the Birthday Paradox: you would need to generate 1 billion UUIDs every second for about 85 years to reach a mere 50% probability of a single collision occurring. For nearly all conceivable software applications, the risk of a UUIDv4 collision is vastly lower than the risk of hardware failure or cosmic rays flipping memory bits.