Talking about the principle and advantages and disadvantages of UUID generation

UUID is a set of standards for generating globally unique identifiers, also known as GUID (Globally Unique Identifier). By using UUID, unique IDs can be generated in distributed systems. There are many ways to generate UUID. This article will explain in detail the generation principle, characteristics, practical scenarios, advantages and disadvantages of UUID.

First, the generation principle of UUID

The full English name of UUID is Universally Unique Identifier, that is, Universal Unique Identifier, which is an identifier composed of a group of 16 bytes (128 bits), which can be used to uniquely identify information. There are many ways to generate UUIDs, among which the most commonly used methods are algorithm-based UUID generation methods and hardware-based UUID generation methods. In the following, we will introduce the principles of these two generation methods respectively.

1. Algorithm-based UUID generation method

Algorithm-based UUID generation refers to the use of computer programs to generate UUIDs based on certain algorithms. The advantage of this method is that UUID can be generated in any environment, and does not need to depend on any hardware device. The disadvantage is that the generated UUID is not random enough and easy to be guessed, so it is not suitable for the security field.

Currently the most commonly used algorithm-based UUID generation method is the timestamp-based UUID generation method. This method generates a UUID based on the current timestamp and the MAC address of the machine. Its algorithm flow is as follows:

  1. Get the current timestamp and MAC address of the machine;
  2. Convert the current timestamp to UTC time, and calculate the number of nanoseconds since midnight on October 15, 1582 (0:00 GMT), and store it in the timestamp field of the UUID;
  3. Hash the MAC address of the machine to get 6 bytes of it as the node field of UUID;
  4. Randomly generate two bytes as the clock sequence field of UUID;
  5. Combine information such as timestamps, nodes, and clock sequences to generate UUIDs.

The timestamp-based UUID generation method can ensure that the UUID generated at the same time is unique and can provide a certain order, so it is very suitable for sorting data in a distributed system. However, if the clocks of multiple computers are different, it may cause duplicate UUIDs to be generated, so some measures need to be taken to prevent the problem of clocks being out of sync. Furthermore, this approach is also vulnerable to clock callback attacks and therefore requires special handling.

2. Hardware-based UUID generation method

The hardware-based UUID generation method refers to the use of computer hardware devices to generate UUIDs. The advantage of this method is that the generated UUIDs are highly random and not easy to be guessed. The disadvantage is that UUIDs can only be generated on machines with corresponding hardware devices, which is not flexible enough. .

Commonly used hardware-based UUID generation methods are MAC address UUID and CPU ID UUID.

MAC address UUID refers to using the MAC address of the computer network card device as the node field of UUID. This method can ensure that the UUID generated by each machine is unique and does not depend on clock synchronization. However, it also has some disadvantages, such as the computer's network card may be replaced, causing the UUID to change.

CPU ID UUID refers to using the serial number of the computer CPU as the node field of the UUID. This method is similar to the MAC address UUID, but it is more secure than the MAC address UUID because the CPU serial number will not change easily. However, since the CPU can be replaced, this approach also has certain risks.

2. Characteristics of UUID

UUID has the following characteristics:

1. Uniqueness

UUID is a globally unique identifier that can provide unique identification for distributed systems.

2. Randomness

The UUID generation process uses random or pseudo-random elements, and the generated UUID is highly random and not easy to guess.

3. Unpredictability

UUID is generated by a certain algorithm, and no information can be deduced from the generated UUID.

4. Reproducibility

UUIDs can be regenerated at different times and places, but in practice, due to the use of random numbers, the probability of duplication is very low.

5. Comparability

UUID is a 128-bit binary number, which can be compared, and the comparison has a certain order.

3. Practical scenarios of UUID

Due to the uniqueness and randomness of UUID, it has been widely used in many application scenarios. Here are some typical application scenarios.

1. Database primary key

In the database, each record needs a unique primary key to distinguish, usually you can use self-increasing ID as the primary key. But in a distributed system, ID conflicts may occur between multiple nodes. Therefore, you can use UUID as the primary key to ensure the uniqueness of each record.

2. Distributed system

In a distributed system, data needs to be distributed and stored on multiple nodes, and the data should be globally uniquely identified. At this time, UUID can be used to generate a unique ID for the data, so as to distinguish it in the whole system.

3. Log tracking

In a distributed system, the system operation log is an important basis for monitoring and troubleshooting. If each record in the log is marked with a unique identifier, it will be convenient for later analysis and tracking. In this case, UUID can be used to generate a unique ID for the log record.

4. Security area

In the field of security, unique identifiers need to be generated for user sessions, keys, and certificates to ensure security. At this time, UUID can be used to generate a unique identifier to ensure the distinction between different objects.

Fourth, the advantages and disadvantages of UUID

UUIDs have several advantages:

1. Globally unique

UUID can provide globally unique identifiers for distributed systems, avoiding the problem of ID conflicts.

2. Comparability

UUID is a number that can be compared and has a certain order.

3. No need for a central coordination mechanism

Generation of UUIDs does not require a central coordination mechanism, so UUIDs can be generated at any time and place.

4. High security

The generation of UUID uses random or pseudo-random elements, and the generated UUID is highly random and not easy to be guessed. Therefore, it can be used in the security field.

However, UUIDs also have several disadvantages:

1. Take up a lot of space

UUID is a 128-bit binary number, which occupies a relatively large space and is not suitable for use as a database primary key.

2. Difficult to read

Since UUID is a number, it is not easy for humans to read, and it is not convenient for debugging and troubleshooting.

3. Not suitable for sequential access

The generation of UUID is random, so it is not suitable for sequential access operations on data.

4. The algorithm is complex

The generation algorithm of UUID is relatively complicated, which affects performance to a certain extent.

5. Code example

 
 

java

copy code

import java.util.Random; public class GenerateUUID { private static final long START_EPOCH = -12219292800000L; // 定义版本号和变体标识的位数 private static final int VERSION_BITS = 4; private static final int VARIANT_BITS = 2; // 生成的 UUID 二进制形式共 128 位 private static final int TOTAL_BITS = 128; public static void main(String[] args) { Random random = new Random(); long timestamp = System.currentTimeMillis() + START_EPOCH; long leastSigBits = random.nextLong(); long mostSigBits = random.nextLong(); // 根据 RFC 4122 规范设置 UUID 版本号和变体标识 mostSigBits &= ~(0xfL << 12); // 清空版本号 mostSigBits |= (4L << 12); // 设置版本号 leastSigBits &= ~(0x3L << 62); // 清空变体标识 leastSigBits |= (0x2L << 62); // 设置变体标识 // 将时间戳写入 UUID 中段 long timeLow = timestamp & 0xffffffffL; long timeMid = (timestamp >> 32) & 0xffffL; long timeHiAndVersion = (timestamp >> 48) & 0x0fffL; mostSigBits &= ~(0xffffffffffffL << 64); // 清空时间戳 mostSigBits |= (timeLow << 32); mostSigBits |= (timeMid << 16); mostSigBits |= timeHiAndVersion; // 构造 UUID 对象 java.util.UUID uuid = new java.util.UUID(mostSigBits, leastSigBits); System.out.println(uuid.toString()); } }

In the above code, we first use  Random the class to generate two 64-bit pseudo-random numbers, which are used as the high 64 bits and low 64 bits of the UUID respectively. Then, according to the RFC 4122 specification, we mark the version number and the variant of the UUID, and write the millisecond part of the current timestamp into the middle segment of the UUID to ensure that the UUID has a certain sequence. Finally, we pass the high 64 bits and low 64 bits of the generated UUID  java.util.UUID into the constructor of the class to create a UUID object.

It should be noted that since we use pseudo-random numbers in the process of generating UUIDs, the generated UUIDs are not real random numbers, they only have pseudo-randomness. At the same time, since we do not guarantee the uniqueness of the timestamp, there may be a risk of duplication of UUIDs generated at the same time. In practical applications, we usually recommend using  java.util.UUID.randomUUID() method to generate UUID.

6. Summary

UUID is a set of standards for generating globally unique identifiers. It has characteristics such as uniqueness and randomness, and can be used in distributed systems. There are many ways to generate UUIDs, among which the most commonly used methods are algorithm-based UUID generation methods and hardware-based UUID generation methods. UUID has a wide range of applications in database primary keys, distributed systems, log tracking, and security fields. Although UUID has many advantages, it also has some disadvantages, such as taking up a large space, not easy to read, not suitable for sequential access, and complex algorithms. On the whole, UUID is a very useful way to generate identifiers. In actual development, the appropriate UUID generation method should be selected according to the specific situation.

 

Guess you like

Origin blog.csdn.net/BASK2312/article/details/131125660