128

I need to create some uniques files in Java and i plan to use UUID.randomUUID to generate their names. Is there any chance to get a collision for this? Should i do something like bellow os I shouldn't worry about this?

Integer attemptsToGenerateUUID = 1;

while (true) {
    UUID fileUUID = UUID.randomUUID();

    if (fileDoesNotExistwith this UUID name) {
        save file;
        break;
    }

    attemptsToGenerateUUID += 1;

    if (attemptsToGenerateUUID > 64) {
        return false;
    }
}
daniels
  • 18,416
  • 31
  • 103
  • 173
  • I've seen there's always a possibility to have collision. I'm just wondering in my case is it worth to implement this "failsafe" so I at least try a few times to get a unique name, just in case of a collision. – daniels Jul 21 '14 at 23:01
  • 1
    Downvote. The answer is in the sidebar. Close as duplicate when you're finished. – djechlin Jul 21 '14 at 23:01
  • 62
    To put it into perspective -- Its more likely for a meteor to hit the computer running the program, blowing it up then for a UUID to collide. So if you want a 100% fail proof program you should solve the meteor defense problem before you start digging into handling UUID collision. – gbtimmon Jul 18 '19 at 15:43
  • 8
    @gbtimmon this has happened - https://www.nytimes.com/2021/10/14/world/canada/meteorite-bed.html – Janac Meena Nov 11 '21 at 14:50

1 Answers1

256

According to wikipedia, regarding the probability of duplicates in random UUIDs:

Only after generating 1 billion UUIDs every second for the next 100 years, the probability of creating just one duplicate would be about 50%. Or, to put it another way, the probability of one duplicate would be about 50% if every person on earth owned 600 million UUIDs.

I guess the same reasoning applies to Java's implementation of UUID. So no, you should not worry about this.

Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • 139
    So you're saying there's a chance? – Jack Dec 15 '21 at 18:43
  • 8
    @Jack yes, but it’s astronomically low. – Óscar López Dec 15 '21 at 19:44
  • 16
    Ah meant it more a as a joke... thank you for your well described answer! – Jack Dec 15 '21 at 20:44
  • 3
    Obviously, noone plays powerball/lotto :) – nosbor Feb 07 '22 at 10:05
  • 1
    I think it is an stupid question, bet let me ask anyway. how about between multiple computers? when I have more than one computer trying to generate UUID that would be stored into database as unique – Popeye Feb 14 '22 at 03:06
  • 2
    @Popeye Not a stupid question! The principle doesn't change, UUID generation is really random—meaning you can consider the generation of UUIDs to to be independent events from one another. In other words, creating UUIDs from different computers does not change anything, it is _incredibly unlikely_ that a collision will occur. In fact, this is why distributed systems tend to use UUID-like identifiers instead of global sequential identifiers which require locking. Google, Facebook and other big data organisations all use identifiers like them (though, perhaps their length is greater?) – Alexandre Cassagne May 03 '22 at 19:02
  • @Jack Not every finite-length string is unique. – Hossein Jul 18 '22 at 10:24
  • When I create a unique constraint on a field containing uuids, then I must have a plan for the failed inserts? or they will not happen. – Sam Washington Aug 19 '22 at 16:54
  • 1
    I foresee an eventual explosion of human and human-like civilization across the galaxy and beyond which coincides with an equally exponential increase in the number of UUIDs each person creates, such that when the population is in the quadrillions, this will eventually become a problem similar to Y2k and it will cause everything to grind to a ha.... wait a second... – emery Nov 29 '22 at 18:54
  • Just got an collision. Luckily it was on a dev machine. – Valerij Dobler Jun 29 '23 at 08:09
  • 2
    @ValerijDobler more than likely you found a bug in your code, not a uuid collision :) – irreal Jul 25 '23 at 07:44