Thursday, May 31, 2007

Unique ID Generation

Generating globally unique IDs in a clustered, distributed environment is a common application requirement. A simple way is to combine the following four parts together.
  1. Unique to the System.currentTimeMillis()
  2. Unique to the IP address
  3. Unique to the object instance System.identityHashCode(this)
  4. Unique within a millisecond, SecureRandom
All ints and longs will be converted to a hex string of length 8.

A open source named Java UUID Generator is available at http://jug.safehaus.org/.

No comments: