Is unique id generation using UUID really unique?

后端 未结 3 657
终归单人心
终归单人心 2020-12-20 14:05

I want generate unique ID just like auto increment in java . So previously i used current nano seconds but i end up with clash since two data comes with in same nano second

相关标签:
3条回答
  • 2020-12-20 14:43

    While the UUIDs are not guaranteed to be unique, the probability of a duplicate is extremely low. See Random UUID probability of duplicates.

    For your application, it makes sense to use the UUID, but you may want to deal with the extremely rare condition, just in case.

    0 讨论(0)
  • 2020-12-20 14:52

    There seems to be some confusion on this page about the nature of UUID.

    Study the Wikipedia page. You will see there are different versions of UUID.

    You asked:

    Does UUID solves the above problem ?

    Yes, UUID values do solve your problem.

    A point in space and time

    The original Version 1 represents a point in space and time, never to be repeated.

    Version 1 does this by using the MAC address of the machine on which it is generated (a point in space). To this it combines the current moment. Add in an arbitrary number that increments when a change in the computer clock is noticed. The clock is not as much of an issue now that computers have built-in batteries and network connections to time servers. By combining these, there is no practical chance of collisions.

    Because of concerns over the security and privacy issues involved in tracking and divulging the MAC address and moment, some people may not want to use this version. For example, Java omits generating Version 1 from its UUID class.

    FYI, the more powerful database servers such as Postgres can generate UUID values including Version 1. You may choose to generate your UUIDs on the database server rather than in your app.

    Random

    One commonly used version is Version 4, in which 122 of the 128 bits are generated randomly. If a cryptographically-strong random generator is used, this is quite effective. This version has a much higher chance of collisions than in Version 1. But for most practical scenarios, the random-based UUID is entirely reliable.

    0 讨论(0)
  • 2020-12-20 14:57

    I seriously doubt you get two records in the same nano-second as making the call System.nanoTime() takes over 100 ns. It is more likely your clock doesn't have nano second accuracy.

    However, if you restart your server, you can get repeating nanoTime().

    One way around this is to use

    AtomicLong counter = new AtomicLong(System.currentTimeMillis()*1000);
    
    long id = counter.incrementAndGet();
    
    // something like ctz9yamgu8
    String id = Long.toString(counter.incrementAndGet(), 36);
    

    This will start a counter when the application restarts and they will not be overlap between restarts unless you sustain over one million ids per second. (Over the life of the instance)

    Note: this only works for on a per instance basis. Multiple servers need to use a different approach.

    0 讨论(0)
提交回复
热议问题