Order ID generation method based on high concurrency environment
By using a mapping mechanism between timestamps and random strings and synchronization lock control, combined with service node identifiers, the problem of frequent order ID conflicts in high-concurrency systems is solved, achieving the uniqueness of order IDs and the stability of the system. This approach is suitable for large-scale e-commerce platforms and financial transaction systems.
Patent Information
- Application Number
- CN202511170206.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-08-20
- Publication Date
- 2025-11-21
AI Technical Summary
In high-concurrency systems, the order ID generation process suffers from frequent order ID conflicts, a lack of unique identification mechanisms for service nodes, and a lack of concurrency control measures, making it difficult to guarantee system stability and consistency.
A mapping mechanism between timestamps and random strings, combined with synchronization lock control, is used to generate order IDs. Service node identifier letters are introduced, and the uniqueness and stability of order IDs are ensured through deduplication verification and maximum number of attempts control.
It effectively avoids duplicate order IDs, ensures the independent identification capability of each node in the distributed system, improves the reliability and stability of the generation process, and solves the problems of data corruption and system anomalies caused by the lack of concurrency control in traditional solutions.
Smart Images

Figure CN120996907A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of generation technology, and in particular to a method for generating order IDs in a high-concurrency environment. Background Technology
[0002] In today's rapidly evolving business scenarios such as e-commerce, online payment, logistics, and financial transactions, order generation and management, as one of the core system processes, directly impact transaction smoothness and system stability. The order number (order ID) not only uniquely identifies each business operation but also forms the foundation for subsequent core processes such as order tracking, payment verification, and data reconciliation. Therefore, achieving fast, stable, and unique order ID generation while ensuring system throughput is a critical and challenging technical task in high-concurrency system architecture design.
[0003] Currently, mainstream order ID generation methods typically include combinations of fields such as timestamps, random numbers, server IDs, and business type codes. Among these, the "timestamp + random number" method is widely used in small and medium-sized systems due to its simplicity and flexible format. However, in high-concurrency, large-scale distributed system environments, random number generation strategies, lacking global state awareness, are prone to order ID collisions within the same time period, leading to serious problems such as order processing failures, data corruption, and even system crashes. Furthermore, in multi-service node deployment scenarios, without a proper node identification or coordination mechanism, IDs generated by different nodes may still be duplicated, failing to meet the technical requirements of high consistency and high reliability in order processing.
[0004] Therefore, in the order ID generation process of high-concurrency systems, the frequent occurrence of order ID conflicts, the lack of a unique identification mechanism for service nodes, and the lack of concurrency control measures have become technical challenges that urgently need to be solved. Summary of the Invention
[0005] This application provides an order ID generation method based on a high-concurrency environment, aiming to solve the problems of frequent order ID conflicts, lack of unique identification mechanism for service nodes, and lack of concurrency control measures in the order ID generation process of existing technologies in high-concurrency systems.
[0006] A method for generating order IDs in a high-concurrency environment, the method comprising:
[0007] Get the current timestamp, the timestamp format is "yyyyMMddHHmmss";
[0008] Using the timestamp as the key, search for the corresponding set of random strings in the key-value pair structure;
[0009] Generate a string of random letters of length 3, wherein the random letters do not include the letters "p", "l" and "x";
[0010] Determine whether the random letter string already exists in the random string set corresponding to the current timestamp. If it exists, regenerate it until a unique random string is generated.
[0011] The generated non-repeating random string is added to the random string set corresponding to the current timestamp, and the key-value pair is written into the memory storage structure;
[0012] The order ID is obtained by concatenating the current operating environment identifier letter, the last 12 characters of the timestamp, a non-repeating random letter string, and the current service node identifier letter;
[0013] Optionally, in the above scheme, the timestamp is in seconds and in the format "yyyyMMddHHmmss" to identify the time when the order ID was generated.
[0014] In the above scheme, optionally, the random letter string is randomly generated from a preset set of allowed letters, which is a set of 26 English letters excluding the letters "p", "l" and "x".
[0015] Optionally, in the above scheme, the generation of the random string has a maximum number of attempts threshold. If a unique string cannot be generated after exceeding a preset number of attempts, the generation process is terminated.
[0016] Optionally, in the above scheme, the mapping relationship between the random string and the timestamp is stored in a single-element key-value structure that only records the current timestamp and its corresponding value.
[0017] Optionally, in the above scheme, the method is executed under a synchronization lock mechanism to ensure the uniqueness of the order ID in a concurrent environment; the synchronization lock mechanism is based on a memory-level lock object to achieve synchronous control of the order ID generation process.
[0018] Optionally, in the above scheme, the service node identifier letter is a single character, and is any one numeric character from 0 to 9, used to distinguish different service nodes in the distributed environment.
[0019] In the above scheme, optionally, the order of concatenation of the order ID is: runtime environment identifier letters + the last 12 characters of the timestamp + the random string + the service node identifier letters.
[0020] Optionally, in the above scheme, the runtime environment identifier letter is used to distinguish the runtime environment in which the order ID is generated, including but not limited to the development environment, testing environment and production environment.
[0021] Optionally, in the above scheme, the order ID generation method is encapsulated in a callable interface method, which receives the runtime environment configuration and service node identifier as input parameters and outputs the order ID string.
[0022] Compared with the prior art, this application has at least the following beneficial effects:
[0023] Based on further analysis and research of existing technologies, this application recognizes the problems of frequent order ID conflicts, lack of unique identification mechanisms for service nodes, and lack of concurrency control measures in the order ID generation process of high-concurrency systems. By adopting a mapping mechanism between timestamps and random strings, combined with synchronization lock control, this application achieves unique order ID generation in high-concurrency environments, effectively avoiding the problem of duplicate IDs generated in concurrency conflicts using the traditional "timestamp + random number" method. Simultaneously, by introducing service node identifier letters, each service node in the distributed system possesses independent identification capabilities, avoiding the possibility of order ID conflicts between nodes. The random string generation process includes deduplication verification and a maximum attempt control mechanism, further enhancing the reliability and stability of ID generation. The entire process runs under the protection of synchronization locks, ensuring the atomicity of the order ID generation process in a multi-threaded execution environment, fundamentally solving the data corruption and system anomaly problems caused by the lack of concurrency control in traditional solutions. In summary, this method effectively solves the key problems mentioned in the background technology, such as duplicate order IDs, lack of identification between nodes, and chaotic generation logic under concurrency, and is suitable for high-concurrency business scenarios such as large-scale e-commerce platforms and financial transaction systems. Attached Figure Description
[0024] Figure 1 This is a flowchart illustrating an order ID generation method based on a high-concurrency environment, provided as an embodiment of this application. Detailed Implementation
[0025] To make the objectives, technical solutions, and advantages of this application clearer, the following detailed description is provided in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are merely illustrative and not intended to limit the scope of this application.
[0026] In the description of this application: unless otherwise stated, "a plurality of" means two or more. The terms "first," "second," "third," etc., in this application are intended to distinguish the objects referred to and do not have any special meaning in terms of technical connotation (e.g., they should not be construed as an emphasis on importance or order). Expressions such as "comprising," "including," and "having" also mean "not limited to" (certain units, components, materials, steps, etc.).
[0027] In one embodiment, such as Figure 1As shown, a method for generating order IDs in a high-concurrency environment is provided, including the following steps:
[0028] Step 1: Obtain the current timestamp, the timestamp format is "yyyyMMddHHmmss";
[0029] Step 2: Using the timestamp as the key, search for the corresponding set of random strings in the key-value pair structure;
[0030] Step 3: Generate a set of random letter strings of length 3, wherein the random letters do not include the letters "p", "l" and "x";
[0031] Step 4: Determine whether the random letter string already exists in the random string set corresponding to the current timestamp. If it exists, regenerate it until a unique random string is generated.
[0032] Step 5: Add the generated non-repeating random string to the random string set corresponding to the current timestamp, and write the key-value pair into the memory storage structure;
[0033] Step 6: Concatenate the current operating environment identifier letters, the last 12 characters of the timestamp, a non-repeating random letter string, and the current service node identifier letters to obtain the order ID;
[0034] Step 7: Steps 2 to 6 are executed under a synchronization lock mechanism to ensure the uniqueness of the order ID in a concurrent environment.
[0035] This embodiment obtains the current system time through a time function, extracts a timestamp string in the format "yyyyMMddHHmmss", such as "20250729113421", and uses this timestamp as a unique key in the subsequent mapping structure. This timestamp has second-level granularity, dividing the order IDs generated within each second of the system into independent time intervals, providing a basis for subsequent random string deduplication. Next, a predefined key-value pair structure in memory is used to store the mapping relationship between the current timestamp and the corresponding set of generated random strings. In one specific implementation, this mapping structure adopts a "SingleEntry" structure, storing only the timestamp used in the most recent generation operation and the set of random strings allocated under that timestamp, greatly simplifying memory usage and query complexity. Subsequently, a random string generation function is called to generate a random English letter string of length 3 according to preset rules. To reduce character confusion and improve human recognition, the allowed character set is the 26 English letters excluding the three easily ambiguous letters "p", "l", and "x". After each generation, the system first performs a deduplication check on the random string set corresponding to the current timestamp. If the string already exists, it is regenerated until a unique string is generated. In the deduplication mechanism, the system attempts to generate a string a maximum of N times (e.g., 100 times). If a unique string is not generated after exceeding the number of attempts, an exception message will be thrown and the process will be terminated to prevent entering an infinite loop.
[0036] To ensure that the above generation process does not generate resource contention or write conflicts in a high-concurrency, multi-threaded environment, the entire generation logic is executed under the protection of a synchronized lock mechanism. The system uses a specific lock object (such as a mapping structure instance) for locking control, ensuring that only one thread can enter the generation process at a time. This mechanism guarantees the atomicity and consistency of the order ID generation process, effectively avoiding the risk of multiple threads generating the same order ID.
[0037] After generating the random string, the system concatenates the current timestamp, the unique random string, and the system runtime environment identifier and service node identifier letters according to a predetermined format to form the final order ID. The runtime environment identifier letters are typically used to distinguish between development, testing, and production environments; for example, "D" represents development and "P" represents production, effectively preventing cross-environment confusion. The service node identifier letters are used to identify different server nodes in a distributed deployment scenario, such as nodes 0, 1, and 2, and are assigned numeric characters between 0 and 9 to ensure that order IDs generated by different service nodes at the same timestamp are unique.
[0038] This embodiment ensures concurrency safety through a synchronization lock mechanism, achieves local uniqueness through a mapping between timestamps and random string sets, improves readability by excluding specified characters, and achieves global uniqueness through distributed service node identifiers. This method does not rely on traditional methods such as databases or global service coordination. It is simple to deploy, has high performance, and strong scalability, making it suitable for scenarios requiring the generation of a large number of high-frequency order IDs, such as microservice architectures, e-commerce platforms, and payment systems.
[0039] This embodiment uses second-level timestamps as the basic generation unit and ensures the serialization of the generation logic through synchronization locks, fundamentally eliminating data overwriting and ID conflict issues caused by concurrent threads writing at the same time. Secondly, through the deduplication of random string generation method, combined with the maximum number of attempts mechanism, even if hundreds of order IDs are generated concurrently within the same second, the uniqueness of each string can be guaranteed. Thirdly, service node identifier letters are introduced, so that each node in the distributed system can independently generate non-repeating order IDs without the need for a central coordination service, greatly simplifying the deployment complexity.
[0040] Therefore, the order ID generation method not only has good scalability, but also significantly improves the stability, uniqueness and overall throughput of the order ID generation process, effectively solving the practical problems faced by existing technologies in high-concurrency scenarios.
[0041] In this embodiment, the timestamp is in seconds and is in the format "yyyyMMddHHmmss", used to identify the time when the order ID was generated.
[0042] The timestamp, with a granularity of seconds and formatted as "yyyyMMddHHmmss", is used to identify the generation time of the order ID. In practice, the system calls a standard time function to obtain the current local time and uses a predefined time format template to format the time object as a string of "year + month + day + hour + minute + second", i.e., "yyyyMMddHHmmss", such as "20250729114035". This timestamp, as a crucial component of the order ID, serves two purposes: first, it provides differentiation for each group of orders in the basic time dimension, preventing overlap of order IDs generated at different times; second, it acts as the key in the key-value mapping structure, uniquely identifying all order generation operations within one second, providing a time-dimensional basis for deduplication of random strings. Choosing "seconds" as the granularity ensures sufficient time interval control while avoiding the performance overhead and system complexity caused by excessively high granularities (such as milliseconds or microseconds), and also facilitates operations in scenarios such as order traceability and system log recording. By using a unified and appropriately precise second-level timestamp, the implementation logic is simplified and the system maintainability is enhanced. It also lays a stable foundation for order ID grouping and deduplication under high concurrency conditions. It can effectively cooperate with random string generation strategies to achieve unique control of order IDs in the time dimension, thereby solving the ID duplication problem caused by chaotic time formats and coarse granularity in the background technology.
[0043] In this embodiment, the random letter string is randomly generated from a preset set of allowed letters, which is a set of 26 English letters excluding the letters "p", "l" and "x".
[0044] The random letter string is randomly generated from a preset set of allowed letters, which is a set of 26 English letters excluding the letters "p", "l", and "x". In actual implementation, the system predefines a character set as the input character pool for the random string generation function. This character pool consists of 26 English letters, with three commonly misread characters, "p", "l", and "x", manually removed. The letter "l" is easily confused with the number "1" in most fonts, "p" is similar in shape to "q" and "b", and "x" has reserved meaning in some business codes (such as for special marking). Therefore, excluding it can improve the accuracy of order IDs when manually read or identified by third-party systems in terminal systems. The random string generation function randomly selects 3 characters from this character pool each time to form the target string, and uses a deduplication mechanism to ensure that there are no duplicates under the current timestamp. By limiting the range of the character pool, not only is the readability and anti-confusion capability of the order ID improved in different terminal systems, but the risks of system input errors and increased difficulty of manual intervention caused by character ambiguity are also avoided. Meanwhile, this measure has a negligible impact on randomness and does not affect the overall distribution uniformity, enabling higher quality order ID coding while ensuring system stability.
[0045] In this embodiment, the generation of the random string has a maximum number of attempts threshold. If a unique string cannot be generated after exceeding the preset number of attempts, the generation process is terminated.
[0046] The generation of random strings has a maximum retry threshold. If a unique string cannot be generated after exceeding the preset number of attempts, the generation process is terminated. In the specific implementation, after each random string is generated at the current timestamp, the system checks for duplicates in the corresponding string set. If a duplicate is found, the string is regenerated. To prevent the inability to generate a new string for an extended period under extreme conditions (such as excessively high concurrency or a small character pool), the system sets a maximum retry threshold, such as 100 times. If a unique string cannot be generated after 100 consecutive attempts, the system throws an exception or logs an error, notifying the business processing flow to perform degradation processing or wait for the next second's timestamp before retrying. This mechanism prevents the system from getting stuck in an infinite loop under extreme high concurrency pressure, leading to resource blockage. The maximum retry count, as a safety mechanism, ensures the robustness of the system during peak periods and prevents the random string generation module from consuming unlimited CPU resources due to abnormal data density. At the same time, it also provides system administrators with anomaly detection indicators. Once retry failure events occur frequently, they can be used to dynamically adjust optimization strategies such as character set size and order frequency control, thereby enhancing the system's fault tolerance and significantly improving the system's stability and controllability.
[0047] In this embodiment, the mapping relationship between the random string and the timestamp is stored in a single-element key-value structure that only records the current timestamp and its corresponding value.
[0048] The mapping relationship between the random strings and timestamps is stored in a single-element key-value structure that records only the current timestamp and its corresponding value. In the specific implementation, the system uses a lightweight inner class structure called SingleEntry, which stores only the current processing timestamp and the set of random strings generated at that time. When the system detects a timestamp change (i.e., entering the next second), it immediately replaces the old key with the new timestamp and clears or overwrites the corresponding set of random strings. Compared with traditional hash tables, cache queues, and other general structures, the SingleEntry structure only stores the most relevant set of mapping information, significantly reducing system memory consumption and cleanup burden. This structure provides an extremely efficient performance optimization path for high-frequency order ID generation, avoiding resource redundancy issues caused by time window accumulation. Furthermore, its "one-second refresh" storage method is highly compatible with the timestamp granularity design, minimizing and maximizing the efficiency of the data storage structure while ensuring that random strings are not duplicated. This is beneficial for resource control in large-scale deployments and multi-threaded concurrent scenarios.
[0049] In this embodiment, the synchronization lock mechanism is based on a memory-level lock object to achieve synchronous control of the order ID generation process.
[0050] The synchronization lock mechanism, based on a memory-level lock object, implements synchronized control over the order ID generation process. In its implementation, the system uses a built-in synchronization mechanism from object-oriented languages (such as Java) to achieve mutual exclusion control between threads. Specifically, the `synchronized` keyword is used within the order ID generation method to lock shared resources, which are random string mapping structures (such as SingleEntry instances). Whenever a thread attempts to generate an order ID, it must first acquire access to the lock object. Other threads that have not acquired the lock will enter a blocked state until the current thread completes the order ID generation operation and releases the lock, at which point other threads can enter sequentially. This memory-level lock control method has significant advantages: firstly, it does not rely on external coordination services (such as distributed lock services, database transactions, etc.), resulting in high execution efficiency; secondly, since the lock object only exists in the memory of the current service process, the acquisition and release of the lock have almost no performance overhead, making it suitable for high-frequency call scenarios. Furthermore, this mechanism ensures that the read and update of SingleEntry mapping data during each ID generation process is atomic, preventing data overwriting or duplicate ID generation issues caused by interleaved thread execution in concurrent scenarios.
[0051] By introducing a memory-level synchronization lock mechanism, the system can guarantee the thread safety of each generation path even under high-concurrency access environments, completely avoiding problems such as conflicts, duplication, and data consistency anomalies that may occur when multiple threads operate on the same data structure simultaneously. This is particularly suitable for modules with high QPS (queries per second) services that have high data consistency requirements, such as e-commerce order entry points and payment transaction creation. Compared to lock-free or lightweight lock designs, this solution balances system performance and stability, effectively avoiding concurrency defects while ensuring response speed, providing a reliable concurrency control foundation for the entire order ID generation process.
[0052] In this embodiment, the service node identifier letter is a single character, which is any one numeric character from 0 to 9, used to distinguish different service nodes in the distributed environment.
[0053] In this embodiment, the order of concatenation of the order ID is: runtime environment identifier letters + the last 12 characters of the timestamp + the random string + the service node identifier letters.
[0054] In this embodiment, the runtime environment identifier letters are used to distinguish the runtime environment in which the order ID is generated, including but not limited to the development environment, testing environment, and production environment.
[0055] In this embodiment, the order ID generation method is encapsulated in a callable interface method. The interface method receives the runtime environment configuration and service node identifier as input parameters and outputs the order ID string.
[0056] In one embodiment, an order ID generation method is proposed, which achieves efficient and unique order ID generation by introducing strategies such as synchronization lock mechanism, timestamp and random string mapping, and service node identification.
[0057] The order ID structure in this embodiment includes: runtime environment identifier letter + timestamp (yyyyMMddHHmmss) + random 3-digit letter (excluding p, l, x) + service node identifier letter (0-9).
[0058] The specific implementation steps are as follows:
[0059] Use synchronization locks to ensure the atomicity of the order ID generation process;
[0060] Use the timestamp as the mapping key and a random 3-letter character as the mapping value, and store them in key-value pairs;
[0061] When generating random letters, check if the key-value pair already has a set of letters with the corresponding timestamp. If duplicates exist, regenerate until no duplicates are found.
[0062] Each service node is assigned a unique identifier letter (0-9) which is added to the end of the order ID to achieve global uniqueness across nodes.
[0063] This embodiment effectively avoids the problem of duplicate order IDs by using a synchronization lock and a timestamp-random string mapping mechanism; at the same time, the addition of a service node identifier makes it suitable for distributed environments and significantly improves system performance and stability.
[0064] The technical features of the above embodiments can be combined in any way. For the sake of brevity, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this specification.
Claims
1. A method for generating order IDs in a high-concurrency environment, characterized in that, The method includes: Get the current timestamp, the timestamp format is "yyyyMMddHHmmss"; Using the timestamp as the key, search for the corresponding set of random strings in the key-value pair structure; Generate a string of random letters of length 3, wherein the random letters do not include the letters "p", "l" and "x"; Determine whether the random letter string already exists in the random string set corresponding to the current timestamp. If it exists, regenerate it until a unique random string is generated. The generated non-repeating random string is added to the random string set corresponding to the current timestamp, and the key-value pair is written into the memory storage structure; The order ID is obtained by concatenating the current operating environment identifier letter, the last 12 characters of the timestamp, a non-repeating random letter string, and the current service node identifier letter.
2. The method according to claim 1, characterized in that, The timestamp is in seconds and is in the format "yyyyMMddHHmmss", used to identify the time when the order ID was generated.
3. The method according to claim 1, characterized in that, The random letter string is randomly generated from a preset set of allowed letters, which is a set of 26 English letters excluding the letters "p", "l", and "x".
4. The method according to claim 1, characterized in that, The generation of the random string has a maximum number of attempts threshold. If a unique string cannot be generated after exceeding the preset number of attempts, the generation process is terminated.
5. The method according to claim 1, characterized in that, The mapping relationship between the random string and the timestamp is stored in a single-element key-value structure that only records the current timestamp and its corresponding value.
6. The method according to claim 1, characterized in that, The method is executed under a synchronization lock mechanism to ensure the uniqueness of order IDs in a concurrent environment; The synchronization lock mechanism is based on a memory-level lock object to achieve synchronous control over the order ID generation process.
7. The method according to claim 1, characterized in that, The service node identifier is a single character, which can be any digit from 0 to 9, used to distinguish different service nodes in the distributed environment.
8. The method according to claim 1, characterized in that, The order ID is concatenated in the following order: runtime environment identifier letters + the last 12 characters of the timestamp + the random string + the service node identifier letters.
9. The method according to claim 1, characterized in that, The runtime environment identifier letters are used to distinguish the runtime environment in which the order ID is generated, including the development environment, testing environment, and production environment.
10. The method according to claim 1, characterized in that, The order ID generation method is encapsulated in a callable interface method, which receives the runtime environment configuration and service node identifier as input parameters and outputs the order ID string.