Search code examples
pythonpython-3.xuuid

Prevent uuid collision in Python same process


I am generating uuid in Python, I noticed there are collisions. I get collisions if I use uuid.uuid1() or uuid.uuid4().

I read many articles online but they elaborate about the "theory" of impossibility of UUID collision if generated properly.

But I have yet to find one that explains how I can ensure my UUID generation is properly done.

How can I do this before Python 3.7?
I saw Python 3.7 has a notion of UUID safety but is there a way to do this outside of 3.7?

Details:
running on Ubuntu 18.04, running Python code in Jupyter Lab.


Solution

  • First of we need to understand how UUID is generated.

    UUID1 is generated depending on time + Host ID + random component. So if you generate UUID1 on the same host at the same time you only rely on the random component which is 14 bits which means that you have 1 chance out of 16384 to have a collision. Not very high but should be considered.

    UUID4 is full random(128bits) and should NOT collide, if it's colliding in your case it's probably due to something on your system like on this issue.

    Could you please specify your os for further investigations ? Do you have os.urandom installed ?