Table of Contents

What is a UUID? - Universally Unique IDentifier - (also known as GUID, Globally Unique IDentifier)

About

UUID (Universally Unique IDentifier) are generated identifier (known as surrogate) that are guaranteed to be unique and avoid then collision.

A UUID is an identifier that is unique across both :

and is therefore globally unique.

An UUID is then known as GUIDs (Globally Unique IDentifier)

Since UUIDs are unique and persistent, you can use them as URNs (The string representation of a UUID is fully compatible with the URN syntax) See the example for an URN example

Example

urn:uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6

27701bfc-78d0-4e2b-92ca-193cea53fa30

Usage

Pro/Cons

Variant and version (Type)

All UUID version are time-based with differents variants defined by the RFC 4122. They are also called the 5 sub type of UUID:

Which one you choose to use depends on the use-case.

The full qualified version of a UUID is given by:

On a global level, only three algorithms are used to generate this UUIDs:

Algorithm UUID Versions
Unique values of 802 MAC addresses
pseudo-random number 4
cryptographic hashing and application-provided text strings 3, 5

Version 1

A version 1 UUID uses:

Therefore when reading a version 1 UUID, you can get:

Usage:

Example version 1 (the 1 in 11d0)

f81d4fae-7dec-11d0-a765-00a0c91e6bf6

Version 2

A version 2 UUID uses:

Therefore when reading a version 2 UUID, you can get:

By aware that this format comes with uniqueness problem

Usage:

Version 3

Version 3 (and version_5) are Name-based UUID. They just differs on the hash function (version 3 uses md5 while version_5 uses sha1).

They are meant for generating UUIDs from names where they are unique inside a namespace.

A Name-based UUID accept as parameters:

A Name-based UUID is deterministic (not random). For the same namespace and name, you will always get the same UUID created.

The specification defined the following predefined namespaces (Ref).

Name type Namespace UUID
FQDN 6ba7b810-9dad-11d1-80b4-00c04fd430c8
URL 6ba7b811-9dad-11d1-80b4-00c04fd430c8
ISO object identifier (OID) 6ba7b812-9dad-11d1-80b4-00c04fd430c8
X.500 DN
(in DER or a text output format)
6ba7b814-9dad-11d1-80b4-00c04fd430c8

Version 4

The version 4 UUID is the most used and is meant for generating UUIDs from truly-random or pseudo-random numbers. ref doc

Because it's not device scoped, it provides better privacy properties.

The chance of collision are so small that it can be ignored.

Furthermore, Google Analytics use it to generate the Anonymous Id on the mobile platform.

Except bits 6, 7 and 12 through 15, all other bits are random:

The algorithm is explained in detail in the specification 4.4.

Example: version 4 (the 4 in 4e2b)

27701bfc-78d0-4e2b-92ca-193cea53fa30

Version 5

Version 5 is as version_3 a Name-based UUID but uses the sha1 hash. For more information, see the version_3 section.

Version 6

A time-ordered version with gregorian epoch proposed as new UUID format

Version 7

A time-ordered version with Unix epoch proposed as new UUID format.

Collision

The change of collision are so small 1) that it can be ignored.

For example, for the version-4 UUIDs, there is a 50% chance of collision when generating:

Format / Size

For all version (Ref), the UUID:

String

The format of an UUID hexadecimal string representation is in BNF syntax for bit.

'UUID' := 'time-low' "-" 'time-mid' "-" 'time-high-and-version' "-" 'clock-seq-and-reserved clock-seq-low' "-" 'node'

where:

Note that:

Because every application use them as URN, the bit sequence is generally converted as this string representation.

Pattern

regular expression pattern

const UUID4_PATTERN = "/^[0-9A-F]{8}-[0-9A-F]{4}-[4][0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i";

Specification

Implementation

Javascript Code

Javascript UUID4

console.log(window.crypto.randomUUID());
function uuidv4() {
  return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, c =>
    (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
  );
}

console.log(uuidv4());

Javascript Package

https://www.npmjs.com/package/uuid

Example:

let uuidString = uuid.v4()
console.log(`Hexadecimal String: ${uuidString}`);
console.log(`Hexadecimal String Character length: ${uuidString.length}`);
console.log(`Hexadecimal String Bit length: ${(uuidString.length -4 )* 4}`);
let uuidByteArray = uuid.parse(uuidString); // 16 bytes array
console.log(`Binary byte length: ${uuidByteArray.length}`);
console.log(`Binary bit length: ${uuidByteArray.length * 8}`);
console.log(uuidByteArray);

Windows SDK

The Windows SDK you get by installing Visual Studio also contains a GUID-generation tool, available, for example, at

%ProgramFiles%\Microsoft SDKs\Windows\v7.1A\Bin\uuidgen.exe
%ProgramFiles%\Windows Kits\8.1\bin\x86\uuidgen.exe

Database

In a database, you can store a UUID:

Storing the UUID in binary will make the data unreadable for humans. This is then not really practical to write a query.

The standard hexadecimal text representation can be shortened by:

If you still want to have a smaller id, you can always create one that suits your needs.

Query performance will not really suffer as even an index on text is using a binary representation. 4)

5)