Optimization method and system for service end log recording under high concurrency
By setting up a local log queue independently in each business thread and using the ThreadLocal feature, the problem of decreased log write throughput in high-concurrency scenarios was solved, achieving efficient log recording and consumption, reducing glitches, and improving business performance.
Patent Information
- Application Number
- CN202211407574.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-11-10
- Publication Date
- 2026-02-10
- Estimated Expiration
- 2042-11-10
AI Technical Summary
In high-concurrency scenarios, existing technologies use global locking on local queues, which reduces log write throughput and impacts business throughput performance.
Each business thread has its own independent local log queue, and messages are written and read using the ThreadLocal function to avoid locking operations. Log consumption is carried out in both active and passive ways.
It improves the efficiency of log writing and reading in high-concurrency scenarios, reduces business glitches, and enhances business throughput.
Smart Images

Figure CN115640152B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of communication technology, and more specifically, to an optimization method and system for server-side log recording under high concurrency. Background Technology
[0002] Many systems today have corresponding server-side components, and each of these systems requires logging functionality, as do various open-source software programs. For unimportant log information, the current approach is for the main thread to print the logs directly. To prevent log printing from affecting the main thread, a better solution is for the main thread to synchronously write to a local queue, while a separate asynchronous thread consumes messages from the local queue and prints the logs. If log messages are prone to loss or inaccuracy, this better solution can be used. However, in databases or message queues, multi-threaded business processing and logging are typically used, requiring precise logging methods. The current better solution necessitates global locking of the local queue; that is, a global lock is required when writing and consuming messages. In low-concurrency scenarios, this method ensures message security and accuracy but impacts business throughput.
[0003] In existing technologies, in high-concurrency scenarios, in order to ensure safe contention for the local queue in multi-threaded scenarios, the entire local queue is locked. This is because only under the condition of locking can only one thread access the queue at a time. However, this will affect the throughput of log writing.
[0004] Patent document CN111699479A discloses a log processing method, device, and computer-readable storage medium. The method includes: a client sending a registration request to a server through a preset calling interface; the server receiving the registration request from the client; the server requesting shared memory for the client based on the registration request; upon successful shared memory allocation, the server sending registration success information to the client; upon receiving the registration success information from the server, the client writing its log information into the shared memory; and the server retrieving the log information from the shared memory and storing it in a preset storage path. However, this patent document differs from the technical solution of this application. Summary of the Invention
[0005] To address the shortcomings of existing technologies, the purpose of this invention is to provide an optimized method and system for server-side log recording under high concurrency.
[0006] An optimization method for server-side logging under high concurrency provided by the present invention includes the following steps:
[0007] Step 1: Add ThreadLocal functionality to the thread that needs to send messages. Using ThreadLocal, set up a local log queue independently in each business thread and add methods for writing and reading log messages.
[0008] Step 2: When a business thread needs to record logs, it records messages to its own local log queue; when the number of messages in its own local log queue reaches a threshold, it notifies the log consumption queue to retrieve messages from the local variables of its own log consumption thread.
[0009] Preferably, in step 2, the log consumption queue retrieves messages from the local queues of each business thread at preset intervals and performs message consumption processing.
[0010] Preferably, the method for writing log messages specifically involves writing messages to the local queue of the business thread itself using the set method in ThreadLocal;
[0011] The method for reading log messages specifically involves using the get method in ThreadLocal to read messages from the local queue of the business thread itself.
[0012] Preferably, the method for writing log messages specifically includes the following steps:
[0013] Step 2.1: Write log messages to a local queue;
[0014] Step 2.2: Check if the total number of messages in the local queue has reached the threshold;
[0015] Step 2.3: If the total number of messages in the local queue has not reached the threshold, no further processing is required. When the threshold is reached, a notification message is sent to the log consumption thread, and this process ends.
[0016] Preferably, the log consumption thread has the following two ways to consume log messages:
[0017] Active approach: The consumer thread calls the read log message method at preset intervals to read messages from the local queue in the consumer business thread and prints them to the log.
[0018] Passive approach: When the consumer thread receives the notification message in step 2.3, it calls the read log message method to read the message in the local queue of the consumer business thread and performs log printing operation.
[0019] Preferably, the internal process of the log message reading method is as follows: read messages from the local queue and delete the message records that have been consumed from the queue.
[0020] This invention also provides an optimized system for server-side logging under high concurrency, comprising the following modules:
[0021] Module M1: Add ThreadLocal functionality to the thread that needs to send messages. Through ThreadLocal, set up a local log queue independently in each business thread, and add modules for writing log messages and reading log messages.
[0022] Module M2: When a business thread needs to record logs, it records messages to its own local log queue. When the number of messages in its own local log queue reaches a threshold, it notifies the log consumption queue to retrieve messages from the local variables of its own log consumption thread.
[0023] Preferably, in module M2, the log consumption queue retrieves messages from the local queues of each business thread at preset intervals and performs message consumption processing.
[0024] Preferably, the log message writing module specifically performs the following process: writing messages to the local queue of its own business thread through the set method in ThreadLocal;
[0025] The log message reading module specifically performs the following process: using the get method in ThreadLocal to read messages from the local queue of its own business thread.
[0026] Preferably, the log message writing module specifically includes the following modules:
[0027] Module M2.1: Writes log messages to a local queue;
[0028] Module M2.2: Checks whether the total number of messages in the local queue has reached the threshold;
[0029] Module M2.3: If the total number of messages in the local queue has not reached the threshold, no further processing is required. When the threshold is reached, a notification message is sent to the log consumption thread, and this process ends.
[0030] Compared with the prior art, the present invention has the following beneficial effects:
[0031] 1. The present invention uses the ThreadLocal function to write and consume messages, and the message writing and consumption methods provided by its own thread ensure that only its own thread can operate on the local queue at any given time, without the need to lock and unlock the queue.
[0032] 2. In high-concurrency scenarios, this invention can significantly improve business throughput and reduce glitches in certain business scenarios.
[0033] 3. In this invention, message writing and reading are both completed by the business thread itself, avoiding the need for locking in multi-threaded writing and reading. In high-concurrency scenarios, it can effectively improve business throughput and reduce glitches in some business scenarios.
[0034] 4. This invention performs message writing and reading for each thread's own threadlocal. Because of the inherent thread data isolation in threadlocal, there is no need to perform locking operations again. Each thread only needs to write to its own threadlocal to achieve lock-free data writing, which further improves writing efficiency. Attached Figure Description
[0035] Other features, objects, and advantages of the present invention will become more apparent from the following detailed description of non-limiting embodiments with reference to the accompanying drawings:
[0036] Figure 1 This is a flowchart illustrating the steps of the optimized server-side log recording method under high concurrency according to the present invention. Detailed Implementation
[0037] The present invention will now be described in detail with reference to specific embodiments. These embodiments will help those skilled in the art to further understand the present invention, but do not limit the invention in any way. It should be noted that those skilled in the art can make several changes and improvements without departing from the concept of the present invention. These all fall within the scope of protection of the present invention.
[0038] Example 1:
[0039] like Figure 1 As shown, this embodiment provides an optimization method for server-side log recording under high concurrency, including the following steps:
[0040] Step 1: Add ThreadLocal functionality to the thread that needs to send messages. Using ThreadLocal, set up an independent local log queue in each business thread, and add methods for writing and reading log messages. The method for writing log messages specifically involves using the `set` method in ThreadLocal to write messages to the local queue of the business thread itself. The writing log message method includes the following steps:
[0041] Step 2.1: Write log messages to a local queue;
[0042] Step 2.2: Check if the total number of messages in the local queue has reached the threshold;
[0043] Step 2.3: If the total number of messages in the local queue has not reached the threshold, no further processing is required. When the threshold is reached, a notification message is sent to the log consumption thread, and this process ends.
[0044] The specific method for reading log messages is as follows: use the get method in ThreadLocal to read messages from the local queue of the business thread itself;
[0045] The log consumption thread has the following two ways to consume log messages:
[0046] Active approach: The consumer thread calls the read log message method at preset intervals to read messages from the local queue in the consumer business thread and prints them to the log.
[0047] Passive approach: When the consumer thread receives the notification message in step 2.3, it calls the read log message method to read the message in the local queue of the consumer business thread and performs log printing operation;
[0048] The internal process of the log message reading method is as follows: read messages from the local queue and delete consumed message records from the queue.
[0049] Step 2: When a business thread needs to record logs, it records messages to its own local log queue. When the number of messages in its own local log queue reaches a threshold, it notifies the log consumption queue to retrieve messages from the local variables of its own log consumption thread. The log consumption queue retrieves messages from the local queues of each business thread at preset intervals and processes the messages.
[0050] This embodiment also provides an optimization system for server-side logging under high concurrency. The optimization system for server-side logging under high concurrency can be implemented by executing the process steps of the optimization method for server-side logging under high concurrency. That is, those skilled in the art can understand the optimization method for server-side logging under high concurrency as a preferred implementation of the optimization system for server-side logging under high concurrency.
[0051] Example 2:
[0052] This embodiment provides an optimized system for server-side logging under high concurrency, including the following modules:
[0053] Module M1: Adds ThreadLocal functionality to the thread that needs to send messages. Through ThreadLocal, a local log queue is set up independently in each business thread, and a module for writing log messages and a module for reading log messages are added. The specific process of writing log messages is as follows: the set method in ThreadLocal is used to write messages to the local queue of its own business thread.
[0054] The log message reading module performs the following process: it uses the get method in ThreadLocal to read messages from the local queue of its own business thread;
[0055] The log message writing module specifically includes the following modules:
[0056] Module M2.1: Writes log messages to a local queue;
[0057] Module M2.2: Checks whether the total number of messages in the local queue has reached the threshold;
[0058] Module M2.3: If the total number of messages in the local queue has not reached the threshold, no further processing is required. When the threshold is reached, a notification message is sent to the log consumption thread, and this process ends.
[0059] Module M2: When a business thread needs to record logs, it records messages to its own local log queue. When the number of messages in its own local log queue reaches a threshold, it notifies the log consumption queue to retrieve messages from the local variables of its own log consumption thread. The log consumption queue retrieves messages from the local queues of each business thread at preset intervals and processes the messages.
[0060] Example 3:
[0061] Those skilled in the art can understand this embodiment as a more specific description of Embodiment 1 and Embodiment 2.
[0062] This embodiment provides a new logging method. Taking Java as an example, using the ThreadLocal feature, a local log queue is set up independently in each business thread. When a business thread needs to record logs, it records messages to its own local log queue (since the data recorded in ThreadLocal is naturally thread-isolated, no write locking is required). When the number of messages in the local log queue of its own thread reaches a threshold, it notifies the log consumption queue to retrieve messages from the local variables of its own thread. At the same time, the log consumption queue also retrieves messages from the local queues of each business thread at regular intervals and processes the messages.
[0063] By using the above method, writing and consuming messages through the ThreadLocal function, with the message writing and consumption methods provided by the thread itself, it is ensured that only the thread itself can operate on the local queue at any given time, without the need to lock and unlock the queue. In high-concurrency scenarios, this can greatly improve business throughput and reduce glitches in some business scenarios.
[0064] Taking the Java language as an example:
[0065] 1. Add ThreadLocal functionality to the thread that needs to send messages, and provide at least two new methods: writing log messages and reading log messages;
[0066] Write log messages: This is a private method that writes messages to the local queue of the thread itself, mainly using the set method in ThreadLocal; because this method is private, it can only be called by the thread itself.
[0067] Read log messages: This is a public method that reads messages from the thread's local queue, primarily using the get method in ThreadLocal. Because this method is public, it can be called by external threads.
[0068] 2. When a business thread needs to record logs, it assembles the log data and calls the method to write the log message. The main internal process of this method is as follows:
[0069] 2.1 Write log messages to a local queue;
[0070] 2.2 Check if the total number of messages in the local queue has reached the threshold (the specific threshold depends on the business requirements; here we assume the threshold is 20).
[0071] 2.3. If the total number of messages in the local queue has not reached the threshold, no further processing is required. When the threshold is reached, a notification message is sent to the log consumption thread, and this process ends.
[0072] 3. There are two ways for the log consumption thread to consume log messages: active and passive.
[0073] Active approach: Every certain period of time (the specific value depends on the business logic, here we assume the value is 3 seconds), the consumer thread calls the log message reading method to consume messages in the local queue of the business thread and print the logs.
[0074] Passive approach: When the consumer thread receives the notification message in step 2.3, it calls the read log message method to consume the messages in the local queue of the business thread and print the logs.
[0075] The main internal process of the method for reading log messages is as follows:
[0076] 3.1 Read messages from the local queue and delete messages that have already been consumed from the queue.
[0077] 4. Since message writing and reading are both completed by the business thread itself, the scenario of needing to lock for multi-threaded writing and reading is avoided. In high-concurrency scenarios, it can greatly improve business throughput and reduce glitches in some business scenarios.
[0078] This invention avoids the need for locking in multi-threaded write and read scenarios, and can significantly improve business throughput and reduce glitches in high-concurrency scenarios.
[0079] Those skilled in the art will understand that, besides implementing the system and its various devices, modules, and units provided by this invention in the form of purely computer-readable program code, the same functions can be achieved entirely through logical programming of the method steps, making the system and its various devices, modules, and units of this invention function in the form of logic gates, switches, application-specific integrated circuits, programmable logic controllers, and embedded microcontrollers. Therefore, the system and its various devices, modules, and units provided by this invention can be considered as a hardware component, and the devices, modules, and units included therein for implementing various functions can also be considered as structures within the hardware component; alternatively, the devices, modules, and units for implementing various functions can be considered as both software modules implementing the method and structures within the hardware component.
[0080] Specific embodiments of the present invention have been described above. It should be understood that the present invention is not limited to the specific embodiments described above, and those skilled in the art can make various changes or modifications within the scope of the claims, which do not affect the essence of the present invention. Unless otherwise specified, the embodiments and features described in this application can be arbitrarily combined with each other.
Claims
1. An optimization method for server-side logging under high concurrency, characterized in that, Includes the following steps: Step 1: Add ThreadLocal functionality to the thread that needs to send messages. Using ThreadLocal, set up a local log queue independently in each business thread and add methods for writing and reading log messages. Step 2: When a business thread needs to record logs, it records messages to its own local log queue; when the number of messages in its own local log queue reaches a threshold, it notifies the log consumption queue to retrieve messages from the local variables in its own log consumption thread. The method for writing log messages specifically includes the following steps: Step 2.1: Write log messages to a local queue; Step 2.2: Check if the total number of messages in the local queue has reached the threshold; Step 2.3: If the total number of messages in the local queue has not reached the threshold, no further processing is required. When the threshold is reached, a notification message is sent to the log consumption thread, and this process ends. The log consumption thread has the following two ways to consume log messages: Active approach: The consumer thread calls the read log message method at preset intervals to read messages from the local queue in the consumer business thread and prints them to the log. Passive approach: When the consumer thread receives the notification message in step 2.3, it calls the read log message method to read the message in the local queue of the consumer business thread and performs log printing operation; The internal process of the log message reading method is as follows: read messages from the local queue and delete consumed message records from the queue.
2. The optimization method for server-side log recording under high concurrency as described in claim 1, characterized in that, In step 2, the log consumption queue retrieves messages from the local queues of each business thread at preset intervals and processes the messages.
3. The optimization method for server-side log recording under high concurrency as described in claim 1, characterized in that, The method for writing log messages specifically involves writing messages to the local queue of the business thread itself using the set method in ThreadLocal. The method for reading log messages specifically involves using the get method in ThreadLocal to read messages from the local queue of the business thread itself.
4. An optimized system for server-side logging under high concurrency, characterized in that, The optimized system for server-side logging under high concurrency can be implemented by executing the process steps of the optimized method for server-side logging under high concurrency as described in any one of claims 1 to 3, including the following modules: Module M1: Add ThreadLocal functionality to the thread that needs to send messages. Through ThreadLocal, set up a local log queue independently in each business thread, and add modules for writing log messages and reading log messages. Module M2: When a business thread needs to record logs, it records messages to its own local log queue; when the number of messages in its own local log queue reaches a threshold, it notifies the log consumption queue to retrieve messages from the local variables in its own log consumption thread. The log message writing module specifically includes the following modules: Module M2.1: Writes log messages to a local queue; Module M2.2: Checks whether the total number of messages in the local queue has reached the threshold; Module M2.3: If the total number of messages in the local queue has not reached the threshold, no further processing is required. When the threshold is reached, a notification message is sent to the log consumption thread, and this process ends.
5. The optimized server-side log recording system under high concurrency as described in claim 4, characterized in that, In module M2, the log consumption queue retrieves messages from the local queues of each business thread at preset intervals and performs message consumption processing.
6. The optimized server-side log recording system under high concurrency as described in claim 4, characterized in that, The log message writing module specifically performs the following process: writing messages to the local queue of its own business thread through the set method in ThreadLocal; The log message reading module specifically performs the following process: using the get method in ThreadLocal to read messages from the local queue of its own business thread.
Citation Information
Patent Citations
Log processing method and equipment and computer readable storage medium
CN111699479A
Association method of internet application systems with high accuracy
CN109474669A
Abnormal reconciliation list processing method and device, computer equipment and storage medium
CN110119989A