A UUID (Universally Unique Identifier) is a string of characters that is used to uniquely identify an object. You may also see the term GUID (Globally Unique Identifier) which is also commonly used. Both UUIDs and GUIDs are generated according to a standard, using a combination of random numbers, timestamps and algorithms, all of which makes it extremely unlikely that two values will be equal. It is based on the RFC 4122 standard.
Data types specified as UUID are expected to be in a valid string representation of a universally unique identifier (UUID). The API specifically expects the UUID to be provided in its canonical form which is represented by 32 lowercase hexadecimal digits displayed in five groups separated by hyphens.
This representation takes the form of 8-4-4-4-12 for a total of 36 characters, 32 hexadecimal characters and four hyphens. In case you are converting an array of bytes, the break down of bytes for the hexadecimal String is 4-2-2-2-6. UUIDs are version 4.
Example of a UUID in the expected canonical string format in a JSON request or response body.
Example of a UUID being provided as a URL segment for an API.
Here are some common use cases for generating a version 4 UUID:
This UUID generator generates a version 4 universally unique identifier. A version 4 UUID is made up of 32 hexadecimal characters (128 bits), divided into five groups separated by hyphens in this format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx. You may also see this written as 8-4-4-4-12.
The main advantage of version 4 UUIDs is that they are generated using random numbers, which makes it extremely unlikely that two UUIDs will be the same.
How unlikely? To be specific, a UUID is 16 bytes or 128 bits. Out of these 128 bits, 4 are used to indicate the version, and another 2 indicate the variant. This leaves us with 122 bits, and as a result we have 2^122 possible combinations, or 5.3 undecillion, or 5.31x10^36 (or a 5 with 36 zeros behind it). That is a lot. When we begin talking about numbers in this range - the probability that a duplicate value will be generated is essentially zero.
This makes UUIDs perfect for use as a unique object identifier in software programs, database records, and other situations where a unique identifier is needed.
One way to think about a version 4 UUID collision is like this:
Only after generating 1 billion UUIDs every second for the next 100 years, the probability of creating just one duplicate would be about 50%. Or, to put it another way, the probability of one duplicate would be about 50% if every person on earth owned 600 million UUIDs.