Data processing method for financial transaction scene, medium and electronic equipment

By using ring buffers and ThreadLocal in financial trading systems, the performance bottleneck problem caused by thread competition in traditional technology is solved, and high throughput, low latency and thread-safe data processing are achieved, which is suitable for high concurrency scenarios.

CN118819899APending Publication Date: 2024-10-22BEIYIN FINANCIAL TECH CO LTD
View PDF 0 Cites 2 Cited by

Patent Information

Application Number
CN202410915504.X
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-07-09
Publication Date
2024-10-22

AI Technical Summary

Technical Problem

In high-concurrency data processing scenarios, traditional blocking queue and lock mechanisms are difficult to meet high-performance requirements and can easily lead to thread competition and performance bottlenecks.

Method used

Using ring buffer (RingBuffer) combined with ThreadLocal, through lock-free design and pre-allocated memory, it achieves high throughput and low-latency data processing, supports multi-producer and multi-consumer modes, and uses the Compare-And-Swap mechanism and ThreadLocal. Thread local storage and event handling.

Benefits of technology

Significantly improves the processing performance of the financial transaction system, meets the requirements of high concurrency, high throughput, and low latency, provides a thread-safe and efficient data processing method, and is suitable for financial transactions and other high-performance data processing fields.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118819899A_ABST
    Figure CN118819899A_ABST
Patent Text Reader

Abstract

The invention discloses a data processing method for a financial transaction scene, a medium and electronic equipment. The method comprises the following steps: initializing an annular buffer area; thread Local is used for carrying out thread local storage; the method comprises the following steps: in a producer thread, obtaining a thread local event object through Thread Local; acquiring a next available serial number of the annular buffer area; writing data in a local event object of the thread into an event object in the annular buffer area; issuing an event through the annular buffer area, and notifying a consumer that a new event can be processed; the consumer thread obtains the event from the annular buffer area; processing the transaction data obtained from the annular buffer area by using a local processing state object of the thread; and after processing the transaction data, updating a local processing state object of the thread. The financial transaction system data processing method is efficient, low in delay and safe in thread, and the strict requirements of high concurrency, high throughput and low delay of a financial system are met.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of financial transactions, and in particular to a data processing method, a medium and an electronic device for use in financial transaction scenarios. Background Art

[0002] In high-concurrency data processing scenarios, such as online trading systems and real-time data analysis, the system needs to process a large number of concurrent requests quickly and efficiently. Currently, in high-frequency data processing scenarios, the common implementation scheme is to use traditional blocking queues (such as ArrayBlockingQueue or LinkedBlockingQueue) combined with thread pools to produce and consume data.

[0003] Traditional queue and lock mechanisms often fail to meet high performance requirements and easily lead to thread contention and performance bottlenecks. RingBuffer, as an efficient buffer structure, can provide high throughput and low latency, while ThreadLocal can provide independent variable storage for each thread to avoid sharing and contention between threads. Summary of the invention

[0004] In view of the above problems, the present invention is proposed to provide a data processing method, medium and electronic device for financial transaction scenarios that overcome the above problems or at least partially solve the above problems.

[0005] To achieve the above-mentioned purpose, a first aspect of the present application provides a data processing method for a financial transaction scenario, the method comprising:

[0006] Initialize the ring buffer;

[0007] Use ThreadLocal for thread local storage;

[0008] In the producer thread, obtain the thread local event object through ThreadLocal and write the transaction data into the thread local event object;

[0009] Get the next available sequence number in the ring buffer, and use the obtained sequence number to get the corresponding event object from the ring buffer;

[0010] Write the data in the thread local event object to the event object in the ring buffer;

[0011] Publish events through the ring buffer to notify consumers that new events are available for processing;

[0012] The consumer thread obtains events from the ring buffer, uses the serial number of the consumer thread to identify the events to be processed, and obtains the local processing status object of the consumer thread through ThreadLocal;

[0013] Use the thread-local processing state object to process the transaction data from the ring buffer;

[0014] After processing the transaction data, update the thread-local processing status object.

[0015] Optionally, the initializing the ring buffer includes:

[0016] Select the buffer based on the expected concurrency and data processing requirements of the trading system;

[0017] Choose a wait strategy to balance system latency and CPU usage;

[0018] Create an event factory, which is used to create event objects in the ring buffer;

[0019] Initialize the Disruptor framework, create and manage the ring buffer through the Disruptor framework;

[0020] Configure the event handler, which is used to process events read from the ring buffer.

[0021] Optionally, the use of ThreadLocal for thread local storage includes:

[0022] Determine the objects that need ThreadLocal storage;

[0023] Create ThreadLocal variables for each object that requires thread local storage;

[0024] Initialize ThreadLocal variables. During system initialization, initialize ThreadLocal variables for each thread.

[0025] Get the ThreadLocal object in the thread. When processing transaction data, get the object stored locally in the thread through the ThreadLocal variable.

[0026] In the producer thread, write the transaction data into the transaction event object stored in ThreadLocal;

[0027] In the consumer thread, the processing status object is obtained through the ThreadLocal variable.

[0028] Optionally, the stored objects include transaction event objects and processing status objects, the transaction event objects include transaction ID and transaction price, and the processing status objects include intermediate states of transaction processing and results of transaction processing.

[0029] Optionally, after the transaction data is processed, logging operations may be performed or other systems may be notified based on the processing results.

[0030] Optionally, the ring buffer implements lock-free sequence number acquisition and event publishing through a Compare-And-Swap mechanism.

[0031] Optionally, the ring buffer supports multi-producer and multi-consumer mode. Through the synchronization mechanism, multiple producers can publish events at the same time, and multiple consumers can process events at the same time.

[0032] Optionally, through lock-free design and pre-allocated memory, event objects are pre-allocated in a ring buffer to ensure data consistency and processing accuracy in a high-concurrency environment.

[0033] In a second aspect of the present application, a computer-readable storage medium is provided, in which a computer program is stored. When the computer program is loaded and executed by a processor, the method described in any one of the first aspects is adopted.

[0034] In a third aspect of the present application, an electronic device is provided, comprising a memory, a processor, and a computer program stored in the memory and capable of running on the processor, wherein when the processor loads and executes the computer program, the method described in any one of the first aspects is adopted.

[0035] In summary, due to the adoption of the above technical solution, the beneficial effects of the present invention are:

[0036] The present invention combines the use of a ring buffer and ThreadLocal to provide an efficient, low-latency, thread-safe financial transaction system data processing method, which significantly improves the processing performance, meets the strict requirements of high concurrency, high throughput, and low latency of the financial system, and has broad application prospects and market value. BRIEF DESCRIPTION OF THE DRAWINGS

[0037] Figure 1 It is a flow chart of a data processing method for a financial transaction scenario provided in an embodiment of the present application. DETAILED DESCRIPTION

[0038] In order to make the purpose, technical solutions and advantages of the embodiments of the present invention clearer, the technical solutions in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, rather than all the embodiments. Based on the embodiments in the present invention, all other embodiments obtained by ordinary technicians in this field without making creative work are within the scope of protection of the present invention.

[0039] See also Figure 1 This embodiment provides a data processing method for a financial transaction scenario, including:

[0040] S1. Initialize the ring buffer.

[0041] In financial transaction scenarios, initializing the ring buffer (hereinafter referred to as RingBuffer) is one of the key steps to ensure that the system can efficiently handle high-concurrency transaction requests. The detailed steps are as follows:

[0042] S101. Determine the buffer size.

[0043] The buffer size is the core parameter of RingBuffer, which directly affects the throughput and performance of the system. Choose an appropriate buffer size based on the expected concurrency and data processing requirements of the trading system. For example, set the buffer size to 1024 or 2048 to ensure that there is enough buffer space in a high-concurrency environment.

[0044] S102: Select a waiting strategy.

[0045] The waiting strategy determines the behavior of the consumer when waiting for the producer to publish new data. Common waiting strategies include:

[0046] BlockingWaitStrategy: Uses locks and condition variables, suitable for scenarios where latency requirements are not high.

[0047] YieldingWaitStrategy: The consumer thread will continue to give up the CPU, which is suitable for low latency and high throughput scenarios.

[0048] BusySpinWaitStrategy: The consumer thread is busy waiting, which is suitable for extremely low latency scenarios, but consumes more CPU resources.

[0049] Choose an appropriate wait strategy to balance system latency and CPU usage.

[0050] S103. Create an event factory.

[0051] The event factory is used to create event objects in RingBuffer. Event objects in financial transaction scenarios usually contain information such as transaction ID and price.

[0052] S104. Initialize the Disruptor framework.

[0053] The Disruptor framework is the core tool for implementing RingBuffer. Through the Disruptor framework, you can easily create and manage RingBuffer, as well as producer and consumer threads.

[0054] S105. Configure event handler.

[0055] The event processor is used to process events read from the RingBuffer. Depending on the needs of the trading system, a single or multiple event processors can be configured to process trading data.

[0056] S2. Use ThreadLocal for thread local storage.

[0057] In financial trading scenarios, in order to avoid data sharing and competition between threads, we use ThreadLocal to provide independent variable storage for each thread. The detailed steps are as follows:

[0058] S201. Determine the objects that need to be stored in ThreadLocal.

[0059] In financial trading systems, determine which data or objects need to be stored locally in the thread to avoid data sharing and contention between threads. Typically, these objects include:

[0060] Transaction event object (TradeEvent): includes transaction ID, transaction price and other information.

[0061] Processing State object: includes the intermediate state of transaction processing and the result of transaction processing.

[0062] S202. Create a ThreadLocal variable.

[0063] Create a ThreadLocal variable for each object that requires thread local storage. These variables exist independently in each thread, avoiding data competition between threads.

[0064] S203. Initialize ThreadLocal variables.

[0065] During system initialization, initialize the ThreadLocal variable for each thread. Use the withInitial method of ThreadLocal to allocate a separate object instance for each thread.

[0066] S204. Get the ThreadLocal object in the thread.

[0067] When processing transaction data, objects stored locally in the thread are obtained through ThreadLocal variables. These objects are only used within the thread, avoiding data sharing between threads.

[0068] S205. Write the data into the ThreadLocal object.

[0069] In the producer thread, the transaction data is written to the transaction event object stored in ThreadLocal. In this way, each producer thread can process its own data independently without conflicting with other threads.

[0070] S206. Process data and update ThreadLocal status.

[0071] In the consumer thread, the processing status object is obtained through the ThreadLocal variable. The object is used to process the transaction data obtained from the RingBuffer and the processing status is updated after the processing is completed.

[0072] S207. Release the ThreadLocal object.

[0073] In some cases, after the thread has finished executing, you need to manually clean up the ThreadLocal object to prevent memory leaks. This can be done by calling the ThreadLocal.remove() method.

[0074] S3, Event Production

[0075] S301. Obtain a thread-local event object.

[0076] In the producer thread, the thread-local event object is obtained through ThreadLocal. This ensures that each thread has an independent event object and avoids data sharing and competition between threads.

[0077] S302: Write transaction data.

[0078] Write the transaction data (such as transaction ID and price) to the thread-local event object. This operation is thread-local and does not involve synchronization issues between threads.

[0079] S303. Get the next sequence number of RingBuffer.

[0080] Use the `next()` method of RingBuffer to get the next available sequence number. This is a lock-free operation, which is thread-safe through the CAS (Compare-And-Swap) mechanism.

[0081] S304: Get the event object in RingBuffer.

[0082] Get the corresponding event object from the RingBuffer through the obtained sequence number. RingBuffer pre-allocates memory to ensure that the obtained event object is allocated without additional memory allocation operations.

[0083] S305: Write the data into the event object in RingBuffer.

[0084] Write the data in the thread local event object to the event object in the RingBuffer. This step is to copy the data from the ThreadLocal event object to the RingBuffer.

[0085] S306: Publish an event.

[0086] Use the publish(sequence) method of RingBuffer to publish events and notify consumers that there are new events to process. This is also a lock-free operation, and thread safety is guaranteed by the CAS mechanism.

[0087] S4. Event processing.

[0088] S401. Get events from RingBuffer.

[0089] The consumer thread gets events from the RingBuffer and uses the sequence number of the consumer thread to identify the events to be processed. RingBuffer ensures that events are read by consumers in the order in which they are published by the producer.

[0090] S402: Obtain thread local processing status.

[0091] Get the local processing state object of the consumer thread through ThreadLocal (such as

[0092] `ProcessingState`). Each consumer thread has an independent processing state object, avoiding data sharing and competition between threads.

[0093] S403: Process transaction data.

[0094] Use the thread-local processing state object to process the transaction data obtained from the RingBuffer. The processing logic can be customized according to specific needs, such as calculation, storage, filtering and other operations.

[0095] S404: Update processing status.

[0096] After processing the transaction data, update the thread-local processing state object. This ensures the independence and thread safety of the processing state.

[0097] S405: Execute subsequent operations.

[0098] After processing the transaction data, some subsequent operations will be performed based on the processing results, such as logging, notifying other systems, etc. These operations are also completed locally to ensure that they do not affect the execution of other threads.

[0099] This embodiment uses the CAS mechanism in multi-thread synchronization to ensure thread safety. RingBuffer uses the CAS (Compare-And-Swap) mechanism to achieve lock-free sequence number acquisition and event publishing, ensuring thread safety between producers and consumers. This mechanism reduces lock contention and improves system performance.

[0100] The design of RingBuffer ensures the orderly processing of events. Consumers process events in the order published by producers, ensuring data consistency and processing accuracy.

[0101] RingBuffer supports multi-producer and multi-consumer modes. Through an efficient synchronization mechanism, multiple producers can publish events at the same time, and multiple consumers can process events at the same time, adapting to the needs of high-concurrency environments. Through lock-free design and pre-allocated memory, RingBuffer avoids data consistency issues. Event objects are pre-allocated in RingBuffer to ensure data consistency and processing accuracy in a high-concurrency environment. By reducing lock contention and context switching, using CAS mechanism and ThreadLocal storage, the present invention significantly optimizes system performance and improves the throughput and response speed of financial transaction systems.

[0102] In the financial transaction scenario, this embodiment combines RingBuffer and ThreadLocal to improve the single-machine processing performance, which has the following significant advantages:

[0103] High throughput: Lock-free design: RingBuffer adopts lock-free design, through CAS

[0104] The (Compare-And-Swap) mechanism realizes data production and consumption, reduces lock contention and context switching, and improves system throughput. Pre-allocated memory: Event objects are pre-allocated in the RingBuffer, avoiding frequent memory allocation and garbage collection at runtime, and improving the overall performance of the system.

[0105] Low latency: Thread local storage: Use ThreadLocal to provide independent storage space for each thread, avoiding data sharing and competition between threads, significantly reducing the latency of data processing. Efficient waiting strategy: By selecting an appropriate waiting strategy (such as BusySpinWaitStrategy or YieldingWaitStrategy), the latency of event processing is further reduced, meeting the strict requirements of financial trading systems for low latency.

[0106] Thread safety: Independent thread local variables: ThreadLocal variables ensure that each thread has an independent event object and processing status, avoiding data competition between threads and improving the thread safety of the system. Ordered event processing: RingBuffer ensures that events are read and processed by consumers in the order published by producers, ensuring data consistency and processing accuracy.

[0107] Easy scalability: Support multiple producers and multiple consumers: RingBuffer supports multiple producers and multiple consumers. Through an efficient synchronization mechanism, multiple producers can publish events at the same time, and multiple consumers can process events at the same time, adapting to the needs of transaction systems of different scales.

[0108] Flexible configuration: According to the specific needs of the system, the size and waiting strategy of RingBuffer can be flexibly configured to meet different load and performance requirements.

[0109] Efficient resource utilization: Reduce context switching: Lock-free design and efficient waiting strategy reduce context switching between threads, reduce system overhead, and improve CPU resource utilization.

[0110] Optimize memory usage: ThreadLocal and pre-allocated memory mechanisms reduce the frequency of garbage collection, optimize memory usage, and improve system stability and performance.

[0111] Strong adaptability: Coping with high-concurrency scenarios: The present invention is particularly suitable for high-concurrency data processing scenarios in financial transaction systems, and can efficiently process a large number of concurrent transaction requests to ensure high availability and reliability of the system.

[0112] Wide application: In addition to financial transaction scenarios, the method and system of the present invention are also applicable to other fields that require high-performance data processing, such as real-time data analysis, online advertising bidding, Internet of Things data processing, etc.

[0113] By combining RingBuffer and ThreadLocal, this embodiment provides an efficient, low-latency, and thread-safe financial transaction system data processing method. It significantly improves the single-machine processing performance, meets the strict requirements of high concurrency, high throughput, and low latency, and has broad application prospects and market value.

[0114] In addition, it should be noted that: an embodiment of the present application also provides a computer-readable storage medium, in which a computer program is stored. When the computer program is loaded and executed by a processor, the method described in the above embodiment is executed.

[0115] In addition, it should be noted that an embodiment of the present application also provides an electronic device, including a memory, a processor, and a computer program stored in the memory and capable of running on the processor. When the processor loads and executes the computer program, the method described in the above embodiment is executed.

[0116] The above contents are further detailed descriptions of the present invention in combination with specific preferred embodiments, and it cannot be determined that the specific implementation of the present invention is limited to these descriptions. For ordinary technicians in the technical field to which the present invention belongs, several simple deductions or substitutions can be made without departing from the concept of the present invention, which should be regarded as falling within the protection scope of the present invention.

Claims

1. A data processing method for financial transaction scenarios, It is characterized in that The method comprises: Initialize the ring buffer; Use ThreadLocal for thread local storage; In the producer thread, obtain the thread local event object through ThreadLocal and write the transaction data into the thread local event object; Get the next available sequence number in the ring buffer, and use the obtained sequence number to get the corresponding event object from the ring buffer; Write the data in the thread local event object to the event object in the ring buffer; Publish events through the ring buffer to notify consumers that new events are available for processing; The consumer thread obtains events from the ring buffer, uses the serial number of the consumer thread to identify the events to be processed, and obtains the local processing status object of the consumer thread through ThreadLocal; Use the thread-local processing state object to process the transaction data from the ring buffer; After processing the transaction data, update the thread-local processing status object.

2. A data processing method for financial transaction scenarios as claimed in claim 1, It is characterized in that The initialization ring buffer comprises: Select the buffer based on the expected concurrency and data processing requirements of the trading system; Choose a wait strategy to balance system latency and CPU usage; Create an event factory, which is used to create event objects in the ring buffer; Initialize the Disruptor framework, create and manage the ring buffer through the Disruptor framework; Configure the event handler, which is used to process events read from the ring buffer.

3. A data processing method for financial transaction scenarios as claimed in claim 1, It is characterized in that The use of ThreadLocal for thread local storage includes: Determine the objects that need ThreadLocal storage; Create ThreadLocal variables for each object that requires thread local storage; Initialize ThreadLocal variables. During system initialization, initialize ThreadLocal variables for each thread. Get the ThreadLocal object in the thread. When processing transaction data, get the object stored locally in the thread through the ThreadLocal variable. In the producer thread, write the transaction data into the transaction event object stored in ThreadLocal; In the consumer thread, the processing status object is obtained through the ThreadLocal variable.

4. A data processing method for financial transaction scenarios as claimed in claim 3, It is characterized in that The stored objects include transaction event objects and processing status objects. The transaction event objects include transaction ID and transaction price. The processing status objects include intermediate status of transaction processing and results of transaction processing.

5. A data processing method for financial transaction scenarios as claimed in claim 1, It is characterized in that After processing the transaction data, logging operations will be performed or other systems will be notified based on the processing results.

6. A data processing method for financial transaction scenarios as claimed in claim 1, It is characterized in that The ring buffer implements lock-free sequence number acquisition and event publishing through a Compare-And-Swap mechanism.

7. A data processing method for financial transaction scenarios as claimed in claim 1, It is characterized in that The ring buffer supports multi-producer and multi-consumer modes. Through the synchronization mechanism, multiple producers can publish events at the same time, and multiple consumers can process events at the same time.

8. A data processing method for financial transaction scenarios as claimed in claim 1, It is characterized in that Through lock-free design and pre-allocated memory, event objects are pre-allocated in a ring buffer to ensure data consistency and processing accuracy in a high-concurrency environment.

9. A computer-readable storage medium having a computer program stored therein. It is characterized in that When the computer program is loaded and executed by a processor, the method according to any one of claims 1 to 8 is adopted.

10. An electronic device comprising a memory, a processor, and a computer program stored in the memory and capable of running on the processor, It is characterized in that When the processor loads and executes the computer program, the method according to any one of claims 1 to 8 is adopted.

Citation Information

Cited By

  • CAN frame data transmission method and device, equipment and storage medium

    CN121056424A

  • Real-time transaction interception method and device based on memory ring technology

    CN122529853A