Distributed unique ID generation method and device based on dynamic adjustment
By dynamically adjusting the number of digits in the ID in the distributed system and using the Redis in-memory database to allocate worker node IDs, the problem of fixed ID digits in traditional distributed systems is solved, thereby improving flexibility and security and enabling stable operation in high-concurrency environments.
Patent Information
- Application Number
- CN202511067289.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-07-31
- Publication Date
- 2025-11-14
AI Technical Summary
Traditional unique ID generation algorithms in distributed systems cannot dynamically adjust the number of bits in each field according to actual needs, and it is difficult to balance machine ID management and security in a distributed environment, especially when the network is unstable or nodes frequently go online and offline.
Unique IDs are generated by dynamically adjusting the number of bits in the timestamp, worker node ID, and millisecond concurrency count, combined with a Redis in-memory database and distributed locks. Specific steps include determining the number of bits based on application requirements, allocating worker node IDs using an ID bitmap from the Redis in-memory database, and ensuring security and uniqueness through encryption protocols and distributed lock mechanisms.
It enables dynamic adjustment of ID bit length according to actual needs, improving the flexibility and security of distributed systems, ensuring the efficiency and uniqueness of ID generation, and adapting to stable operation in high-concurrency environments.
Smart Images

Figure CN120950600A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of distributed system identifier technology, and in particular to a method and apparatus for generating distributed unique IDs based on dynamic adjustment. Background Technology
[0002] In modern distributed systems, generating unique identifiers (IDs) is a critical requirement. As system scale and concurrent requests increase, traditional ID generation algorithms include UUIDs (Universally Unique Identifiers) and database-auto-incrementing IDs. However, each method has its limitations. For example, while UUIDs guarantee uniqueness, their long length makes them inconvenient for storage and transmission. Database-auto-incrementing IDs rely on a single database, easily becoming a system bottleneck. In recent years, the Snowflake algorithm has been widely used in distributed ID generation due to its efficiency and flexibility. The Snowflake algorithm generates unique IDs by combining timestamps, machine IDs, and sequence numbers, enabling the rapid generation of short and unique identifiers in high-concurrency environments. However, traditional Snowflake algorithms still have room for improvement in terms of flexibility and security in distributed environments.
[0003] First, the traditional snowflake algorithm has a fixed number of bits in each field, which cannot be adjusted according to actual needs. This limits its applicability in certain application scenarios. For example, in some cases, higher timestamp precision may be required, while in others, the number of machine IDs may need to be increased. The fixed bit design cannot meet these dynamic requirements. Second, the snowflake algorithm relies on the uniqueness of machine IDs, and in a distributed environment, effectively managing and allocating machine IDs is a challenge. Traditional methods may not be able to strike a balance between efficiency and security, especially in situations of network instability or frequent node online / offline cycles. Summary of the Invention
[0004] This invention provides a method and apparatus for generating distributed unique IDs based on dynamic adjustment, which solves the technical problem in existing distributed systems where the number of digits in each field of a unique ID is fixed and cannot be adjusted according to actual needs. The technical solution is as follows:
[0005] In a first aspect, embodiments of the present invention provide a method for generating a distributed unique ID based on dynamic adjustment, comprising:
[0006] Determine the number of bits for the timestamp in the ID, the number of bits for the worker node ID, and the number of bits for millisecond concurrency based on application requirements;
[0007] Based on the registration request of the new node and the number of bits in the worker node ID, the worker node ID is determined according to the ID bitmap provided by the Redis in-memory database;
[0008] A unique ID is generated based on the real-time timestamp, the worker node ID, and the millisecond concurrency.
[0009] In some embodiments of the present invention, determining the worker node ID based on the ID bitmap provided by the Redis in-memory database includes:
[0010] Obtain the encrypted registration request for the new node;
[0011] Based on the encrypted registration request, the ID bitmap resource pool is locked according to the Redlock distributed lock in the Redis in-memory database;
[0012] Scan the ID bitmap resource pool, allocate the minimum available ID, and mark it;
[0013] Retrieve the returned minimum available marked ID, perform dynamic time-limited signature, and determine the worker node ID.
[0014] In some embodiments of the present invention, obtaining the encrypted registration request for a new node includes:
[0015] Asymmetric encryption algorithms are used for key negotiation and exchange to generate session keys;
[0016] The registration request data is encrypted using a symmetric encryption algorithm;
[0017] An encrypted registration request is obtained based on the session key and encrypted data.
[0018] In some embodiments of the present invention, it further includes:
[0019] Controls the data packets sent to the Redis in-memory database by nodes that are still active at preset time intervals;
[0020] Determine whether the node sends data packets to the Redis in-memory database at preset time intervals;
[0021] If not, mark the node as a failed node and reclaim the ID corresponding to the node from the Redis in-memory database.
[0022] In some embodiments of the present invention, it further includes:
[0023] Requests are distributed to multiple Redis in-memory databases based on application load and frequency.
[0024] In some embodiments of the present invention, the millisecond concurrency is determined as follows:
[0025] The upper limit of the millisecond concurrency counter is determined based on the number of bits in the millisecond concurrency count;
[0026] Initialize the millisecond concurrency counter and define it to start counting from 0;
[0027] Within the same millisecond, for each worker node ID determined by the same node, the millisecond concurrency counter is incremented by one, until the upper limit of the millisecond concurrency counter is reached.
[0028] In some embodiments of the present invention, it further includes:
[0029] Distributed locks and automatic retry strategies are used to resolve concurrency conflicts.
[0030] Secondly, the present invention also provides a distributed unique ID generation device based on dynamic adjustment, comprising:
[0031] The bit allocation module is used to determine the number of bits in the timestamp, the number of bits in the worker node ID, and the number of bits in the millisecond concurrency in the ID, according to application requirements;
[0032] The node ID determination module is used to determine the worker node ID based on the registration request of the new node and the number of digits in the worker node ID, according to the ID bitmap provided by the Redis in-memory database.
[0033] The unique ID generation module is used to generate a unique ID based on the real-time timestamp, the worker node ID, and the millisecond concurrency.
[0034] Thirdly, the present invention also provides an electronic device, comprising: a processor and a memory;
[0035] The memory stores a computer-readable program that can be executed by the processor;
[0036] When the processor executes the computer-readable program, it implements the steps in the dynamically adjusted distributed unique ID generation method described above.
[0037] Fourthly, the present invention also provides a computer-readable storage medium storing one or more programs that can be executed by one or more processors to implement the steps in the dynamically adjusted distributed unique ID generation method described above.
[0038] The beneficial effects of the technical solutions provided by some embodiments of the present invention include at least the following: First, the number of bits in the timestamp, the number of bits in the worker node ID, and the number of bits in the millisecond concurrency count are determined according to application requirements; thus, dynamic bit adjustment enables adjustments based on actual needs, improving the flexibility of the distributed system. Then, based on the registration request of a new node and the number of bits in the worker node ID, the worker node ID is determined according to the ID bitmap provided by the Redis in-memory database; thus, worker node ID allocation and uniqueness verification are implemented based on Redis; finally, a unique ID is generated based on the real-time timestamp, the worker node ID, and the millisecond concurrency count. Therefore, the present invention achieves dynamic allocation of the number of bits in each component of the unique ID and improves the reliability of secure and efficient ID management. Attached Figure Description
[0039] To more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0040] Figure 1 A system architecture diagram of an embodiment of the dynamically adjusted distributed unique ID generation method provided by the present invention;
[0041] Figure 2 A flowchart illustrating an embodiment of the dynamically adjusted distributed unique ID generation method provided by the present invention.
[0042] Figure 3 For the present invention Figure 2 In step S202, a method flowchart of one embodiment is shown;
[0043] Figure 4 This is a schematic diagram of an embodiment of the distributed unique ID generation device based on dynamic adjustment provided by the present invention;
[0044] Figure 5 This is a schematic diagram of the operating environment of an embodiment of the electronic device provided by the present invention. Detailed Implementation
[0045] To make the objectives, technical solutions, and advantages of this application clearer, the embodiments of this application will be described in further detail below with reference to the accompanying drawings.
[0046] It should be noted that the dynamically adjusted distributed unique ID generation method provided in this application is generally executed by the terminal device, and correspondingly, the dynamically adjusted distributed unique ID generation device is generally set in the terminal device.
[0047] Figure 1 An exemplary system architecture is shown that can be applied to the dynamically adjusted distributed unique ID generation method or apparatus of this application.
[0048] like Figure 1 As shown, the system architecture may include: terminal device 101 and server 102. Terminal device 101 and server 102 can communicate via a network, which serves as the medium for providing communication links between the various units. The network may include various types of wired or wireless communication links, such as: wired communication links including fiber optic cables, twisted-pair cables, or coaxial cables; and wireless communication links including Bluetooth communication links, Wi-Fi communication links, or microwave communication links.
[0049] It should be noted that the terminal device 101 and the server 102 can be either hardware or software. When the terminal device 101 and the server 102 are hardware, they can be implemented as a distributed server cluster consisting of multiple servers, or as a single server. When the terminal device 101 and the server 102 are software, they can be implemented as multiple software programs or software modules (e.g., used to provide distributed services), or as a single software program or software module; no specific limitations are made here.
[0050] The terminal device of this application can be equipped with various communication client applications, such as video recording applications, video playback applications, voice interaction applications, search applications, instant messaging tools, email clients, social platform software, etc.
[0051] A terminal device can be either hardware or software. When the terminal device is hardware, it can be various terminal devices with a display screen, including but not limited to smartphones, tablets, laptops, and desktop computers. When the terminal device is software, it can be installed on the terminal devices listed above. It can be implemented as multiple software programs or software modules (e.g., used to provide distributed services) or as a single software program or software module; no specific limitation is made here.
[0052] When the terminal device is hardware, it can also be equipped with a display device and a camera. The display device can be any device capable of displaying information, and the camera is used to capture video streams. For example, the display device can be a cathode ray tube display (CR), a light-emitting diode display (LED), an e-ink screen, a liquid crystal display (LCD), or a plasma display panel (PDP). Users can use the display device on the terminal device to view displayed text, images, videos, and other information.
[0053] It should be understood that Figure 1 The number of terminal devices, networks, and servers shown is for illustrative purposes only. Depending on implementation needs, there can be any number of terminal devices, networks, and servers.
[0054] The following will be combined with the appendix Figure 2 This application provides a detailed description of the dynamically adjusted distributed unique ID generation method provided in its embodiments. Specifically, the dynamically adjusted distributed unique ID generation device in these embodiments can be... Figure 1 The terminal device shown.
[0055] Please see Figure 2 The diagram below illustrates a dynamically adjusted distributed unique ID generation method as described in this application. Figure 2 As shown, the method described in this application embodiment may include the following steps:
[0056] S201. Determine the number of bits for the timestamp in the ID, the number of bits for the worker node ID, and the number of bits for millisecond concurrency based on application requirements;
[0057] It's important to note that the number of bits in the timestamp within the ID is adjusted according to system requirements to accommodate different time precisions and ID generation speeds, ensuring the uniqueness of IDs generated within a specified time range. The system first analyzes the application scenario's time precision requirements, such as whether it needs to support ID generation over long time spans. Based on the analysis results, the number of bits in the timestamp is determined. For example, for scenarios requiring millisecond accuracy, the timestamp may need to use 41 bits to cover a sufficient time range. The timestamp is then converted to a suitable storage format, ensuring its uniqueness within the specified time range.
[0058] It should be noted that when analyzing the number of distributed nodes in the system, the number of worker nodes in the current distributed environment needs to be assessed, and the possibility of future expansion should be considered. The number of bits in the worker node ID should be allocated appropriately based on the number of nodes. For example, if there are 1024 nodes in the system, 10 bits may be needed to ensure that each node has a unique identifier. Finally, node IDs are allocated using Redis or other distributed management tools to ensure their uniqueness and support dynamic adjustment. In this embodiment, Redis is chosen as the node allocation management tool.
[0059] It should be noted that the system evaluates the concurrent processing capability of each node to determine the maximum number of IDs that may be generated within the same millisecond. Through reasonable bit allocation, it ensures that IDs generated within the same millisecond do not conflict, supporting efficient ID distribution. In a specific embodiment, the millisecond concurrency count is determined as follows: the upper limit of the millisecond concurrency counter is determined based on the number of bits in the millisecond concurrency count; the millisecond concurrency counter is initialized and defined to start counting from 0; within the same millisecond, for each worker node ID determined by the same node, the count of the millisecond concurrency counter is incremented by one, until the upper limit of the millisecond concurrency counter is reached.
[0060] Specifically, lightweight atomic operations or locking mechanisms (such as Java's AtomicInteger) are used to ensure that the incrementing operation of the millisecond concurrency counter is thread-safe. In high-concurrency environments, lock contention is reduced and generation speed is improved through pre-allocation and caching optimization of sequence numbers.
[0061] Compared with existing technologies, this invention achieves flexibility and adaptability in ID generation by dynamically adjusting the number of bits in each field when the number of bits in the unique ID is fixed, thus significantly improving the system's scalability.
[0062] S202. Based on the registration request of the new node and the number of bits in the worker node ID, determine the worker node ID according to the ID bitmap provided by the Redis in-memory database;
[0063] It should be noted that a new node is a node newly added to the node cluster. The ID bitmap is a data structure used to store worker node IDs.
[0064] It's important to note that newly added worker nodes first establish a connection with the Redis server through a pre-configured secure channel, ensuring the security and reliability of communication. The node indicates its need for a unique workID by sending a specific formatted registration request message to Redis. This request message contains basic node information, such as the node identifier, network address, and other necessary metadata.
[0065] In one specific embodiment of the present invention, the step of determining the worker node ID based on the ID bitmap provided by the Redis in-memory database is described in detail below. Figure 3 ,include:
[0066] S301, Obtain the encrypted registration request for the new node;
[0067] S302. Based on the encrypted registration request, lock the ID bitmap resource pool according to the Redlock distributed lock of the Redis in-memory database;
[0068] S303. Scan the ID bitmap resource pool, allocate the minimum available ID, and mark it;
[0069] S304. Obtain the returned minimum available marked ID, perform dynamic time-limited signature, and determine the worker node ID.
[0070] In this embodiment, a custom encryption protocol is employed, combining symmetric and asymmetric encryption technologies to ensure the confidentiality of all communication data with Redis during transmission. Specifically, AES-256 symmetric encryption is used for data encryption to achieve efficient encryption processing, while RSA-2048 asymmetric encryption is used for key exchange to ensure the security of key transmission. Subsequently, a query is performed in the ID bitmap resource pool to find an unoccupied worker node ID. Furthermore, during worker node ID allocation, Redis uses a distributed locking mechanism to ensure allocation security in a concurrent environment, preventing multiple nodes from simultaneously acquiring the same worker node ID.
[0071] It should be noted that after data transmission using an encrypted protocol, Message Authentication Code (MAC) technology is used to ensure the integrity of the transmitted data. The HMAC-SHA256 algorithm is employed to verify the integrity of each data packet, ensuring that the data has not been tampered with during transmission.
[0072] S203. Generate a unique ID based on the real-time timestamp, the worker node ID, and the millisecond concurrency.
[0073] It's important to note that after successfully assigning a workID, the Redis server dynamically calculates the current concurrency level within milliseconds based on the request frequency of the current node and the system load, ensuring the uniqueness of IDs generated within the same millisecond. The assigned worker node ID, the obtained timestamp, and the calculated millisecond concurrency level are then combined bit-by-bit to construct a complete and unique ID. This combination process follows specific bit shifting and concatenation rules to ensure the orderliness and uniqueness of the ID.
[0074] In this embodiment of the invention, the number of bits in the timestamp, the number of bits in the worker node ID, and the number of bits in the millisecond concurrency count are first determined according to application requirements. This dynamic bit adjustment improves the flexibility of the distributed system by allowing adjustments based on actual needs. The worker node ID is then determined based on the registration request of a new node and the number of bits in the worker node ID, using the ID bitmap provided by the Redis in-memory database. This enables the allocation and uniqueness verification of worker node IDs based on Redis. Finally, a unique ID is generated based on the real-time timestamp, the worker node ID, and the millisecond concurrency count. Furthermore, this embodiment employs an advanced asynchronous communication mechanism combined with an event-driven architecture design to ensure communication efficiency and reliability in high-concurrency environments. Through asynchronous I / O and non-blocking network communication, the system can efficiently handle a large number of concurrent requests. Through these multi-layered security measures, the system ensures high security and data integrity in communication with Redis, supporting secure and efficient operation in complex distributed environments.
[0075] In some embodiments of the present invention, it further includes:
[0076] Controls the data packets sent to the Redis in-memory database by nodes that are still active at preset time intervals;
[0077] Determine whether the node sends data packets to the Redis in-memory database at preset time intervals;
[0078] If not, mark the node as a failed node and reclaim the ID corresponding to the node from the Redis in-memory database.
[0079] In this embodiment, worker nodes periodically send heartbeats to Redis to indicate that they are still active. Redis updates the node's activity status based on the heartbeat information, ensuring that the worker node ID is not reclaimed during the node's active period. If a node does not send a heartbeat within a specified time, Redis will mark the workID as reclaimable for reallocation. The validity of the node's worker ID (WorkID) enables real-time monitoring of the node's status.
[0080] In some embodiments of the present invention, it further includes:
[0081] Requests are distributed to multiple Redis in-memory databases based on application load and frequency.
[0082] In this embodiment, under high concurrency, the system employs an optimized polling strategy to ensure rapid response to co-working node ID requests. By implementing non-blocking polling, the system can process a large number of requests in a short time, reducing waiting time and improving response speed. The polling interval and strategy are dynamically adjusted based on the current system load and request frequency. Through an adaptive algorithm, the system can optimize the polling frequency in real time, ensuring optimal performance under different load conditions.
[0083] Furthermore, the system distributes worker node ID requests across multiple Redis instances, using a round-robin strategy to achieve load balancing. By monitoring the load status of each instance, the system can intelligently allocate requests, avoiding single points of overload. It also employs an intelligent scheduling algorithm to dynamically adjust the request allocation strategy based on the real-time load of the instances, ensuring load balancing across all Redis instances and improving the overall system's response efficiency.
[0084] Furthermore, during the worker node ID allocation process, the system uses Redis's distributed locking mechanism (such as the Redlock algorithm) to ensure uniqueness. Through distributed locks, the system prevents multiple nodes from simultaneously acquiring the same worker node ID, maintaining data consistency and integrity. To avoid deadlock, the locking mechanism includes an automatic timeout function. If a node fails to complete worker node ID allocation within a specified time, the lock will be automatically released, ensuring system stability. If a conflict is detected during worker node ID allocation, the system automatically triggers a retry mechanism. Through an exponential backoff algorithm, the system gradually increases the retry interval, avoiding conflicts without impacting system performance. The system monitors the worker node ID allocation status in real time, detecting potential conflicts. If a conflict is detected, a conflict resolution process is immediately initiated to ensure that each node obtains a unique worker node ID. Through a strict mutual exclusion control mechanism, the system ensures the accuracy of worker node ID allocation in high-concurrency environments. Utilizing distributed locks and retry strategies, the system effectively manages concurrent requests, avoiding conflicts and resource contention. By comprehensively applying locking mechanisms, retry strategies, and dynamic scheduling, the system maintains stable operation under high-concurrency conditions, ensuring the accuracy and reliability of worker node ID allocation.
[0085] In some embodiments of the present invention, the system also includes real-time monitoring of the system's request rate, identifying abnormal traffic spikes or sudden request surges, tracking the system's error rate (including request failures, timeouts, ID generation conflicts, etc.), and identifying potential system failures. It also monitors the usage of resources such as CPU, memory, and network bandwidth to identify resource bottlenecks that may lead to performance degradation. Furthermore, it measures the system's average and maximum response times to identify performance issues that may degrade user experience.
[0086] Subsequently, reasonable thresholds are set for each key indicator, triggering alerts when these thresholds are exceeded. Machine learning or statistical methods are used to identify abnormal patterns, such as a sudden increase in request volume or a consistently high error rate. Real-time risk warnings are pushed to administrators via email, SMS, or system notifications. Detailed risk reports are provided, including risk type, scope of impact, and possible causes, to help administrators quickly locate problems. An automatic scaling strategy is configured to automatically adjust resource allocation based on real-time load. Upon detecting certain failures, automatic recovery operations are performed, such as restarting the service or switching to a standby node.
[0087] Thus, this invention, through three core designs—"dynamic ID allocation protocol + layered secure communication protocol + intelligent heartbeat keep-alive protocol"—combined with distributed lock optimization, load-aware polling, and conflict avoidance mechanisms, upgrades Redis from a simple caching tool into a distributed ID coordination center, a real-time load scheduler, and a high-concurrency conflict arbitrator, ultimately achieving breakthroughs in the flexibility, security, and scalability of distributed systems.
[0088] This invention offers the following advantages: it dynamically adjusts the number of bits in the ID, allowing for flexible configuration of the ID structure according to the needs of different application scenarios. This flexibility not only improves the system's adaptability and resource utilization but also effectively meets the specific needs of different industries and applications. It effectively solves the problems of insufficient flexibility, security risks, and limited high-concurrency processing capabilities in existing distributed ID generation technologies, providing an efficient, reliable, and flexible solution for distributed systems.
[0089] The following are embodiments of the apparatus described in this application, which can be used to execute the embodiments of the method described in this application. For details not disclosed in the apparatus embodiments of this application, please refer to the embodiments of the method described in this application.
[0090] Please see Figure 4 This illustration shows a schematic diagram of a dynamically adjusted distributed unique ID generation device provided in an exemplary embodiment of this application, hereinafter referred to as device 400. Device 400 can be implemented as all or part of a terminal device through software, hardware, or a combination of both. Device 400 includes:
[0091] The bit allocation module 410 is used to determine the number of bits in the timestamp, the number of bits in the worker node ID, and the number of bits in the millisecond concurrency in the ID according to application requirements;
[0092] The node ID determination module 420 is used to determine the worker node ID based on the registration request of the new node and the number of bits in the worker node ID, according to the ID bitmap provided by the Redis in-memory database.
[0093] The unique ID generation module 430 is used to generate a unique ID based on the real-time timestamp, the worker node ID, and the millisecond concurrency.
[0094] This application also provides a computer storage medium that can store multiple instructions, which are adapted to be loaded and executed by a processor as described above. Figure 2 The method steps of the illustrated embodiment can be found in the following documentation for detailed execution. Figure 2 The specific details of the illustrated embodiments will not be elaborated here.
[0095] This application also provides a computer program product that stores at least one instruction, which is loaded and executed by the processor to implement the dynamically adjusted distributed unique ID generation method as described in the above embodiments.
[0096] Please see Figure 5 This document provides a schematic diagram of the structure of a terminal device according to an embodiment of this application. Figure 5 As shown, the terminal device 500 may include: at least one processor 501, at least one network interface 504, user interface 503, memory 505, and at least one communication bus 502.
[0097] The communication bus 502 is used to enable communication between these components.
[0098] The user interface 503 may include a display screen and a camera. Optionally, the user interface 503 may also include a standard wired interface and a wireless interface.
[0099] The network interface 504 may optionally include a standard wired interface or a wireless interface (such as a Wi-Fi interface).
[0100] The processor 501 may include one or more processing cores. The processor 501 connects to various parts within the terminal device 500 using various interfaces and lines, and performs various functions and processes data by running or executing instructions, programs, code sets, or instruction sets stored in the memory 505, and by calling data stored in the memory 505. Optionally, the processor 501 may be implemented using at least one hardware form of Digital Signal Processing (DSP), Field-Programmable Gate Array (FPGA), or Programmable Logic Array (PLA). The processor 501 may integrate one or a combination of several of the following: Central Processing Unit (CPU), Graphics Processing Unit (GPU), and modem. The CPU primarily handles the operating system, user interface, and applications; the GPU is responsible for rendering and drawing the content required for display; and the modem handles wireless communication. It is understood that the modem may also be implemented as a separate chip, without being integrated into the processor 501.
[0101] The memory 505 may include random access memory (RAM) or read-only memory. Optionally, the memory 505 may include a non-transitory computer-readable storage medium. The memory 505 can be used to store instructions, programs, code, code sets, or instruction sets. The memory 505 may include a program storage area and a data storage area, wherein the program storage area may store instructions for implementing an operating system, instructions for at least one function (such as touch function, sound playback function, image playback function, etc.), instructions for implementing the above-described method embodiments, etc.; the data storage area may store data involved in the above-described method embodiments, etc. Optionally, the memory 505 may also be at least one storage device located remotely from the aforementioned processor 501. Figure 5 As shown, the memory 505, which serves as a computer storage medium, may include an operating system, a network communication module, a user interface module, and application programs.
[0102] exist Figure 5In the terminal device 500 shown, the user interface 503 is mainly used to provide an input interface for the user and to obtain the user's input data; while the processor 501 can be used to call the application program stored in the memory 505 and specifically execute, such as Figure 2 The method shown can be referred to for details. Figure 2 As shown, it will not be elaborated further here.
[0103] Those skilled in the art will understand that all or part of the processes in the above embodiments can be implemented by a computer program instructing related hardware. The program can be stored in a computer-readable storage medium, and when 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, or random access memory, etc.
[0104] The above description discloses only preferred embodiments of the present invention and should not be construed as limiting the scope of the present invention. Therefore, equivalent variations made in accordance with the claims of the present invention are still within the scope of the present invention.
Claims
1. A method for generating distributed unique IDs based on dynamic adjustment, characterized in that, include: Determine the number of bits for the timestamp in the ID, the number of bits for the worker node ID, and the number of bits for millisecond concurrency based on application requirements; Based on the registration request of the new node and the number of bits in the worker node ID, the worker node ID is determined according to the ID bitmap provided by the Redis in-memory database; A unique ID is generated based on the real-time timestamp, the worker node ID, and the millisecond concurrency.
2. The distributed unique ID generation method based on dynamic adjustment according to claim 1, characterized in that, The step of determining the worker node ID based on the ID bitmap provided by the Redis in-memory database includes: Obtain the encrypted registration request for the new node; Based on the encrypted registration request, the ID bitmap resource pool is locked according to the Redlock distributed lock in the Redis in-memory database; Scan the ID bitmap resource pool, allocate the minimum available ID, and mark it; Retrieve the returned minimum available marked ID, perform dynamic time-limited signature, and determine the worker node ID.
3. The method for generating a distributed unique ID based on dynamic adjustment according to claim 2, characterized in that, The encrypted registration request for obtaining a new node includes: Asymmetric encryption algorithms are used for key negotiation and exchange to generate session keys; The registration request data is encrypted using a symmetric encryption algorithm; An encrypted registration request is obtained based on the session key and encrypted data.
4. The distributed unique ID generation method based on dynamic adjustment according to claim 1, characterized in that, Also includes: Controls the data packets sent to the Redis in-memory database by nodes that are still active at preset time intervals; Determine whether the node sends data packets to the Redis in-memory database at preset time intervals; If not, mark the node as a failed node and reclaim the ID corresponding to the node from the Redis in-memory database.
5. The distributed unique ID generation method based on dynamic adjustment according to claim 1, characterized in that, Also includes: Requests are distributed to multiple Redis in-memory databases based on application load and frequency.
6. The distributed unique ID generation method based on dynamic adjustment according to claim 1, characterized in that, The method for determining the millisecond concurrency is as follows: The upper limit of the millisecond concurrency counter is determined based on the number of bits in the millisecond concurrency count; Initialize the millisecond concurrency counter and define it to start counting from 0; Within the same millisecond, for each worker node ID determined by the same node, the millisecond concurrency counter is incremented by one, until the upper limit of the millisecond concurrency counter is reached.
7. The distributed unique ID generation method based on dynamic adjustment according to claim 1, characterized in that, Also includes: Distributed locks and automatic retry strategies are used to resolve concurrency conflicts.
8. A distributed unique ID generation device based on dynamic adjustment, characterized in that, include: The bit allocation module is used to determine the number of bits in the timestamp, the number of bits in the worker node ID, and the number of bits in the millisecond concurrency in the ID, according to application requirements; The node ID determination module is used to determine the worker node ID based on the registration request of the new node and the number of digits in the worker node ID, according to the ID bitmap provided by the Redis in-memory database. The unique ID generation module is used to generate a unique ID based on the real-time timestamp, the worker node ID, and the millisecond concurrency.
9. An electronic device, characterized in that, include: Processor and memory; The memory stores a computer-readable program that can be executed by the processor; When the processor executes the computer-readable program, it implements the steps in the dynamically adjusted distributed unique ID generation method as described in any one of claims 1-7.
10. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores one or more programs, which can be executed by one or more processors to implement the steps in the dynamically adjusted distributed unique ID generation method as described in any one of claims 1-7.