Reputation: 310
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 :D ) 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:
Based on the comments it seems unnecessary to check for duplicates. Thanks!
Upvotes: 3
Views: 1657
Reputation: 260630
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.
Upvotes: 3