Skip to content

UUID

1. UUID (Universally Unique IDentifier)

[Figure 1] UUID Format

[Figure 1] UUID Format

UUID, as its name implies, means an ID that is unique anywhere. [Figure 1] shows the Format of a UUID. It contains a total of 32 characters in groups of 8,4,4,4,12, each character is a hexadecimal digit, and including the Dashes it consists of 36 characters. Expressed in Bits, a UUID consists of “32*4=128” Bits.

Since it consists of so many characters, even if each Server generates UUIDs with arbitrary values, the probability of collision between the generated UUIDs is very low. Therefore, when an App uses UUIDs, the App often uses them without performing duplicate checks on the UUIDs.

A UUID has a Version depending on how the UUID is generated, and there is a 4-Bit Version Field in the UUID that stores the Version information. Versions 1 through 5 currently exist, and [Figure 1] shows Version 4. A UUID also has a Variant that contains the Format information of the UUID, and there is a 3-Bit Variant Field in the UUID.

1.1. Version

A UUID has a Version depending on how it is generated. There are v1/v2 based on TimeStamp, v4 based on complete Randomness, and v3/v5 based on Hashing. In practice, however, v1/v4/v5 are mainly used. To generate a Random UUID, v1/v4 can be used, and to generate a fixed UUID, v5 can be used.

1.1. v1

[Figure 2] UUID v1

[Figure 2] UUID v1

A v1 UUID is generated based on a Timestamp. [Figure 2] shows the generation process of a v1 UUID. In the front part, the Timestamp is split up and included in the UUID. The Timestamp is a value that increases by 1 every 100ns starting from midnight on October 15, 1582. In the rear part, the MAC Address of the Computer generating the UUID is stored.

Because of this v1 UUID generation method, if an App running on the same Computer generates multiple UUIDs within 100ns, all the UUIDs become identical. Therefore, an App that generates many UUIDs in a short period of time should avoid using v1 UUIDs. Since the Mac Address is stored in the rear part of a v1 UUID, it is possible to trace which Computer generated a given v1 UUID through the v1 UUID.

1.2. v4

A v4 UUID is generated based on complete Randomness. All Fields of the UUID except the Version and Variant Fields are generated completely at Random to form the v4 UUID.

1.3. v5

A v5 UUID is generated based on SHA-1 Hashing. To generate a v5 UUID, a Namespace and a Name value are required. If the Namespace and Name values are the same, the same UUID is generated. The Namespace values are defined as follows, and values other than the defined ones can also be used.

  • NAMESPACE_DNS : The Name is a Domain name.
  • NAMESPACE_URL : The Name is a URL.
  • NAMESPACE_OID : The Name is an OID (Object Identitfier).
  • NAMESPACE_X500 : The Name is a Directory Name of the LDAP Protocol.

1.4. v2

A v2 UUID is based on a Timestamp like a v1 UUID, but the Timestamp Field is reduced and Domain/Identifier Fields are added. The Timestamp value of a v2 UUID increases by 1 only after about 7 minutes. Since a v2 UUID is not a general-purpose UUID but a UUID for DCE (Distributed Computing Environment) environments, it is rarely used.

1.5. v3

A v5 UUID is the same as a v3 UUID, except that it performs Hashing using the MD5 Hahsing Algorithm. The MD5 Hashing Algorithm is currently in a state where Reverse Hashing is easily possible through its security vulnerabilities. Therefore, using a v5 UUID rather than a v3 UUID is currently recommended.

2. with DB

v1/v4 UUIDs are also widely used as the PK (Primary Key) of DB Tables. There are advantages and disadvantages compared to using a typical Integer Type ID. Compared to using a typical Integer Type ID, they have the following advantages.

  • Since a UUID has the characteristic of being Unique in all environments, there is no need to worry about UUID collisions when merging Tables. Therefore, Tables can be freely merged. Conversely, it is also easy to freely split one Table into multiple Tables.
  • Data-related information cannot be inferred from the content of a UUID alone. For example, if there is a User whose Integer Type ID value is 100, it can be inferred that the User is the 100th created User, but such inference is impossible with a UUID.
  • A UUID does not necessarily have to be generated in the Database. A UUID generated by the App can be used in the DB.

Compared to using a typical Integer Type ID, they have the following disadvantages.

  • As the UUID is long, more storage space is required. An Integer Type ID uses 4 Bytes, but a UUID requires 16 Bytes, which is 4 times larger.
  • Since the generation of v1/v4 UUIDs is random, Indexing takes more time when Inserting into the DB. From the DB’s point of view, a monotonically increasing value is ideal for the ID (PK). A v1 UUID is better than a v4 UUID because it is Timestamp-based, but since a v1 UUID is also not perfectly monotonically increasing, DB Indexing takes longer compared to an Integer Type ID.

3. References