Skip to content

Note Of Universally Unique Identifier (UUID)

Last Updated on 2024-06-01 by Clay

Introduction

When assigning identifiers to our data, if we want each data to have a unique identifier rather than a simple sequential number, UUID is the most common method we used.

So, what is UUID?

UUID (Universally Unique Identifier) is a standard identifier used to ensure uniqueness in the computer system. Typically, an UUID is a 128-bit number, which guarantees a high probability of uniqueness, making the chance of duplication nearly zero. Therefore, in practical applications, there is usually no need to check whether assigned UUIDs are duplicated.

The common standard

The most commonly used standard for UUIDs is defined by the Internet Engineering Task Force (IETF) in RFC 4122. The standard specifies five different versions of UUIDs:

  • UUIDv1: Generated based on a timestamp, includes a timestamp, clock sequence, and node (usually a MAC address).
  • UUIDv2: Similar to UUIDv1, but add POSIX UID/GID.
  • UUIDv3: Generated based on namespace and name, used MD5 hash (similar to UUIDv5)
  • UUIDv4: Randomly generated, with most of the bits being random.
  • UUIDv5: Generated based on namespace and name, used SHA-1 hash (similar to UUIDv3)

Among these, UUIDv4 is the most commonly used. Due to the majority of its bits being randomly generated, it also maintains a high degree of uniqueness.


UUID Structure

The structure of UUID is very clearly. If you watch the similar structure you will know in one second: “Oh, it’s an UUID!”

As previously mentioned, an UUID has 128-bit (16 bytes), and divided into 5 sections, separated by hyphens (-).

123e4567-e89b-12d3-a456-426614174000


這五個部份依序為:

  1. The first 8 characters (32 bits)
  2. The next 4 characters (16 bits)
  3. The next 4 characters (16 bits)
  4. The next 4 characters (16 bits)
  5. The last 12 characters (48 bits)

How To Generate

Generate UUID is a easy job, many programming language have the related library, such as Python’s uuid module.

import uuid

# UUIDv1
uuid1 = uuid.uuid1()
print(uuid1)

# UUIDv4
uuid4 = uuid.uuid4()
print(uuid4)



References


Read More

Tags:

Leave a Reply