UUIDs are a specific type of ID.
UUID stands for Universally Unique Identifier
, and it’s a 128-bit format for creating IDs in code that has become popular recently, especially when it comes to database keys.
By using UUIDs, you ensure that your ID is not just unique in the context of a single database table, but is unique in the universe.
No other ID in the world should match the value of yours.
Why use a UUID?
Benefit
- They are unique across every table, every database and every server
- They allow easy merging of records from different databases
- They allow easy distribution of databases across multiple servers
- You can generate UUID anywhere, both Frontend side and Backend side
Drawback
- It can be cumbersome to debug Â
where userid='{bc2d0f53-5041-46e8-a14c-267875a49f0c}’
- It’s like 4 times larger than the traditional 4-byte index value, this can have serious performance and storage implications if you’re not careful.
The UUID Format
A UUID is made up of 32 hex (base-16) digits, displayed in five sections. If you take a look at this example, bc2d0f53-5041-46e8-a14c-267875a49f0c
. The sections are broken in the form 8-4-4-4-12. Including the four hyphens, it comes to a total of 36 characters. UUIDs are also typically displayed in lowercase.
UUID vs IDs
All UUIDs are IDs, but not all IDs are UUIDs. A UUID is a specific type of ID that has a format like this : bc2d0f53-5041-46e8-a14c-267875a49f0c
. And common forms of IDs include:
- Integers 0,1,2,3, etc
- Strings - Predefined strings like command_loading, command_received, command_validated
Another important point of difference between UUIDs and IDs is the predictability. Let’s assume we have Restful application, because of the simplicity of ID, we (clients) can guess the correct of some resources that are not intended for us.
For example, if we are intended to see a product with ID 15, for example with this URL:
https://demo.com/api/product/15
Because of sequential IDs, I can guess the previous product in the catalog by entering an ID before mine:
https://demo.com/api/product/14
With a UUID like this bc2d0f53-5041-46e8-a14c-267875a49f0c
it is impossible to guess the previous and next Product or Data in our system. And that’s such a great security that UUID offers in bonus.
I hope you enjoyed reading this, and I'm curious to hear if this tutorial helped you. Please let me know your thoughts below in the comments. Don't forget to subscribe to my newsletter to avoid missing my upcoming blog posts.
You can also find me here LinkedIn • Twitter • GitHub or Medium
Closing Thoughts
It is important to note that while the probability that a UUID will collide with another is not zero, it’s practically zero. The chances of a collision are so astronomically low, worrying about it would be ridiculous.