Should an application check for UUID v4 duplicated?

Question:

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!

Asked By: bieboebap

||

Answers:

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.

Answered By: mozway
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.