A distributed unique ID generation method, device and electronic equipment

By adjusting the machine ID format and using a cache array to pre-generate IDs, the problem of duplicate IDs in the snowflake algorithm during clock rollback was solved, achieving efficient and reliable distributed unique ID generation and significantly increasing the number of generated IDs.

CN116227436BActive Publication Date: 2026-02-24北京自如信息科技有限公司
View PDF 3 Cites 0 Cited by

Patent Information

Application Number
CN202310287538.4
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2023-03-22
Publication Date
2026-02-24
Estimated Expiration
2043-03-22

AI Technical Summary

Technical Problem

The snowflake algorithm is prone to generating duplicate IDs when the server clock rolls back, leading to chaotic ID allocation on worker machines.

Method used

The machine ID format is set to start with 0, the first preset number of digits represents the machine number, the last preset number of digits represents the serial number, the sum of the two preset numbers is equal to 63, the serial number range is 36 to 38 digits, and the serial number is incremented by 1 to generate a new ID when the machine is not restarted. When the machine restarts, both the machine number and the serial number are incremented by 1. A cache array is used to pre-generate backup IDs, and a single-threaded task is configured to obtain the ID.

Benefits of technology

In the event of server time rollback or machine restart, the generated machine IDs are unique, exceeding the 4096 limit with a minimum of 68.7 billion IDs. This avoids ID generation lag and duplication issues, improving ID generation efficiency and reliability.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN116227436B_ABST
    Figure CN116227436B_ABST
Patent Text Reader

Abstract

The application discloses a distributed unique ID generation method, device and electronic equipment, and the method comprises the following steps: setting the format of a machine ID as the first bit being 0, and setting the first preset bits from the second bit to represent a machine number, and the last preset bits to represent a serial number; the sum of the first preset bits and the last preset bits is equal to 63, and the value range of the last preset bits is 36-38; when generating the machine ID for a certain machine, it is judged whether the current machine is restarted; if the current machine is not restarted, the serial number in the last generated machine ID is added by 1 to generate the current machine ID; and the current machine ID is assigned to the current machine. The technical scheme provided by the application avoids the problem of generating repeated IDs when the server clock backtracking occurs.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of computer technology, and more specifically to a distributed unique ID generation method, apparatus, and electronic device. Background Technology

[0002] Most current internet services are distributed and microservice-based, and the large volume of data has led to database sharding and table partitioning. For horizontal partitioning, it's crucial to ensure the global uniqueness of IDs within the tables. The Snowflake algorithm, an open-source distributed ID generation algorithm from Twitter, is used to generate unique IDs across different machines. For example... Figure 1 As shown, the Snowflake algorithm generates a unique 64-bit long-type ID for each machine. The first bit is unused (0 is not used). In binary, the first bit represents positive or negative, with 0 indicating a positive value. The 41-bit timestamp represents the current time in milliseconds, ranging from 0 to (2^41) - 1 milliseconds. Each millisecond generates an ID in an auto-incrementing manner, equivalent to 69 years. In other words, the Snowflake algorithm guarantees no duplicate IDs will be generated within 69 years. The next 10 bits are the machine code, consisting of a data center number and a machine number, representing 2^10 = 1024 different machines. Typically, the first five bits represent the data center number, and the last five bits represent the machine number. The final 12 bits represent the sequence number. If the same machine generates multiple IDs within the same millisecond, their first 52 bits are identical (timestamp, data center number, and machine number are all identical), allowing the sequence number to distinguish the generated IDs. In the current millisecond, the first generated ID sequence number is 0, the second is 1, and so on, representing a maximum of 2^12 = 4096 numbers. Therefore, a single machine can generate a maximum of 4096 IDs per millisecond. In other words, the Snowflake algorithm can support a maximum of 1024 machines simultaneously generating 4096 unique IDs per millisecond within 69 years. However, the main drawback of the Snowflake algorithm is that it relies on server time. When the server experiences clock rollback, duplicate IDs are generated, causing ID allocation chaos among the working machines. Summary of the Invention

[0003] In view of this, embodiments of the present invention provide a distributed unique ID generation method, apparatus and electronic device, thereby avoiding the problem of generating duplicate IDs when the server experiences clock rollback.

[0004] According to a first aspect, embodiments of the present invention provide a distributed unique ID generation method, the method comprising: setting the format of a machine ID to have the first digit as 0, and setting a preset number of digits starting from the second digit to represent the machine number and a preset number of digits starting from the second digit to represent the serial number, wherein the sum of the preset number of digits starting from the second digit and the preset number of digits starting from the second digit is equal to 63, and the value range of the preset number of digits starting from the second digit is 36 to 38; when generating a machine ID for a certain machine, determining whether the current machine has restarted; if the current machine has not restarted, incrementing the serial number in the previously generated machine ID by 1 to generate the current machine ID; and assigning the current machine ID to the current machine.

[0005] Optionally, before assigning the current machine ID to the current machine, the method further includes: if the current machine restarts, incrementing both the machine number and the sequence number in the previously generated machine ID by 1 to generate the current machine ID.

[0006] Optionally, the machine number is 25 bits and the serial number is 38 bits.

[0007] Optionally, the step of incrementing the serial number in the previously generated machine ID by 1 to generate the current machine ID if the current machine has not restarted includes: pre-generating multiple backup machine IDs if the current machine has not restarted, and adding the backup machine IDs to a cache array; wherein each backup machine ID is different from the others, and each backup machine ID is obtained by combining the current machine number with a different serial number; extracting a target ID from the cached backup machine IDs as the current machine ID, wherein the machine number in the target ID is the same as the machine number in the previously generated machine ID, and is incremented by 1 relative to the serial number in the previously generated machine ID.

[0008] Optionally, the method further includes: checking the remaining amount of backup machine IDs in the cache array at a preset period; if the remaining amount is lower than a preset percentage of the cache array capacity, then continuing to pre-generate multiple backup machine IDs to fill the cache array.

[0009] Optionally, the cache array capacity is 65536 IDs.

[0010] Optionally, the task of generating the machine ID can be configured as a single-threaded task.

[0011] According to a second aspect, embodiments of the present invention provide a distributed unique ID generation device, the device comprising: a format configuration module, configured to set the format of the machine ID to have the first digit as 0, and to set a preset number of digits starting from the second digit to represent the machine number and a preset number of digits starting from the second digit to represent the serial number, wherein the sum of the preset number of digits starting from the first digit and the preset number of digits starting from the second digit equals 63, and the value range of the preset number of digits starting from the second digit is 36 to 38; a restart judgment module, configured to determine whether the current machine has restarted when generating a machine ID for a certain machine; an ID generation module, configured to increment the serial number in the previously generated machine ID by 1 if the current machine has not restarted, to generate the current machine ID; and an ID allocation module, configured to assign the current machine ID to the current machine.

[0012] According to a third aspect, embodiments of the present invention provide an electronic device, including: a memory and a processor, wherein the memory and the processor are communicatively connected to each other, the memory stores computer instructions, and the processor executes the computer instructions to perform the method described in the first aspect, or any optional embodiment of the first aspect.

[0013] According to a fourth aspect, embodiments of the present invention provide a computer-readable storage medium storing computer instructions for causing a computer to perform the method described in the first aspect, or any optional embodiment of the first aspect.

[0014] The technical solution provided in this application has the following advantages:

[0015] The technical solution provided in this application sets the machine ID format so that the first digit is 0, and the first preset number of digits (starting from the second digit) represents the machine number, and the last preset number of digits represents the serial number. The sum of the first and last preset number of digits equals 63, and the value of the last preset number of digits ranges from 36 to 38. When generating a machine ID for a machine, if the current machine has not restarted, the previously generated machine ID is retrieved, and a new machine ID is generated by incrementing the serial number by 1 while keeping the machine number unchanged. Finally, the new machine ID is assigned to the current machine. In this way, even if the machine has not restarted, regardless of whether the server time has been rolled back, the serial number is incremented by 1 whenever a new ID is generated. Since the serial number has a relatively large number of digits (36-38), the number of generated machine IDs exceeds the 4096 limit, and at least 68.7 billion machine IDs can be generated. This not only meets user needs for the number of IDs generated within a certain time period, but also ignores the impact of time rollback. Attached Figure Description

[0016] The features and advantages of the invention will be more clearly understood by referring to the accompanying drawings, which are schematic and should not be construed as limiting the invention in any way. In the drawings:

[0017] Figure 1 This diagram illustrates the machine ID format of the snowflake algorithm in the prior art.

[0018] Figure 2 The diagram illustrates the steps of a distributed unique ID generation method according to one embodiment of the present invention.

[0019] Figure 3 A schematic diagram of the machine ID format of a distributed unique ID generation method according to one embodiment of the present invention is shown.

[0020] Figure 4 A schematic diagram of a distributed unique ID generation device according to one embodiment of the present invention is shown.

[0021] Figure 5 A schematic diagram of an electronic device according to one embodiment of the present invention is shown. Detailed Implementation

[0022] To make the objectives, technical solutions, and advantages of the embodiments of the present invention clearer, the technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only a part of the embodiments of the present invention, and not all of them. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.

[0023] For tasks using the Snowflake algorithm to generate distributed unique IDs, the phenomenon of server time rollback is the main reason why the Snowflake algorithm generates incorrect IDs. Clock rollback means that the server's time suddenly reverts to a previous time. For example, suppose the timestamp corresponding to the time 2023-03-01-12:00:00 is 1001, the assigned machine number is 001, and the generated sequence number is 001, resulting in a machine ID of 1001001001. If the current actual time is 2023-03-01-12:00:01, but due to a machine system error, the server time rolls back to 2023-03-01-12:00:00, the machine number remains unchanged at 001 because the corresponding timestamp is still 1001. This will also generate an ID with the sequence number 001, resulting in 1001001001, causing duplicate IDs generated on 2023-03-01-12:00:01. To address this problem, embodiments of the present invention provide the following solution.

[0024] Please see Figure 2 In one implementation, a distributed unique ID generation method specifically includes the following steps:

[0025] Step S101: Set the format of the machine ID to 0 for the first digit, and set the first preset number of digits starting from the second digit to represent the machine number and the last preset number of digits to represent the serial number. The sum of the first preset number of digits and the last preset number of digits is equal to 63, and the value range of the last preset number of digits is 36 to 38.

[0026] Step S102: When generating a machine ID for a machine, determine whether the current machine needs to be restarted.

[0027] Step S103: If the current machine has not restarted, increment the serial number in the previously generated machine ID by 1 to generate the current machine ID;

[0028] Step S104: Assign the current machine ID to the current machine.

[0029] Specifically, to ensure the quantity of machine IDs, the Snowflake algorithm generates new machine IDs by changing a 12-bit sequence number in a one-to-one correspondence with a 41-bit timestamp, while the middle 10-bit machine number remains unchanged. In this embodiment, as... Figure 3 As shown, a 64-bit machine ID is defined as two parts: the machine number and the serial number, with the timestamp removed. The machine ID format is as follows: the first bit is 0, indicating a positive number; the remaining 63 bits represent the machine number (first preset bits) and the serial number (last preset bits), with the serial number ranging from 36 to 38 bits. When a machine needs to generate a new machine ID while remaining powered on, the server device used for ID allocation receives the generation request and initiates the process of generating and allocating machine IDs. In this process, regardless of whether the server's time is rolled back, the current machine number remains unchanged and the serial number is incremented by 1 whenever a new ID is generated. Assuming a 36-bit serial number, if a new ID is generated every millisecond, it can be generated continuously for 2^36 / 1000 / 60 / 60 / 24 / 365≈2.18 years. If the serial number is 38 bits, it can last for 8.72 years, which generally meets a company's ID quantity requirements. By following the steps above, the serial number no longer needs to change one-to-one with the timestamp, thus solving the problem of duplicate machine IDs caused by server time rollback.

[0030] Specifically, in this embodiment of the invention, considering that the number of machines in a company is usually no more than 3,000, a machine ID format of 25 bits for the machine number and 38 bits for the serial number is adopted to increase the number of machine IDs that can be generated simultaneously. Assuming that a machine needs to generate multiple IDs simultaneously within one millisecond, the serial number capacity of 2^38 ≈ 274.8 billion places almost no limit on the number of machine IDs generated simultaneously, which is far greater than the limitation of the Snowflake algorithm of generating 4,096 machine IDs simultaneously.

[0031] Specifically, in one embodiment, the distributed unique ID generation method provided by this invention further includes the following steps:

[0032] Step 1: If the current machine restarts, increment both the machine number and serial number in the previously generated machine ID by 1 to generate the current machine ID.

[0033] Specifically, considering the adjustment of the machine ID format in this embodiment, the timestamp portion has been removed. To further ensure that the machine ID generated after a machine restart is not duplicated with the machine ID generated before the restart, when the machine restarts and obtains a new machine ID, this embodiment of the invention simultaneously increments both the machine number and the serial number in the machine ID by 1, thereby further increasing the number of unique machine IDs generated. For example, when the machine number is 25 digits, it is equivalent to having 2^25 = 33,554,432 machine numbers available. Assuming the company's business requires 3,000 machine instances, and each machine restarts 3 times a day, requiring a new machine number to be generated each time, the 33,554,432 machine numbers can be used for 33,554,432 / 3,000 / 3 = 3,728 days, equivalent to 10.2 years. In other words, when the machine restarts, the newly generated machine ID increments both the machine number and the serial number by 1 compared to the previous machine ID. When the machine does not restart, the machine number of the newly generated machine ID remains unchanged compared to the previous machine ID, only the serial number increments by 1. In practical applications, there is usually only a requirement that the machine ID assigned to each machine must be unique, but there is no requirement that the machine number must correspond one-to-one with the physical machine. Therefore, the distributed unique ID generation method provided by the above steps can significantly increase the number of unique machine IDs generated, and the IDs before and after the machine restart will not be duplicated.

[0034] Specifically, in one embodiment, step S103 above includes the following steps:

[0035] Step 2: If the current machine has not restarted, generate multiple backup machine IDs and add them to the cache array; each backup machine ID is different and is obtained by combining the current machine number with a different serial number.

[0036] Specifically, for application scenarios without restarting, this embodiment of the invention pre-stores a large number of backup machine IDs by configuring a cache array. The backup machine IDs are combined in a manner where the current machine number remains unchanged and the sequence number increments automatically. For example, assuming the current machine number is 1001 and the sequence number has 3 digits, the sequence numbers, in auto-incrementing order, include 001, 002, 003, ..., 999. The cache array pre-stores 1001001, 1001002, ..., 1001999 backup machine IDs.

[0037] Step 3: Extract the target ID from the cached backup machine ID as the current machine ID. The machine number in the target ID is the same as the machine number in the previously generated machine ID, and is 1 greater than the sequence number in the previously generated machine ID.

[0038] Specifically, if the current machine needs to generate a new machine ID, this embodiment of the invention only needs to retrieve it from the cache array according to the principle that the newly generated machine ID is the same as the machine number in the previously generated machine ID and the sequence number in the previously generated machine ID is increased by 1. Thus, through the above steps, a large number of IDs are pre-cached when machine ID generation is not required, and the corresponding ID is directly retrieved from the cache when machine ID generation is needed. This significantly improves the efficiency of machine ID generation and avoids the ID generation task from stalling when the processor is performing multitasking.

[0039] Specifically, in one embodiment, the cache array capacity is set to 65,536 IDs to meet the needs of most task scenarios.

[0040] Specifically, in one embodiment, the distributed unique ID generation method provided by this invention further includes the following steps:

[0041] Step 4: Check the remaining amount of standby machine IDs in the cache array at a preset period.

[0042] Step 5: If the remaining amount is lower than the preset percentage of the cache array capacity, continue to pre-generate multiple spare machine IDs to fill the cache array.

[0043] Specifically, in this embodiment, a data filling thread is also configured to continuously check the number of remaining spare machine IDs in the cache array at a preset period. When the number of remaining spare machine IDs in the cache array is lower than a preset percentage of the cache array capacity (e.g., lower than 50%), the data filling thread will automatically pre-generate new spare machine IDs to fill the cache array, thereby ensuring the reliability of the generated machine IDs and ensuring the global efficiency of the distributed unique ID generation method.

[0044] Specifically, in one embodiment, the task of generating machine IDs in this embodiment is configured as a single-threaded task. Specifically, the Snowflake algorithm uses a fixed 10-bit machine ID, with only the 41-bit timestamp and 12-bit sequence number being variable. The 12-bit sequence number can store a maximum of 4096 IDs. A machine can generate a maximum of 4096 IDs per millisecond. If this value is reached, the program spins and waits for the next millisecond before generating a new distributed ID. Therefore, when more than 4096 IDs are obtained per millisecond, thread blocking occurs until the timer catches up, at which point a new ID is generated. To avoid thread blocking, the Snowflake algorithm often supports multi-threaded ID acquisition. However, the sequence number in this embodiment contains 36-38 bits, so the upper limit on the number of machine IDs can be ignored, eliminating blocking scenarios. Since the operation is performed in memory, a single-threaded ID acquisition method is chosen, simplifying code development and avoiding the false sharing problem and high complexity associated with multi-threaded development.

[0045] Through the above steps, the technical solution provided in this application sets the machine ID format so that the first digit is 0, and sets the first preset number of digits (starting from the second digit) to represent the machine number and the last preset number of digits to represent the serial number. The sum of the first and last preset number of digits equals 63, and the value range of the last preset number of digits is 36 to 38. When generating a machine ID for a machine, if the current machine has not restarted, the previously generated machine ID is obtained, and a new machine ID is generated by incrementing the serial number by 1 while keeping the machine number unchanged. Finally, the new machine ID is assigned to the current machine. In this way, under the condition that the machine has not restarted, regardless of whether the server time has been rolled back, the serial number is incremented by 1 whenever a new ID is generated. Since the serial number has a number of digits between 36 and 38, the number of generated machine IDs exceeds the 4096 limit, and at least 68.7 billion machine IDs can be generated. This not only meets the user's needs for the number of IDs generated within a certain period of time, but also ignores the impact of time rollback.

[0046] Furthermore, this embodiment of the invention uses a cache array to pre-generate backup machine IDs, eliminating the need for calculations when obtaining IDs and resulting in faster speed. The single-threaded ID acquisition code is simple to develop and maintain, providing a unified and easy-to-use tool for ID acquisition. Additionally, the snowflake algorithm uses a fixed machine number, which can lead to duplicate IDs if a clock rollback occurs and the machine restarts. Even without a restart, it may block while waiting for the current time to catch up with the last ID generation time. In this embodiment, the 25-bit machine number can vary, and a new, incrementing machine number is obtained with each restart. Therefore, when the machine restarts, a unique machine number is obtained, and the subsequent 38-bit sequence number continues to increment from 0, ensuring no duplicate IDs when the number of IDs does not exceed 274.8 billion. Furthermore, this embodiment uses a 38-bit sequence number, eliminating the concept of time, thus limiting the number of IDs generated per millisecond.

[0047] like Figure 4 As shown, this embodiment also provides a distributed unique ID generation device, the device comprising:

[0048] The format configuration module 101 is used to set the format of the machine ID to 0 as the first digit, and to set the first preset number of digits starting from the second digit to represent the machine number and the last preset number of digits to represent the serial number. The sum of the first preset number of digits and the last preset number of digits is equal to 63, and the value range of the last preset number of digits is 36 to 38. For details, please refer to the relevant description of step S101 in the above method embodiment, which will not be repeated here.

[0049] The restart judgment module 102 is used to determine whether the current machine needs to restart when a machine ID is generated for a certain machine. For details, please refer to the relevant description of step S102 in the above method embodiment, which will not be repeated here.

[0050] ID generation module 103 is used to increment the serial number in the previously generated machine ID by 1 if the current machine has not been restarted, and generate the current machine ID. For details, please refer to the relevant description of step S103 in the above method embodiment, which will not be repeated here.

[0051] The ID allocation module 104 is used to assign the current machine ID to the current machine. For details, please refer to the relevant description of step S104 in the above method embodiment, which will not be repeated here.

[0052] This invention provides a distributed unique ID generation device for executing a distributed unique ID generation method provided in the above embodiments. Its implementation and principle are the same. For details, please refer to the relevant descriptions of the above method embodiments, which will not be repeated here.

[0053] Through the collaborative efforts of the aforementioned components, the technical solution provided in this application sets the machine ID format so that the first digit is 0, and the first preset number of digits, starting from the second digit, represents the machine number, and the last preset number of digits represents the serial number. The sum of the first and last preset number of digits equals 63, and the value of the last preset number of digits ranges from 36 to 38. When generating a machine ID for a machine, if the current machine has not restarted, the previously generated machine ID is retrieved, and a new machine ID is generated by incrementing the serial number by 1 while keeping the machine number unchanged. Finally, the new machine ID is assigned to the current machine. In this way, even if the machine has not restarted, regardless of whether the server time has been rolled back, the serial number is incremented by 1 whenever a new ID is generated. Since the serial number has a relatively large number of digits (36-38), the generated machine IDs exceed the 4096 limit, and at least 68.7 billion machine IDs can be generated. This not only meets user needs for the number of IDs generated within a certain time period but also ignores the impact of time rollback.

[0054] Figure 5 An electronic device according to an embodiment of the present invention is shown. The device includes a processor 901 and a memory 902, which can be connected via a bus or other means. Figure 5 Taking the example of a connection between China and Israel via a bus.

[0055] Processor 901 can be a Central Processing Unit (CPU). Processor 901 can also be other general-purpose processors, digital signal processors (DSPs), application-specific integrated circuits (ASICs), field-programmable gate arrays (FPGAs), or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, or combinations of the above types of chips.

[0056] The memory 902, as a non-transitory computer-readable storage medium, can be used to store non-transitory software programs, non-transitory computer-executable programs, and modules, such as the program instructions / modules corresponding to the methods in the above method embodiments. The processor 901 executes various functional applications and data processing of the processor by running the non-transitory software programs, instructions, and modules stored in the memory 902, thereby implementing the methods in the above method embodiments.

[0057] The memory 902 may include a program storage area and a data storage area. The program storage area may store the operating system and applications required for at least one function; the data storage area may store data created by the processor 901, etc. Furthermore, the memory 902 may include high-speed random access memory and may also include non-transitory memory, such as at least one disk storage device, flash memory device, or other non-transitory solid-state storage device. In some embodiments, the memory 902 may optionally include memory remotely located relative to the processor 901, and these remote memories may be connected to the processor 901 via a network. Examples of such networks include, but are not limited to, the Internet, corporate intranets, local area networks, mobile communication networks, and combinations thereof.

[0058] One or more modules are stored in memory 902, and when executed by processor 901, they perform the methods described in the above method embodiments.

[0059] The specific details of the aforementioned electronic device can be understood by referring to the relevant descriptions and effects in the above method embodiments, and will not be repeated here.

[0060] Those skilled in the art will understand that all or part of the processes in the methods of the above embodiments can be implemented by a computer program instructing related hardware. The implemented program can be stored in a computer-readable storage medium. When the program is executed, it can include the processes of the embodiments of the above methods. The storage medium can be a magnetic disk, optical disk, read-only memory (ROM), random access memory (RAM), flash memory, hard disk drive (HDD), or solid-state drive (SSD), etc.; the storage medium can also include combinations of the above types of memory.

[0061] Although embodiments of the invention have been described in conjunction with the accompanying drawings, those skilled in the art can make various modifications and variations without departing from the spirit and scope of the invention, and such modifications and variations all fall within the scope defined by the appended claims.

Claims

1. A distributed unique ID generation method, characterized in that, The method includes: The machine ID is set to have 0 as the first digit, and the first preset number of digits starting from the second digit represents the machine number, and the last preset number of digits represents the serial number. The sum of the first preset number of digits and the last preset number of digits is equal to 63, and the value range of the last preset number of digits is 36 to 38. When generating a machine ID for a machine, determine whether the current machine has been restarted; If the current machine has not restarted, increment the serial number in the previously generated machine ID by 1 to generate the current machine ID. This step includes: if the current machine has not restarted, pre-generating multiple backup machine IDs and adding them to a cache array; each backup machine ID is unique, and each backup machine ID is obtained by combining the current machine ID with a different serial number; extracting a target ID from the cached backup machine IDs as the current machine ID, where the machine number in the target ID is the same as the machine number in the previously generated machine ID, and is incremented by 1 relative to the serial number in the previously generated machine ID. If the current machine restarts, increment both the machine number and serial number in the previously generated machine ID by 1 to generate the current machine ID; Assign the current machine ID to the current machine.

2. The method according to claim 1, characterized in that, The machine number is 25 bits, and the serial number is 38 bits.

3. The method according to claim 1, characterized in that, The method further includes: The remaining amount of standby machine IDs in the cache array is checked at a preset period. If the remaining amount is lower than a preset percentage of the cache array capacity, then multiple backup machine IDs will be pre-generated to fill the cache array.

4. The method according to claim 3, characterized in that, The cache array has a capacity of 65,536 IDs.

5. The method according to claim 1, characterized in that, Configure the task of generating machine IDs as a single-threaded task.

6. A distributed unique ID generation device, characterized in that, The device includes: The format configuration module is used to set the format of the machine ID to 0 as the first digit, and to set the first preset number of digits starting from the second digit to represent the machine number and the last preset number of digits to represent the serial number. The sum of the first preset number of digits and the last preset number of digits is equal to 63, and the value range of the last preset number of digits is 36~38. The restart detection module is used to determine whether the machine needs to be restarted when a machine ID is generated for a certain machine. The ID generation module is used to generate the current machine ID by incrementing the serial number of the previously generated machine ID by 1 if the current machine has not restarted; and by incrementing both the machine number and serial number of the previously generated machine ID by 1 if the current machine has restarted. The step of incrementing the serial number of the previously generated machine ID by 1 if the current machine has not restarted includes: pre-generating multiple backup machine IDs and adding them to a cache array; each backup machine ID is unique, and each backup machine ID is obtained by combining the current machine number with a different serial number; extracting a target ID from the cached backup machine IDs as the current machine ID, wherein the machine number in the target ID is the same as the machine number in the previously generated machine ID, and is incremented by 1 relative to the serial number in the previously generated machine ID. The ID allocation module is used to assign the current machine ID to the current machine.

7. An electronic device, characterized in that, include: A memory and a processor, the memory and the processor being communicatively connected to each other, the memory storing computer instructions, the processor executing the computer instructions to perform the method as described in any one of claims 1-5.

8. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores computer instructions for causing the computer to perform the method as described in any one of claims 1-5.

Citation Information

Patent Citations

  • Distributed global unique ID generation method and device

    CN108804545A

  • Distributed ID generation system

    CN114244807A

  • Distributed system incremental digital ID generation method

    CN115577042A