A multi-instance concurrent sequence segmentation competition method, system and computer readable storage medium based on a TDSQL database

By using a multi-instance concurrent sequence segmentation competition method in TDSQL database, and leveraging CAS optimistic locking and thread-safe mechanisms to generate incremental sequence numbers, the problem of sequence number duplication and performance bottlenecks under high concurrency is solved. This achieves globally unique and high-performance sequence number generation, making it suitable for cloud-native environments.

CN122152826APending Publication Date: 2026-06-05中国建设银行股份有限公司苏州分行

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
中国建设银行股份有限公司苏州分行
Filing Date
2025-12-18
Publication Date
2026-06-05

Smart Images

  • Figure CN122152826A_ABST
    Figure CN122152826A_ABST
Patent Text Reader

Abstract

The present application relates to the technical field of database concurrency control, and particularly relates to a multi-instance concurrent sequence segment competition method, system and computer readable storage medium based on a TDSQL database. The method first acquires sequence segment information corresponding to a current instance from a sequence list; then calculates a target sequence segment range based on the sequence segment information, and occupies the target sequence segment in the TDSQL database through an update operation with an original value condition, the update operation including a conditional judgment of whether the starting value of the current sequence segment is consistent with a pre-stored value; finally, it is judged whether the update operation successfully occupies the target sequence segment, the successfully occupied sequence segment is cached to the local instance, and an incremental sequence number is generated one by one through a thread safety mechanism. The present application realizes the generation of a globally unique and strictly incremental sequence number in a multi-instance concurrent environment under the constraint of only using a TDSQL database.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of database concurrency control technology, and in particular to a method, system, and computer-readable storage medium for multi-instance concurrent sequence segmentation competition based on TDSQL database. Background Technology

[0002] Serial number generation, as a core capability of distributed systems, is widely used in fields such as financial transactions and cloud-native services. With the development of cloud-native architecture, the number of system service instances needs to automatically scale according to the load. Multiple instances need to generate unique and incremental business serial numbers simultaneously under high concurrency. If there is no effective concurrency control when incrementing serial numbers, issues such as duplicate serial numbers, incorrect number order, preemption deadlocks, and a sharp decline in performance may occur.

[0003] In existing technologies, methods such as Redis INCR, Zookeeper distributed locks, or database pessimistic locks SELECT FOR UPDATE are commonly used to generate sequence numbers. However, Redis INCR requires a Redis cluster, resulting in high maintenance costs when adding new components; Zookeeper distributed locks have low performance and are not suitable for high-concurrency scenarios; and database pessimistic locks SELECT FOR UPDATE suffer from severe lock contention and poor performance. Summary of the Invention

[0004] The present invention aims to at least partially solve one of the technical problems in the related art.

[0005] Therefore, the first objective of this invention is to propose a multi-instance concurrent sequence segmentation competition method based on TDSQL database, comprising: S1, retrieve the sequence segment information corresponding to the current instance from the sequence list; S2, calculate the target sequence segment range based on the sequence segment information, and preempt the target sequence segment in the TDSQL database through an update operation with original value conditions. The update operation includes a condition judgment to verify whether the current sequence segment start value is consistent with the pre-stored value. S3 determines whether the update operation successfully preempted the target sequence segment, caches the successfully preempted sequence segment locally in the instance, and generates an incrementing sequence number one by one through a thread-safe mechanism.

[0006] In one embodiment of the present invention, the sequence segment information includes the current sequence segment start value and a preset sequence segment length.

[0007] In one embodiment of the present invention, S2 further includes: S21, Calculate the sequence segment range of this application, and determine the maximum value based on the starting value of the current sequence segment and the preset sequence segment length; S22, execute CAS optimistic lock contention for the target sequence segment with old value conditions, and return the sequence list segment after successfully updating the sequence list.

[0008] In one embodiment of the present invention, S3 further includes: S31, cache the successfully preempted sequence segment in the new local range of the instance, and return the next sequence number through SeqRange.getAndIncrement(); S32, if the current sequence segment is exhausted, trigger the acquisition of a new sequence segment and generate a sequence number from the new sequence segment.

[0009] In one embodiment of the present invention, S3 further includes: When preemption of the target sequence segment fails, a delayed retry is performed according to the preset retry strategy until the maximum number of retries is reached or the sequence segment is successfully preempted.

[0010] In one embodiment of the present invention, the delay retry strategy adopts an exponential backoff algorithm with an initial delay of 500ms. The delay multiple for each retry is controlled by configuration parameters. When the maximum number of retries is reached and the attempt still fails, an exception is triggered and a failure log is recorded.

[0011] To achieve the above objectives, a second aspect of the present invention proposes a multi-instance concurrent sequence segmentation competition system based on a TDSQL database, comprising: The sequence segment information acquisition module is used to acquire the sequence segment information corresponding to the current instance. The sequence segment information includes the current sequence segment start value and the preset sequence segment length. The target sequence segment preemption module is used to calculate the range of the target sequence segment based on the sequence segment information, and preempt the target sequence segment in the TDSQL database through an update operation with original value conditions. The update operation includes a condition judgment to verify whether the starting value of the current sequence segment is consistent with the pre-stored value. The sequence segment caching and generation module is used to determine whether the update operation has successfully preempted the target sequence segment, cache the successfully preempted sequence segments locally on the instance, and generate incremental sequence numbers one by one through a thread-safe mechanism.

[0012] In one embodiment of the present invention, the target sequence segment preemption module is further configured to: The range of the sequence segment in this application is calculated by determining the maximum value based on the starting value of the current sequence segment and the preset sequence segment length. Perform a CAS optimistic lock contention for the target sequence segment with old value conditions, and return the sequence list segment after successfully updating the sequence list.

[0013] In one embodiment of the present invention, it further includes: The sequence segment retry control module is used to perform delayed retries according to a preset retry strategy when sequence segment generation fails, until the maximum number of retries is reached or the sequence segment is successfully preempted. The delayed retry strategy adopts an exponential backoff algorithm with an initial delay of 500ms. The delay multiple for each retry is controlled by configuration parameters, and the maximum number of retries is dynamically set through configuration items. When the maximum number of retries is reached and the attempt still fails, an exception is triggered and a failure log is recorded.

[0014] To achieve the above objectives, a third aspect of the present invention provides a computer-readable storage medium storing a computer program that, when executed by a processor, implements the method described in the first aspect.

[0015] The method, system, and storage medium of this invention, under the constraint of using only a TDSQL database, achieve globally unique and strictly incremental sequence number generation in a multi-instance concurrent environment, effectively reducing database access frequency and lock contention, improving system throughput performance, and being compatible with cloud-native auto-scaling features.

[0016] Additional aspects and advantages of the invention will be set forth in part in the description which follows, and in part will be obvious from the description, or may be learned by practice of the invention. Attached Figure Description

[0017] The above and / or additional aspects and advantages of the present invention will become apparent and readily understood from the following description of the embodiments taken in conjunction with the accompanying drawings, wherein: Figure 1 This is a flowchart of a multi-instance concurrent sequence segmentation competition method based on a TDSQL database according to an embodiment of the present invention; Figure 2 This is a structural diagram of a multi-instance concurrent sequence segmentation competition system based on a TDSQL database according to an embodiment of the present invention. Detailed Implementation

[0018] It should be noted that, unless otherwise specified, the embodiments and features described in the present invention can be combined with each other. The present invention will now be described in detail with reference to the accompanying drawings and embodiments.

[0019] To enable those skilled in the art to better understand the present invention, the technical solutions of the present invention will be clearly and completely described below with reference to the accompanying drawings of the embodiments of the present invention. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort should fall within the scope of protection of the present invention.

[0020] The following describes, with reference to the accompanying drawings, a method and system for multi-instance concurrent sequence segmentation competition based on a TDSQL database, according to an embodiment of the present invention.

[0021] Example 1 Figure 1 This is a flowchart of a multi-instance concurrent sequence segmentation competition method based on a TDSQL database according to an embodiment of the present invention.

[0022] like Figure 1 As shown, the multi-instance concurrent sequence segmentation competition method based on TDSQL database includes the following steps: S1, obtain the sequence segment information corresponding to the current instance, the sequence segment information includes the current sequence segment start value and the preset sequence segment length.

[0023] Specifically, in some implementations, obtaining the sequence segment information corresponding to the current instance is a key step in achieving high-concurrency, lock-free sequence generation in this invention. The core of this step lies in reading the starting value `seq_no_cur_val` and the preset sequence segment length `rvl_len` of the current sequence segment from the sequence table `plt_sequence` in the TDSQL database, thereby providing a safe and usable sequence range for the instance's local sequence generator.

[0024] From a technical implementation perspective, this step is typically completed by the sequence request service `SequenceServiceImpl` calling the database interface. The instance first retrieves the current record via an SQL query, for example, executing `SELECT seq_no_cur_val, rvl_len FROM plt_sequence WHERE seq_nm = 'xxx' FOR UPDATE`, to ensure consistency in subsequent CAS update operations. After obtaining `seq_no_cur_val` and `rvl_len`, the instance's local `DefaultSequence` component constructs a sequence range based on these two parameters, i.e., `[seq_no_cur_val, seq_no_cur_val + rvl_len - 1]`, and caches it in memory for subsequent sequence number generation.

[0025] In cloud-native deployment environments, this step supports automatic scaling of instances. When a new instance joins, it immediately participates in the CAS competition for sequence segments without waiting for a centralized coordinating node. This mechanism aligns with the stateless instance characteristics of platforms like TKE or Kaiyang Cloud, ensuring that the system maintains the global uniqueness and increment of sequence numbers even during dynamic changes.

[0026] The technical advantage of this step lies in significantly reducing frequent database accesses by caching sequence segment information locally on the instance, thereby alleviating hotspot pressure on the database. Simultaneously, combined with the subsequent CAS update mechanism, it ensures the atomic allocation of sequence segments, avoiding the performance bottlenecks and deadlock risks associated with traditional pessimistic locking. Therefore, this step plays a crucial role in this invention, serving as a fundamental step in achieving high-performance, strongly consistent sequence generation.

[0027] S2, calculate the target sequence segment range based on the sequence segment information, and preempt the target sequence segment in the TDSQL database through an update operation with original value conditions. The update operation includes a condition judgment to verify whether the current sequence segment start value is consistent with the pre-stored value.

[0028] Further, step S2 includes: S21, Calculate the sequence segment range of this application, and determine the maximum value based on the starting value of the current sequence segment and the preset sequence segment length; S22, execute CAS optimistic lock contention for the target sequence segment with old value conditions, and return the sequence list segment after successfully updating the sequence list.

[0029] Specifically, in some implementations, this step preempts the target sequence segment in the TDSQL database through an update operation with original value conditions. Its core lies in employing a CAS (Compare-And-Set) optimistic locking mechanism to avoid the high-concurrency performance bottleneck caused by traditional pessimistic locking. Specifically, this step first calculates the range of the target sequence segment to be requested, i.e., `[seq_no_cur_val, seq_no_cur_val + rvl_len - 1]`, based on the sequence segment information read by the current instance (including the current sequence segment's starting value `seq_no_cur_val` and segment length `rvl_len`). This range is used for subsequent sequence number generation caching.

[0030] In practical applications, this step is typically deployed in stateless service instances within cloud-native environments (such as Kaiyang Cloud and TKE). Each instance automatically participates in the competition for sequence segments after startup. Through a local caching mechanism, after successfully preempting a sequence segment, the instance can use the `getAndIncrement()` method in the thread-safe `DefaultSequence` class to generate sequence numbers one by one, thereby significantly reducing the frequency of database access and improving system throughput.

[0031] In terms of technical effectiveness, this step effectively avoids database row lock contention and reduces the probability of transaction conflicts through the CAS optimistic locking mechanism, while ensuring the global uniqueness and monotony of the sequence number. In cloud environment scenarios with automatic instance scaling, this mechanism can achieve a balance between high consistency and high performance without relying on external components such as Redis or Zookeeper, demonstrating good engineering practicality and deployment compatibility.

[0032] S3 determines whether the update operation successfully preempts the target sequence segment, caches the successfully preempted sequence segments locally on the instance, and generates an incrementing sequence number one by one through a thread-safe mechanism; when the preemption of the target sequence segment fails, it performs a delayed retry according to the preset retry strategy until the maximum number of retries is reached or the sequence segment is successfully preempted.

[0033] Furthermore, step S3 also includes: S31, cache the successfully preempted sequence segment in the new local range of the instance, and return the next sequence number through SeqRange.getAndIncrement(); S32, if the current sequence segment is exhausted, trigger the acquisition of a new sequence segment and generate a sequence number from the new sequence segment.

[0034] Specifically, in some implementations, after a service instance successfully preempts a sequence segment using the CAS optimistic locking mechanism, the instance caches the acquired sequence segment in local memory and generates an incrementing sequence number one by one using a thread-safe mechanism. This step is the key to achieving high performance, low database pressure, and strictly incrementing sequence number generation in this invention.

[0035] At the application level, this step is widely used in scenarios such as core banking systems, task ID generation in cloud-native microservice architectures, and global transaction serial number allocation. In environments where stateless service instances frequently scale, the local caching mechanism effectively reduces frequent access to the database, thereby improving the overall throughput of the system.

[0036] From a technical perspective, this step achieves efficient generation of sequence numbers through local caching and thread-safe mechanisms, avoiding performance bottlenecks caused by database lock contention. Simultaneously, since database updates are triggered only when a sequence segment is exhausted, it significantly reduces hotspot pressure on the database, improving system scalability and stability.

[0037] When sequence segment generation fails, the system will retry after a delay according to a preset retry strategy until the maximum number of retries is reached or the sequence segment is successfully preempted. This step is the core of the conflict retry mechanism in this invention. Its technical implementation is based on optimistic locking (CAS) update failure handling logic, aiming to improve the success rate of sequence number generation and system stability in a multi-instance concurrent environment.

[0038] At the parameter level, the maximum number of retries is controlled by system configuration options, typically set to 3 to 5 times to balance retry success rate and resource consumption. If the sequence segment is not successfully preempted within the maximum number of retries, the system will throw an exception or log a failure to prevent infinite looping of thread resources. Furthermore, parameters such as the retry interval, the maximum number of retries, and the sequence segment length (`rvl_len`) can be flexibly adjusted through configuration files or environment variables to adapt to the performance requirements of different business scenarios.

[0039] At the application level, this mechanism is widely applicable to stateless service instances in cloud-native environments, such as Kaiyang Cloud or the TKE platform. In scenarios involving automatic instance scaling and frequent online / offline cycles, multiple instances may simultaneously attempt to request sequence segments, leading to CAS update failures. Through the delayed retry mechanism, the system ensures that each instance obtains a sequence segment within a reasonable time, thereby maintaining the global uniqueness and incrementality of the sequence number.

[0040] From a technical perspective, this step effectively alleviates lock contention issues in the database under high concurrency, improving the system's fault tolerance and throughput performance. By controlling the number of retries and the delay interval, the system can achieve strong consistency sequence number generation based on TDSQL without relying on external components such as Redis or Zookeeper, meeting the high requirements of the banking system for data consistency and stability.

[0041] The multi-instance concurrent sequence segmentation competition method based on TDSQL database in this invention realizes globally unique and strictly incremental sequence number generation in a multi-instance concurrent environment under the condition of using only TDSQL database. This effectively reduces database lock contention and access pressure, and improves the performance and consistency of the system in high concurrency and cloud-native elastic scaling scenarios.

[0042] Example 2 Figure 2 This is a structural diagram of a multi-instance concurrent sequence segmentation competition system based on a TDSQL database according to an embodiment of the present invention.

[0043] like Figure 2 As shown, a multi-instance concurrent sequence segmentation competition system based on the TDSQL database includes: The sequence segment information acquisition module is used to acquire the sequence segment information corresponding to the current instance. The sequence segment information includes the current sequence segment start value and the preset sequence segment length. The target sequence segment preemption module is used to calculate the range of the target sequence segment based on the sequence segment information, and preempt the target sequence segment in the TDSQL database through an update operation with original value conditions. The update operation includes a condition judgment to verify whether the starting value of the current sequence segment is consistent with the pre-stored value. The sequence segment caching and generation module is used to determine whether the update operation has successfully preempted the target sequence segment, cache the successfully preempted sequence segments locally on the instance, and generate incremental sequence numbers one by one through a thread-safe mechanism.

[0044] Furthermore, the target sequence segment preemption module is also used for: The range of the sequence segment in this application is calculated by determining the maximum value based on the starting value of the current sequence segment and the preset sequence segment length. Perform a CAS optimistic lock contention for the target sequence segment with old value conditions, and return the sequence list segment after successfully updating the sequence list.

[0045] Furthermore, the system also includes: The sequence segment retry control module is used to perform delayed retries according to a preset retry strategy when sequence segment generation fails, until the maximum number of retries is reached or the sequence segment is successfully preempted. The delayed retry strategy adopts an exponential backoff algorithm with an initial delay of 500ms. The delay multiple for each retry is controlled by configuration parameters, and the maximum number of retries is dynamically set through configuration items. When the maximum number of retries is reached and the attempt still fails, an exception is triggered and a failure log is recorded.

[0046] The present invention also provides a computer-readable storage medium storing a computer program, which, when executed by a processor, implements the above-described method for multi-instance concurrent sequence segmentation competition based on a TDSQL database.

[0047] In the description of this specification, the references to terms such as "one embodiment," "some embodiments," "example," "specific example," or "some examples," etc., refer to specific features, structures, materials, or characteristics described in connection with that embodiment or example, which are included in at least one embodiment or example of the present invention. In this specification, the illustrative expressions of the above terms do not necessarily refer to the same embodiment or example. Furthermore, the specific features, structures, materials, or characteristics described may be combined in any suitable manner in one or more embodiments or examples. Moreover, without contradiction, those skilled in the art can combine and integrate the different embodiments or examples described in this specification, as well as the features of different embodiments or examples.

[0048] Furthermore, the terms "first" and "second" are used for descriptive purposes only and should not be construed as indicating or implying relative importance or implicitly specifying the number of technical features indicated. Thus, a feature defined as "first" or "second" may explicitly or implicitly include at least one of that feature. In the description of this invention, "a plurality of" means at least two, such as two, three, etc., unless otherwise explicitly specified.

Claims

1. A multi-instance concurrent sequence segmentation competition method based on TDSQL database, characterized in that, include: S1, retrieve the sequence segment information corresponding to the current instance from the sequence list; S2, calculate the target sequence segment range based on the sequence segment information, and preempt the target sequence segment in the TDSQL database through an update operation with original value conditions. The update operation includes a condition judgment to verify whether the current sequence segment start value is consistent with the pre-stored value. S3 determines whether the update operation successfully preempted the target sequence segment, caches the successfully preempted sequence segment locally in the instance, and generates an incrementing sequence number one by one through a thread-safe mechanism.

2. The method as described in claim 1, characterized in that, The sequence segment information includes the current sequence segment start value and the preset sequence segment length.

3. The method as described in claim 2, characterized in that, S2 further includes: S21, Calculate the sequence segment range of this application, and determine the maximum value based on the current sequence segment start value and the preset sequence segment length; S22, execute CAS optimistic locking competition for the target sequence segment with old value conditions, and return the sequence list segment after successfully updating the sequence list.

4. The method as described in claim 1, characterized in that, S3 further includes: S31, cache the successfully preempted sequence segment in the new local range of the instance, and return the next sequence number through SeqRange.getAndIncrement(); S32, if the current sequence segment is exhausted, trigger the acquisition of a new sequence segment and generate a sequence number from the new sequence segment.

5. The method as described in claim 1, characterized in that, S3 further includes: When preemption of the target sequence segment fails, a delayed retry is performed according to the preset retry strategy until the maximum number of retries is reached or the sequence segment is successfully preempted.

6. The method as described in claim 5, characterized in that, The delay retry strategy adopts an exponential backoff algorithm with an initial delay of 500ms. The delay multiplier for each retry is controlled by configuration parameters. When the maximum number of retries is reached and the attempt still fails, an exception is triggered and a failure log is recorded.

7. A multi-instance concurrent sequence segmentation competition system based on TDSQL database, characterized in that, include: The sequence segment information acquisition module is used to acquire the sequence segment information corresponding to the current instance. The sequence segment information includes the current sequence segment start value and the preset sequence segment length. The target sequence segment preemption module is used to calculate the range of the target sequence segment based on the sequence segment information, and preempt the target sequence segment in the TDSQL database through an update operation with original value conditions. The update operation includes a condition judgment to verify whether the starting value of the current sequence segment is consistent with the pre-stored value. The sequence segment caching and generation module is used to determine whether the update operation has successfully preempted the target sequence segment, cache the successfully preempted sequence segments locally on the instance, and generate incremental sequence numbers one by one through a thread-safe mechanism.

8. The system as described in claim 7, characterized in that, The target sequence segment preemption module is also used for: The range of the sequence segment in this application is calculated by determining the maximum value based on the starting value of the current sequence segment and the preset sequence segment length. Perform a CAS optimistic lock contention for the target sequence segment with old value conditions, and return the sequence list segment after successfully updating the sequence list.

9. The system as described in claim 7, characterized in that, Also includes: The sequence segment retry control module is used to perform delayed retries according to a preset retry strategy when sequence segment generation fails, until the maximum number of retries is reached or the sequence segment is successfully preempted. The delayed retry strategy adopts an exponential backoff algorithm with an initial delay of 500ms. The delay multiple for each retry is controlled by configuration parameters, and the maximum number of retries is dynamically set through configuration items. When the maximum number of retries is reached and the attempt still fails, an exception is triggered and a failure log is recorded.

10. A computer-readable storage medium storing a computer program that, when executed by a processor, implements the method as claimed in any one of claims 1-6.