How unique is UUID?

后端 未结 11 1866
终归单人心
终归单人心 2020-11-22 05:40

How safe is it to use UUID to uniquely identify something (I\'m using it for files uploaded to the server)? As I understand it, it is based off random numbers. However, it s

相关标签:
11条回答
  • 2020-11-22 05:53

    UUID schemes generally use not only a pseudo-random element, but also the current system time, and some sort of often-unique hardware ID if available, such as a network MAC address.

    The whole point of using UUID is that you trust it to do a better job of providing a unique ID than you yourself would be able to do. This is the same rationale behind using a 3rd party cryptography library rather than rolling your own. Doing it yourself may be more fun, but it's typically less responsible to do so.

    0 讨论(0)
  • 2020-11-22 05:54

    The answer to this may depend largely on the UUID version.

    Many UUID generators use a version 4 random number. However, many of these use Pseudo a Random Number Generator to generate them.

    If a poorly seeded PRNG with a small period is used to generate the UUID I would say it's not very safe at all. Some random number generators also have poor variance. i.e. favouring certain numbers more often than others. This isn't going to work well.

    Therefore, it's only as safe as the algorithms used to generate it.

    On the flip side, if you know the answer to these questions then I think a version 4 uuid should be very safe to use. In fact I'm using it to identify blocks on a network block file system and so far have not had a clash.

    In my case, the PRNG I'm using is a mersenne twister and I'm being careful with the way it's seeded which is from multiple sources including /dev/urandom. Mersenne twister has a period of 2^19937 − 1. It's going to be a very very long time before I see a repeat uuid.

    So pick a good library or generate it yourself and make sure you use a decent PRNG algorithm.

    0 讨论(0)
  • 2020-11-22 05:55

    I concur with the other answers. UUIDs are safe enough for nearly all practical purposes1, and certainly for yours.

    But suppose (hypothetically) that they aren't.

    Is there a better system or a pattern of some type to alleviate this issue?

    Here are a couple of approaches:

    1. Use a bigger UUID. For instance, instead of a 128 random bits, use 256 or 512 or ... Each bit you add to a type-4 style UUID will reduce the probability of a collision by a half, assuming that you have a reliable source of entropy2.

    2. Build a centralized or distributed service that generates UUIDs and records each and every one it has ever issued. Each time it generates a new one, it checks that the UUID has never been issued before. Such a service would be technically straight-forward to implement (I think) if we assumed that the people running the service were absolutely trustworthy, incorruptible, etcetera. Unfortunately, they aren't ... especially when there is the possibility of governments' security organizations interfering. So, this approach is probably impractical, and may be3 impossible in the real world.


    1 - If uniqueness of UUIDs determined whether nuclear missiles got launched at your country's capital city, a lot of your fellow citizens would not be convinced by "the probability is extremely low". Hence my "nearly all" qualification.

    2 - And here's a philosophical question for you. Is anything ever truly random? How would we know if it wasn't? Is the universe as we know it a simulation? Is there a God who might conceivably "tweak" the laws of physics to alter an outcome?

    3 - If anyone knows of any research papers on this problem, please comment.

    0 讨论(0)
  • 2020-11-22 05:56

    Been doing it for years. Never run into a problem.

    I usually set up my DB's to have one table that contains all the keys and the modified dates and such. Haven't run into a problem of duplicate keys ever.

    The only drawback that it has is when you are writing some queries to find some information quickly you are doing a lot of copying and pasting of the keys. You don't have the short easy to remember ids anymore.

    0 讨论(0)
  • 2020-11-22 05:57

    Very safe:

    the annual risk of a given person being hit by a meteorite is estimated to be one chance in 17 billion, which means the probability is about 0.00000000006 (6 × 10−11), equivalent to the odds of creating a few tens of trillions of UUIDs in a year and having one duplicate. In other words, only after generating 1 billion UUIDs every second for the next 100 years, the probability of creating just one duplicate would be about 50%.

    Caveat:

    However, these probabilities only hold when the UUIDs are generated using sufficient entropy. Otherwise, the probability of duplicates could be significantly higher, since the statistical dispersion might be lower. Where unique identifiers are required for distributed applications, so that UUIDs do not clash even when data from many devices is merged, the randomness of the seeds and generators used on every device must be reliable for the life of the application. Where this is not feasible, RFC4122 recommends using a namespace variant instead.

    Source: The Random UUID probability of duplicates section of the Wikipedia article on Universally unique identifiers (link leads to a revision from December 2016 before editing reworked the section).

    Also see the current section on the same subject on the same Universally unique identifier article, Collisions.

    0 讨论(0)
  • 2020-11-22 05:58

    For UUID4 I make it that there are approximately as many IDs as there are grains of sand in a cube-shaped box with sides 360,000km long. That's a box with sides ~2 1/2 times longer than Jupiter's diameter.

    Working so someone can tell me if I've messed up units:

    • volume of grain of sand 0.00947mm^3 (Guardian)
    • UUID4 has 122 random bits -> 5.3e36 possible values (wikipedia)
    • volume of that many grains of sand = 5.0191e34 mm^3 or 5.0191e+25m^3
    • side length of cubic box with that volume = 3.69E8m or 369,000km
    • diameter of Jupiter: 139,820km (google)
    0 讨论(0)
提交回复
热议问题