Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is unique id generation using UUID really unique?

Tags:

java

uuid

unique

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 seconds .. Does UUID solves the above problem ?

Note :: In my project i can even get 10000 rows of records for each and every minute and I will dump those records along with UIDS in to table .And there may be a situation where i would stop my product and restart it after some time ....So during that situation how could UUID class clarifies the previously generated Uids(which i stored in DB) with the new one going to created(Yet to be dumped in DB) ?

like image 902
vijayasankarj Avatar asked Apr 20 '11 09:04

vijayasankarj


3 Answers

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.

like image 67
AbdullahC Avatar answered Sep 24 '22 23:09

AbdullahC


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.

like image 45
Peter Lawrey Avatar answered Sep 24 '22 23:09

Peter Lawrey


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.

like image 21
Basil Bourque Avatar answered Sep 22 '22 23:09

Basil Bourque