Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Should an application check for UUID v4 duplicated?

I work in my app with a database.
This database stores data with a randomly generated ID of the type UUID v4.
Now I’m wondering, how common is it for a big (let’s be optimistic 😀 ) app with many users to have a duplicate ID? The ID is the primary key in the SQL database so it could only crash one API call.
Is it a clean practice to check if the UUID exists (and thus catch a possible crash in the backend) or is it redundant as it’s very unlikely to happen?

Especially considering:

  • random in python is not that random
  • there are 2¹²² combinations

EDIT:

Based on the comments it seems unnecessary to check for duplicates. Thanks!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

This shouldn’t be a problem in practice due to the very low probability of collisions.

See the Wikipedia article on UUID4 collision

For example, 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

This number is equivalent to generating 1 billion UUIDs per second for
about 85 years. A file containing this many UUIDs, at 16 bytes per
UUID, would be about 45 exabytes.

NB. UUID1 and UUID2 have a time component which make it impossible to have collisions if the UUIDs are generated at a reasonable enough frequency.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading