• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

UUID Uniqueness across JVMs

 
Yohan Liyanage
Ranch Hand
Posts: 132
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am developing a distributed Java application in which I need to assign unique IDs for objects which are distributed across multiple JVMs.

Currently, I have thought of using java.util.UUID to generate UUIDs using its Random Generator (Type 4). However, I would like to know if it is guaranteed that the generated UUIDs will be unique across multiple JVMs.

Thank You
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't say, but I wouldn't think so. Google for 'globally unique identifier'. We implemented one a long time ago that had a central registry for handing out high bits, then each VM was able to allocate the low bits.
 
Yohan Liyanage
Ranch Hand
Posts: 132
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks David!

Is there anyway that I can generate unique IDs without having a central control over it? I have heard of systems which uses MAC Address of network interface, but that would require JNI, isn't it? Is there any "Pure Java" way to do such a thing?
[ May 10, 2008: Message edited by: Yohan Liyanage ]
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could manually configure each instance with (for example) a 4 bit unique key which would allow 16 servers. 4 bits isn't much to give up if you use 64 bit GUIDs. Another way could be to use a sequence generator from a DB, but again has the central connection.

make sure the high key is guaranteed to be larger than you'll ever need, make the key a long rather than an int, and avoid Strings.
 
Yohan Liyanage
Ranch Hand
Posts: 132
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks David !
 
Jason Carreira
Author
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at this post from long long ago:

GUIDs without Singletons and Databases

I've had an implementation of this that I've brought with me ever since. It's very fast (like my test harness took most of the time when I tried to measure it) and you can easily generate 100's of thousands of GUIDs per second per node.
 
Yohan Liyanage
Ranch Hand
Posts: 132
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Jason, this seems to be a good solution for my problem.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I haven't read the full article, only browsed the code, but I am wary of the hashCode part. I recently had an issue where I had to fix a hashCode used as a unique value. In my case the code was guaranteed to fail, but since hash cocdes are not unique I am dubious of their value in GUIDs
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My interpretation of the JavaDocs for the UUID class is that is unique across JVMs. The JavaDoc states its implementation uses the algorithms defined in RFC 4122: A Universally Unique IDentifier (UUID) URN Namespace, section 4.2 "Algorithms for Creating a Time-Based UUID" for creating its UUIDs. That RFC clearly states in its abstract that "A UUID is 128 bits long, and can guarantee uniqueness across space and time." {emphasis added} A such, they would be unique across JVMs instances.

Besides that, a UUID that was not safe across JVM instances wouldn't make much sense; plus it wouldn't be a Universally Unique Identifier, but rather only a UID (Unique Identifier).

We (i.e. my team at work) have been using the Java Util UUID class for UUIDs across JVMs.
[ May 10, 2008: Message edited by: Mark Vedder ]
 
Yohan Liyanage
Ranch Hand
Posts: 132
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found this in Wikipedia:


Randomly generated UUIDs like those generated by the java.util.UUID class have 122 random bits. There are 128 bits altogether with 4 bits being used for the version ('Randomly generated UUID'), and 2 bits for the variant ('Leach-Salz'). With random UUIDs, the chance of two having the same value can be calculated using probability theory (Birthday paradox).

To help non-mathematicians understand what those numbers mean, here's a comparison: One's annual risk of being hit by a meteorite is estimated to be one chance in 17 billion, that means the probability is about 0.00000000006 (6 x 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%. The probability of one duplicate would be about 50% if every person on earth owns 600 million UUIDs.

The odds of a properly generated duplicate UUID being used in error in any practical circumstance is so low that any other work to improve software reliability would clearly be a much higher priority.

Source - Wikipedia

Seems like this is a fair chance that one can take
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic