Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I worry about UUID uniqueness

Tags:

uuid

I am using this https://www.npmjs.com/package/uuidv4 module to identify uniqueness of the user. Should I worry that 2 people might get the same uuid? What if I store of all given uuids for the day and before I give new uuids I check if it already exists in the database? And then after a day I would empty my database and repeat the process. Is it a good approach? Am I missing anything?

like image 469
good_evening Avatar asked Jan 29 '26 08:01

good_evening


1 Answers

Should I worry that 2 people might get the same uuid...

No you shouldn't. UUID's are unique for practical purposes.

From Wikipedia: UUID:

..., the number of random version 4 UUIDs which need to be generated in order to have a 50% probability of at least one collision is 2.71 quintillion...

As for:

What if I store of all given uuids for the day and before I give new uuids I check if it already exists in the database

You should have a Unique Key constraint on your table. Even if you do get a collision the DB would reject it.

like image 97
nicholaswmin Avatar answered Feb 02 '26 04:02

nicholaswmin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!