July 7, 2019 ~ 2 min read

Universally unique identifier

sheep

Also known as uuid.

A concept that has increased his presence in my everyday work.

An uuid is a 128-bit number that is most often know to our eyes as a string that looks like this:

81360820-0a81-4bbe-bf72-9ec70238c12c

For me, this strings replaced the classic incremental id used for identify rows / documents in a database table / collection.

At that time, my naive programmer’s mind was telling me that this strings are random and unique regardless of how many I generate, as long as I don’t need more that the total possible combinations.

And there are a lot of them.

But, deep inside my simple yet chaotic mind, a question has been born:

How is this generator thing keep track of the already generated numbers? What is this sorcery all about?

It turns out that there are some trade-offs made here.

There are multiple versions, with slightly different concepts that solve different problems.

For example, Version 1 uses a MAC address and the date-time at the cost of randomness in order to ensure the uniqueness of the uuid’s. That makes it easily guessable and not suited for certain scenarios.

On the other hand, Version 4 (the one that I used for rows / documents ids) is all randomly generated, which comes with a very small probability of collision.

Yep, there is a chance that I will mess up some data, a small one tho.

If you are unsure of what version you’re using, there is a character in the string representation that is reserved for that. e.g. Version 4 looks like this:

xxxxxxxx-xxxx-4xxx-xxxx-xxxxxxxxxxxx

Here is more information about other versions and this topic.