UUIDs, Why & How ? - DevDummy

Latest

Coding Hacks, Shared ...

Sunday, November 20, 2016

UUIDs, Why & How ?


UUIDs
As per Wikipedia,
A universally unique identifier (UUID) is an identifier standard used in software construction. A UUID is simply a 128-bit value. The meaning of each bit is defined by any of several variants.

Format
A UUID is a 16-octet (128-bit) number in with digit having hyphens in the form of, 8-4-4-4-12.
123e4567-e89b-12d3-a456-426655440000

Variants and Versions
There are four different basic types of UUIDs: time-based, DCE security, name-based, and randomly generated UUIDs. These types have a version value of 1, 2, 3 and 4, respectively.
However in programming rendomly generated type (version 4) is used mostly. 

Version 4, randomly generated UUIDs have its own specific digit content such as;

xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx

Here x is any hexadecimal digit and y is one of 8, 9, a, or b.
Example :
f47ac10b-58cc-4372-a567-0e02b2c3d479

Why UUIDS in programming ?
Specially when you are in a concurrent or distributed environment, when the transactions or the operations are not lablled by a centraliced mechanism, you have to make the each request distinct between others regardless of the external application. 
In this case UUIDs are a great fit which naturally avoids the duplicates ( at lease as per the requirment scope of the application).

java.util.UUID class
UUID generation in Java in very straight forward using  java.util.UUID class:

UUID  randomUUID  =  UUID.randomUUID();

No comments:

Post a Comment