Data Processing Method, Apparatus, Device, and Medium Based on Distributed Optimistic Lock
By obtaining distributed optimistic locks in the cache and matching version numbers, the problem of inconsistency between cache and database data in distributed systems is solved, and data consistency and high concurrency performance are achieved.
Patent Information
- Application Number
- CN202510026372.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-01-08
- Publication Date
- 2025-07-29
- Estimated Expiration
- 2045-01-08
AI Technical Summary
In distributed systems, cache and database data are prone to inconsistent problems, especially when using optimistic locking mechanisms, the cache and database are inconsistent due to the failure to update the data read by the client in time.
By obtaining distributed optimistic locks in the cache, the version number and optimistic lock version number in the data write request are matched. If the match is successful, the optimistic lock version number will be updated and the lock will be released, thereby synchronizing the data in the cache and structured database.
Ensure consistency of cache and database data, prevent data overwrite problems caused by multiple threads modifying the same data item at the same time, and improve concurrency performance and read and write speed.
Smart Images

Figure CN119441244B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the technical field of distributed systems, and particularly to a data processing method, device, equipment, and medium based on a distributed optimistic lock. Background Art
[0002] Multi-Version Concurrency Control (MVCC) in MySQL is a typical way to implement an optimistic lock. It allows read operations to proceed without waiting for write operations to complete, thereby improving the concurrency performance of the system. To support this mechanism, a version field is usually added to the table structure in database design to identify the version information of each record. However, in a distributed system environment, due to communication between multiple nodes and cooperation between different services, there may be a cache layer between the client and the server in a distributed system. If the cache is not updated in a timely manner, it will cause the client to read expired data, resulting in the problem of inconsistent data between the cache and the database. Therefore, relying on the optimistic lock at the database level is prone to the problem of inconsistent data between the cache and the database.
[0003] The above content is only used to assist in understanding the technical solution of this application, and does not represent an admission that the above content is prior art. Summary of the Invention
[0004] The main purpose of this application is to provide a data processing method, device, equipment, and medium based on a distributed optimistic lock, aiming to solve the technical problem of inconsistent data between the cache and the database that is prone to occur when using an optimistic lock.
[0005] To achieve the above purpose, this application proposes a data processing method based on a distributed optimistic lock, which is applied to the current thread. The data processing method based on a distributed optimistic lock includes:
[0006] When receiving a data write request, obtain a distributed optimistic lock from the cache;
[0007] Match the request version number carried in the data write request with the optimistic lock version number recorded by the distributed optimistic lock;
[0008] If the match is successful, update the optimistic lock version number recorded by the distributed optimistic lock, and after the update is completed, release the distributed optimistic lock;
[0009] Obtain the target write data from the data write request, and update the database data in the structured database and the cache data in the cache according to the target write data.
[0010] In one embodiment, the step of updating the optimistic lock version number recorded by the distributed optimistic lock includes:
[0011] Determine the key-value pair data corresponding to the distributed optimistic lock, where the primary key of the key-value pair data is the device identifier, and the value of the key-value pair data records the optimistic lock version number;
[0012] Extract the optimistic lock version number from the key-value pair data;
[0013] Increment the optimistic lock version number based on a preset version update rule to obtain an updated version number;
[0014] Store the updated version number as the optimistic lock version number in the key-value pair data corresponding to the distributed optimistic lock.
[0015] In one embodiment, the step of updating the database data in the structured database and the cache data in the cache according to the target write data includes:
[0016] Parse the target write data to determine the table name, fields, and conditions to be written;
[0017] Construct a database update statement based on the table name, fields, and conditions;
[0018] Update the database data in the structured database based on the database update statement, and incrementally update the version number field of each database data to the corresponding database version number to determine the database version number after the update of each database data;
[0019] Update the cache data in the cache according to the updated database data, and incrementally update the version number field of each database data to the corresponding cache version number to determine the cache version number after the update of each database data, where the initial values of the optimistic lock version number, cache version number, and database version number are the same.
[0020] In one embodiment, the step of obtaining the distributed optimistic lock from the cache when a data write request is received further includes:
[0021] Obtain a preset synchronization lock. In the case where a thread obtains the preset synchronization lock, only the current thread is allowed to perform the operations of obtaining the distributed optimistic lock and updating the optimistic lock version number recorded by the distributed optimistic lock;
[0022] After the step of updating the optimistic lock version number recorded by the distributed optimistic lock and releasing the distributed optimistic lock after the update is completed, it further includes:
[0023] Release the preset synchronization lock.
[0024] In one embodiment, the data processing method based on the distributed optimistic lock further includes:
[0025] When a data reading request is received, obtain a distributed optimistic lock from the cache;
[0026] Match the request version number carried in the data reading request with the updated optimistic lock version number recorded by the distributed optimistic lock;
[0027] If the match is successful, obtain the target data corresponding to each data reading request from the cached data in the cache;
[0028] If the match fails, return a match failure message.
[0029] In one embodiment, after the step of obtaining the target data corresponding to each data reading request from the cached data in the cache, the method further includes:
[0030] If the target data does not exist in the cached data in the cache, obtain the target data corresponding to each data reading request from the structured database, and update the cached data in the cache according to each target data.
[0031] In addition, to achieve the above object, the present application further provides a data processing device based on a distributed optimistic lock, and the data processing device based on a distributed optimistic lock includes:
[0032] A lock acquisition module, configured to obtain a distributed optimistic lock from a cache when a data write request is received;
[0033] A matching module, configured to match the request version number carried in the data write request with the optimistic lock version number recorded by the distributed optimistic lock;
[0034] An update module, configured to, if the match is successful, update the optimistic lock version number recorded by the distributed optimistic lock, and release the distributed optimistic lock after the update is completed;
[0035] A write module, configured to obtain target write data from the data write request, and update the database data in the structured database and the cached data in the cache according to the target write data.
[0036] In addition, to achieve the above object, the present application further provides a data processing device based on a distributed optimistic lock, and the device includes: a memory, a processor, and a computer program stored on the memory and executable on the processor, and the computer program is configured to implement the steps of the data processing method based on a distributed optimistic lock as described above.
[0037] In addition, to achieve the above object, the present application further provides a storage medium, which is a computer-readable storage medium. A computer program is stored on the storage medium, and when the computer program is executed by a processor, the steps of the data processing method based on the distributed optimistic lock as described above are implemented.
[0038] In addition, to achieve the above object, the present application further provides a computer program product, which includes a computer program. When the computer program is executed by a processor, the steps of the data processing method based on the distributed optimistic lock as described above are implemented.
[0039] In the present application, when a data write request is received, a distributed optimistic lock is obtained from the cache; the request version number carried in the data write request is matched with the optimistic lock version number recorded by the distributed optimistic lock; if the match is successful, the optimistic lock version number recorded by the distributed optimistic lock is updated, and after the update is completed, the distributed optimistic lock is released; the target write data is obtained from the data write request, and the database data in the structured database and the cache data in the cache are updated according to the target write data. The present application sets the optimistic lock in the cache instead of in the structured database, improving the concurrency performance and read / write speed. Through the version number matching mechanism, it is ensured that only when the version number carried in the request is the same as the currently recorded version number is the update allowed, effectively preventing the data overwrite problem caused by multiple threads modifying the same data item simultaneously, thereby maintaining data consistency. BRIEF DESCRIPTION OF THE DRAWINGS
[0040] The drawings herein are incorporated into the specification and form a part of the specification, showing embodiments consistent with the present application, and are used together with the specification to explain the principles of the present application.
[0041] To more clearly illustrate the technical solutions in the embodiments of the present application or the prior art, the following will briefly introduce the drawings required for use in the description of the embodiments or the prior art. Obviously, for those of ordinary skill in the art, other drawings can be obtained based on these drawings without creative efforts.
[0042] Figure 1 It is a schematic flowchart provided for Embodiment 1 of the data processing method based on the distributed optimistic lock of the present application;
[0043] Figure 2 It is a brief schematic flowchart of the data processing method based on the distributed optimistic lock provided for Embodiment 2 of the present application;
[0044] Figure 3 It is a schematic flowchart provided for Embodiment 2 of the data processing method based on the distributed optimistic lock of the present application;
[0045] Figure 4 The figure is a schematic module structure diagram of the data processing device based on the distributed optimistic lock according to the embodiment of the present application;
[0046] Figure 5 The figure is a schematic device structure diagram of the hardware operating environment involved in the data processing method based on the distributed optimistic lock according to the embodiment of the present application.
[0047] The implementation, functional features and advantages of the present application will be further described in conjunction with the embodiments and with reference to the accompanying drawings. Detailed implementation manners
[0048] It should be understood that the specific embodiments described herein are only used to explain the technical solutions of the present application and are not used to limit the present application.
[0049] In order to better understand the technical solutions of the present application, the following will be described in detail in conjunction with the accompanying drawings of the specification and specific implementation manners.
[0050] It should be noted that the execution subject of this embodiment can be a computing service device with data processing, network communication and program running functions, such as a tablet computer, a personal computer, a mobile phone, etc., or an electronic device, a thread, etc. that can implement the above functions. Hereinafter, taking the current thread as an example, this embodiment and the following embodiments will be described.
[0051] Based on this, the embodiment of the present application provides a data processing method based on a distributed optimistic lock, referring to Figure 1 , Figure 1 The figure is a schematic flowchart of the first embodiment of the data processing method based on the distributed optimistic lock of the present application.
[0052] In this embodiment, the data processing method based on the distributed optimistic lock includes steps S10 to S40:
[0053] Step S10, when a data writing request is received, obtain a distributed optimistic lock from the cache.
[0054] It should be noted that the execution subject of this embodiment is the current thread. In a distributed system, when an application receives a data write request, it will assign this request to an available thread for processing. The thread will call the API (Application Programming Interface) provided by the Redis (Remote Dictionary Server) client library to attempt to acquire a lock. Each thread is independent when processing write requests, and they each attempt to acquire the lock without interfering with each other. Even if multiple threads attempt to acquire the lock for the same resource simultaneously, only the first successful thread can continue to execute the update operation, and the other threads will be blocked or required to retry. The cache in this embodiment refers to Redis. Redis is an open-source, high-performance in-memory database, usually used as a data structure server. All data is stored in memory, so Redis provides extremely high read and write speeds and is suitable for application scenarios that require quick responses.
[0055] First, it is necessary to determine whether there is a lock key corresponding to the target resource in the cache. The lock key is usually composed of a resource ID (Identity document) and a version number. Redis provides the SETNX (Set if Not Exists) command, which will only set the key-value pair when the key does not exist. If the return value is 1, it means the lock has been successfully acquired; if the return value is 0, it means the lock is already held by another client. By acquiring the lock, it can be ensured that only one thread can modify the database at the same time, preventing data conflicts or overwrites caused by multiple threads updating simultaneously.
[0056] Step S20: Match the request version number carried in the data write request with the optimistic lock version number recorded by the distributed optimistic lock.
[0057] It should be noted that the request version number is extracted from the data write request. This version number is usually obtained when reading the data and is sent to the server side together with the subsequent write request for data update. The optimistic lock version number of the distributed optimistic lock record is obtained from the cache. This version number reflects the latest state of the current resource. The request version number and the optimistic lock version number are compared strictly numerically or as strings. If the two are equal, the version is considered to match; otherwise, the version does not match. If the version numbers match, the update operation is allowed to be executed, and the version number is incremented to reflect the new state; if the version numbers do not match, the update request is rejected, and usually an error is returned to the client, prompting the user to retry or take other measures.
[0058] If there is a version number mismatch, it may be because other threads have updated the same resource, resulting in a change in the optimistic lock version number. Or due to network latency or other factors, the request version number read by the client may be expired. If the lock is set with a TTL (Time To Live), it may be automatically released due to timeout, causing other threads to acquire the lock and update the version number.
[0059] By matching the version numbers, it can be ensured that an update is allowed only when the request version number carried in the request is the same as the optimistic lock version number of the current record, effectively preventing the data overwrite problem caused by multiple threads modifying the same data item simultaneously. The version number mechanism is not only used to control concurrent access but also helps to track the historical changes of data, facilitating auditing and rollback operations.
[0060] Step S30, if the match is successful, update the optimistic lock version number of the distributed optimistic lock record, and after the update is completed, release the distributed optimistic lock.
[0061] It should be noted that the operations of updating the optimistic lock version number and releasing the optimistic lock are both executed by the current thread. Updating the optimistic lock version number of the distributed optimistic lock record is to reflect the latest state of the resource. The version number is incremented after each successful update to ensure that each version is unique, thus maintaining data consistency and historical traceability.
[0062] By updating the version number and then releasing the lock, it can be ensured that only one thread can modify a specific resource at the same time, avoiding the data overwrite problem caused by multiple threads updating simultaneously.
[0063] In a feasible implementation manner, in step S30, it may include steps A10 to A40:
[0064] Step A10, determine the key-value pair data corresponding to the distributed optimistic lock, where the primary key of the key-value pair data is the device identifier, and the value of the key-value pair data records the optimistic lock version number.
[0065] It should be noted that the distributed optimistic lock exists in the form of key-value pair data, such as id_1, (id_1 + v_2), where the primary key id_1 is the device identifier, and the value (id_1 + v_2) is a combination of the device identifier and the optimistic lock version number.
[0066] Step A20, extract the optimistic lock version number from the key-value pair data.
[0067] Step A30, increment the optimistic lock version number based on a preset version update rule to obtain an updated version number.
[0068] It should be noted that in the most common case of the preset version update rule, the second_version is simply incremented by 1. This rule applies to most scenarios because it can effectively track the number of changes to resources.
[0069] Step A40, store the updated version number as the optimistic lock version number in the key-value pair data corresponding to the distributed optimistic lock.
[0070] It should be noted that for the original distributed optimistic lock, the updated version number needs to replace the original optimistic lock version number in the key-value pair data to update the distributed optimistic lock.
[0071] In a feasible implementation manner, while step S10 is being executed, step S01 may also be included:
[0072] Step S01, obtain a preset synchronization lock. Among them, when a thread obtains the preset synchronization lock, only the current thread is allowed to perform operations such as obtaining the distributed optimistic lock and updating the optimistic lock version number of the distributed optimistic lock record.
[0073] It should be noted that in this embodiment, the preset synchronization lock can be implemented through synchronized. Synchronized can be applied to methods or code blocks to restrict access to shared resources by multiple threads. Only one thread can enter the method or code block protected by synchronized at the same time, and other threads must wait until the current thread releases the lock.
[0074] Then, after step S30, step S02 is also included:
[0075] Step S02, release the preset synchronization lock.
[0076] In Java, when a thread leaves the method or code block protected by synchronized, the Java virtual machine will automatically release the lock held by the thread. A synchronized method locks the current instance object, while a synchronized code block can specify any object as the lock object. Once the thread leaves these methods or code blocks, the lock will be automatically released.
[0077] In this implementation manner, in a distributed system or a high-concurrency environment, before a thread attempts to execute a critical operation, it first obtains a preset synchronization lock to ensure that only one thread can execute these operations at the same time, which can further ensure thread safety and data consistency.
[0078] Step S40, obtain the target write data from the data write request, and update the database data in the structured database and the cache data in the cache according to the target write data.
[0079] Specifically, the target data to be updated is parsed from the data write request. Usually, the request contains the data items to be updated and their version number information. Based on the parsed target data, a corresponding SQL (Structured Query Language) update statement is constructed, and it is ensured that all related changes are either all successful or all rolled back. While updating the structured database, the version number is incremented to reflect the latest data state. After updating the structured database, the latest data is immediately read from the database instead of directly using the data in the write request, which can ensure that the data in the cache is the latest and avoid data inconsistency caused by concurrent updates. The latest data is updated to the cache to ensure that the cache is synchronized with the persistent layer.
[0080] It can be understood that if the cache is updated first and then the structured database, partial updates may occur due to certain reasons (such as network failures, server crashes), that is, only the cache is updated but the database is not, or only the database is updated but the cache is not. Therefore, always update the structured database first and then the cache. Even if the cache update fails, it will not affect the data consistency of the persistent layer. Since the version number matching mechanism is adopted to ensure that each update is based on the latest data state, there will be no problem of concurrent conflicts caused by multiple threads almost trying to update the same resource.
[0081] Specifically, the cache contains a distributed optimistic lock. The distributed optimistic lock is key-value pair data. The primary key of the key-value pair data is the device identifier, and the value of the key-value pair data records the optimistic lock version number.
[0082] It should be noted that the distributed optimistic lock is a concurrency control mechanism used to ensure that there are no conflicts when multiple clients update the same data in a high-concurrency environment. It tracks the changes of data through version numbers or other identifiers, so that the system can check whether other transactions have modified the data before performing the update operation. The device identifier is used to uniquely identify each device, and the optimistic lock version number records the version number used by the optimistic lock.
[0083] The cache also contains multiple pieces of cache data. The cache data is a cache of database data. Each piece of cache data contains a version number field, and the version number field of the cache data is used to record the cache version number.
[0084] It should be noted that the cache data is a copy of the data in the structured database, used to accelerate the read operation. The cache version number records the version of the cache data to ensure consistency with the data in the structured database. The cache version number ensures that the data in the cache is the latest and is consistent with the data in the structured database.
[0085] The structured database contains multiple database data entries, and each database data entry contains a version number field. The version number field of the database data is used to record the database version number. The initial values of the optimistic lock version number, cache version number, and database version number are the same.
[0086] It should be noted that the database data stores the actual business data, which contains multiple fields such as id, name, value, etc. The database version number records the current version of this record and is used to support the optimistic lock mechanism. By incrementing the database version number, it is ensured that each update is based on the latest data version, preventing concurrent conflicts.
[0087] The initial values of the request version number, cache version number, and database version number are the same, usually 1 or other specified starting values. The optimistic lock version number is incremented when a distributed optimistic lock in the cache is requested to write data, ensuring that only one thread can perform the data write operation; the cache version number is incremented when the cached data in the cache is updated, ensuring consistency with the structured database; the database version number is incremented each time an update occurs in the structured database, ensuring that each update is based on the latest version of the data.
[0088] In a feasible implementation manner, in step S40, steps B10 to B40 may be included:
[0089] Step B10, parse the target write data to determine the table name, fields, and conditions to be written.
[0090] It should be noted that the user submits an update request through a certain interface, carrying the data to be updated, usually sent in JSON (JavaScript Object Notation) or XML (Extensible Markup Language) format, containing specific update information. The target write data contains various information, including: the table name, which is the name of the database table to be updated; the fields, the specific fields to be updated and their new values; the conditions, that is, which records should be updated, usually a WHERE clause in an SQL query, used to precisely match the records to be updated.
[0091] Step B20, construct a database update statement based on the table name, fields, and conditions.
[0092] It should be noted that starting from a basic SQL update statement template, including the UPDATE keyword, table name, SET clause, and WHERE clause. According to the parsed fields and their new values, the SET clause is dynamically constructed. According to the parsed conditions, the WHERE clause is dynamically constructed. The SET clause and the WHERE clause are combined together to form a complete SQL update statement.
[0093] Step B30: Update the database data in the structured database based on the database update statement, incrementally update the version number field of each database data corresponding to the database version number, and determine the database version number after the update of each database data.
[0094] It should be noted that a complete SQL update statement has been constructed in Step B20. Now, this statement needs to be executed through JDBC (Java Database Connectivity) or other database connection libraries. The increment operation of the version number field is included in the SET clause, such as fifth_version = fifth_version + 1, to ensure that the version number will be automatically incremented each time a successful update is made.
[0095] Step B40: Update the cached data in the cache according to the updated database data, incrementally update the version number field of each database data corresponding to the cache version number, and determine the cache version number after the update of each database data. Among them, the initial values of the optimistic lock version number, cache version number, and database version number are the same.
[0096] Specifically, execute a query in the structured database to obtain the just-updated data, map the data obtained from the database to the cache object in the cache, and update the data in the cache through Redis commands or client libraries. Maintain an additional version number field (such as fourth_version) for each record in the cache for synchronous updates. Each time the cache is updated, increment the value of the fourth_version field. After the update operation is completed, query and confirm the latest fourth_version.
[0097] This embodiment can ensure data consistency and integrity in a high-concurrency environment, while improving the system performance and user experience. It not only solves potential data conflict and inconsistency problems, but also greatly improves the system performance and reliability.
[0098] In this embodiment, when it is detected that the current thread receives a data writing request, a distributed optimistic lock is obtained from the cache; the request version number carried in the data writing request is matched with the optimistic lock version number recorded by the distributed optimistic lock; if the match is successful, the optimistic lock version number recorded by the distributed optimistic lock is updated, and after the update is completed, the distributed optimistic lock is released; the target writing data is obtained from the data writing request, and the structured database and the cache are updated according to the target writing data. In this embodiment, the optimistic lock is set in the cache instead of in the structured database, which improves the concurrency performance and the read-write speed. Through the version number matching mechanism, it is ensured that only when the version number carried by the request is consistent with the currently recorded version number is the update allowed, effectively preventing the data overwrite problem caused by multiple threads modifying the same data item simultaneously, thus maintaining the data consistency.
[0099] Exemplarily, to help understand the implementation process of the data processing method based on the distributed optimistic lock obtained by combining this embodiment with the above-mentioned first embodiment, please refer to Figure 2 , Figure 2 A brief flow schematic diagram of the data processing method based on the distributed optimistic lock is provided. Specifically: The process starts from the "Start" node. Thread A obtains the lock and matches the version, and passes; at the same time, Thread B also tries to obtain the lock, but since the lock is occupied by Thread A, Thread B enters the blocked state; Thread A changes the version to v_2; Thread A temporarily releases the lock and enters the blocked state, waiting for subsequent operations; Thread B tries to obtain the lock again, but at this time the version has been updated to v_2, which does not match the expected version of Thread B. Therefore, Thread B directly returns, indicating failure; Thread A is unblocked and continues to execute, modifying the data in the structured database; Thread A updates the cache data in the unstructured database (such as Redis); the process ends.
[0100] Based on the first embodiment of the present application, in the second embodiment of the present application, the same or similar content as the above-mentioned first embodiment can be referred to the above introduction and will not be repeated hereinafter. On this basis, please refer to Figure 3 , the data processing method based on the distributed optimistic lock further includes steps S50 to S80:
[0101] Step S50, when a data reading request is received, obtain a distributed optimistic lock from the cache.
[0102] It should be noted that after the data reading request is sent, if the optimistic lock version number is modified, it means that other threads have updated the resource. At this time, directly reading data from the cache may result in reading expired data. By obtaining the optimistic lock and performing version number matching, it is ensured that the reading operation is based on the latest data status. Although the reading operation itself does not require locking, in this case, obtaining the optimistic lock can ensure that only one thread can successfully read the latest data at the same time, and subsequent write operations can also be based on the latest version.
[0103] Step S60, match the request version number carried in the data reading request with the updated optimistic lock version number recorded by the distributed optimistic lock.
[0104] It should be noted that the request version number is the version number carried by the client when initiating a reading request, usually obtained from previous reading or writing operations. The optimistic lock version number is the latest version number of the distributed optimistic lock record in the cache, reflecting the latest status of the resource.
[0105] Step S70, if the match is successful, obtain the target data corresponding to each data reading request from the cached data in the cache.
[0106] It should be noted that the cache is usually used as a cache layer to store copies of data in a structured database. Each piece of cached data contains a version number field for recording the optimistic lock version number synchronized with the structured database. When the version numbers match successfully, the server side will look up the corresponding data in the cache according to the resource identifier in the request. If the corresponding data exists in the cache, these data will be directly returned to the client.
[0107] In addition, after successfully reading the data, the read version number will also be returned to the client for the client to use as the request version number or the optimistic lock version number when performing data writing or data reading next time.
[0108] In a feasible implementation, after step S70, step C10 may further be included:
[0109] Step C10, if the target data does not exist in the cached data of the cache, obtain the target data corresponding to each data reading request from the structured database and update the cached data in the cache according to each target data.
[0110] It should be noted that if the corresponding data does not exist in the cache, it may be because the data has not been cached yet or the cache has expired. At this time, it is necessary to reread the latest data from the structured database and update it to the cache to ensure that the cache is consistent with the persistent layer.
[0111] Step S80, if the match fails, return a match failure message.
[0112] Other threads or clients updated the same resource within the same time window, resulting in a change in the version number. Or due to network latency or other factors, the request version number read by the client may be expired.
[0113] In this embodiment, when it is detected that multiple threads receive data reading requests, a distributed optimistic lock is obtained from the cache; the request version number carried in the data reading request is matched with the updated optimistic lock version number recorded by the distributed optimistic lock; if the match is successful, the target data corresponding to each data reading request is obtained from the cached data in the cache; if the match fails, a match failure message and the updated optimistic lock version number are returned, and an updated data reading request is received based on the updated optimistic lock version number, and the following execution steps are returned according to the updated data reading request: obtain a distributed optimistic lock from the cache. This embodiment ensures that each read is based on the latest data state through a version number matching mechanism, effectively preventing the "dirty read" problem, and can ensure data consistency and integrity in a high-concurrency environment, while improving system performance and user experience.
[0114] It should be noted that the above examples are only for understanding this application and do not constitute a limitation on the data processing method based on the distributed optimistic lock of this application. Any simple transformation in more forms based on this technical concept is within the protection scope of this application.
[0115] This application also provides a data processing device based on a distributed optimistic lock. Please refer to Figure 4 , the data processing device based on the distributed optimistic lock includes:
[0116] The lock acquisition module 10 is used to obtain a distributed optimistic lock from the cache when a data write request is received;
[0117] The matching module 20 is used to match the request version number carried in the data write request with the optimistic lock version number recorded by the distributed optimistic lock;
[0118] The update module 30 is used to, if the match is successful, update the optimistic lock version number recorded by the distributed optimistic lock, and after the update is completed, release the distributed optimistic lock;
[0119] A writing module 40 is configured to obtain target writing data from the data writing request and update the database data in the structured database and the cache data in the cache according to the target writing data. The data processing device based on the distributed optimistic lock provided in this application adopts the data processing method based on the distributed optimistic lock in the above embodiment, which can solve the technical problem that the cache and the database data are prone to inconsistency when the optimistic lock is applied. Compared with the prior art, the beneficial effects of the data processing device based on the distributed optimistic lock provided in this application are the same as those of the data processing method based on the distributed optimistic lock provided in the above embodiment, and other technical features in the data processing device based on the distributed optimistic lock are the same as the features disclosed in the method of the above embodiment, which will not be elaborated herein.
[0120] This application provides a data processing device based on a distributed optimistic lock. The data processing device based on the distributed optimistic lock includes: at least one processor; and a memory communicatively connected to the at least one processor; wherein, the memory stores instructions executable by the at least one processor, and the instructions are executed by the at least one processor to enable the at least one processor to execute the data processing method based on the distributed optimistic lock in the first embodiment above.
[0121] Reference is made below to Figure 5 , which shows a schematic structural diagram of a data processing device based on a distributed optimistic lock suitable for implementing the embodiments of the present application. The data processing device based on the distributed optimistic lock in the embodiments of the present application may include, but is not limited to, mobile terminals such as mobile phones, laptop computers, digital broadcast receivers, PDAs (Personal Digital Assistants), PADs (Portable Application Descriptions), PMPs (Portable Media Players), in-vehicle terminals (such as in-vehicle navigation terminals), etc., and fixed terminals such as digital TVs, desktop computers, etc. Figure 5 The data processing device based on the distributed optimistic lock shown is only an example and should not impose any limitations on the functions and usage scope of the embodiments of the present application.
[0122] As Figure 5As shown, the data processing device based on the distributed optimistic lock may include a processing device 1001 (such as a central processing unit, a graphics processing unit, etc.), which may perform various appropriate actions and processes according to a program stored in a read-only memory (ROM: Read Only Memory) 1002 or a program loaded from a storage device 1003 into a random access memory (RAM: Random Access Memory) 1004. In the RAM 1004, various programs and data required for the operation of the data processing device based on the distributed optimistic lock are also stored. The processing device 1001, the ROM 1002, and the RAM 1004 are connected to each other through a bus 1005. An input / output (I / O) interface 1006 is also connected to the bus. Generally, the following systems may be connected to the I / O interface 1006: an input device 1007 including, for example, a touch screen, a touchpad, a keyboard, a mouse, an image sensor, a microphone, an accelerometer, a gyroscope, etc.; an output device 1008 including, for example, a liquid crystal display (LCD: Liquid Crystal Display), a speaker, a vibrator, etc.; a storage device 1003 including, for example, a magnetic tape, a hard disk, etc.; and a communication device 1009. The communication device 1009 may allow the data processing device based on the distributed optimistic lock to communicate with other devices wirelessly or wiredly to exchange data. Although the figure shows a data processing device based on the distributed optimistic lock having various systems, it should be understood that it is not required to implement or have all the shown systems. More or fewer systems may be implemented or had alternatively.
[0123] In particular, according to the embodiments disclosed in the present application, the processes described above with reference to the flowcharts may be implemented as computer software programs. For example, the embodiments disclosed in the present application include a computer program product, which includes a computer program carried on a computer-readable medium, and the computer program includes program codes for executing the methods shown in the flowcharts. In such an embodiment, the computer program may be downloaded and installed from a network through the communication device, or installed from the storage device 1003, or installed from the ROM 1002. When the computer program is executed by the processing device 1001, the above functions defined in the methods of the embodiments disclosed in the present application are executed.
[0124] The data processing device based on the distributed optimistic lock provided in the present application adopts the data processing method based on the distributed optimistic lock in the above embodiments, and can solve the technical problem that it is easy to have inconsistent data between the cache and the database when applying the optimistic lock. Compared with the prior art, the beneficial effects of the data processing device based on the distributed optimistic lock provided in the present application are the same as those of the data processing method based on the distributed optimistic lock provided in the above embodiments, and other technical features in the data processing device based on the distributed optimistic lock are the same as the features disclosed in the method of the previous embodiment, and will not be elaborated here.
[0125] It should be understood that each part disclosed in this application can be implemented by hardware, software, firmware, or a combination thereof. In the description of the above embodiments, specific features, structures, materials, or characteristics can be combined in a suitable manner in any one or more embodiments or examples.
[0126] As mentioned above, it is only the specific implementation manner of this application, but the protection scope of this application is not limited thereto. Any person skilled in the art within the technical scope disclosed in this application can easily think of changes or substitutions, which should all be covered within the protection scope of this application. Therefore, the protection scope of this application should be subject to the protection scope of the claims.
[0127] This application provides a computer-readable storage medium having computer-readable program instructions (i.e., computer programs) stored thereon, and the computer-readable program instructions are used to execute the data processing method based on distributed optimistic locks in the above embodiments.
[0128] The computer-readable storage medium provided by this application can be, for example, a USB flash drive, but is not limited to electrical, magnetic, optical, electromagnetic, infrared, or semiconductor systems or devices, or any combination of the above. More specific examples of computer-readable storage media can include, but are not limited to: electrical connections with one or more wires, portable computer disks, hard disks, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM) or flash memory, optical fibers, portable compact disk read-only memory (CD-ROM), optical storage devices, magnetic storage devices, or any suitable combination of the above. In this embodiment, the computer-readable storage medium can be any tangible medium that contains or stores a program, and this program can be used by or in combination with an instruction execution system or device. The program code contained on the computer-readable storage medium can be transmitted by any appropriate medium, including but not limited to: wires, optical cables, RF (Radio Frequency), etc., or any suitable combination of the above.
[0129] The above computer-readable storage medium can be included in the data processing device based on distributed optimistic locks; it can also exist separately without being assembled into the data processing device based on distributed optimistic locks.
[0130] The above computer-readable storage medium carries one or more programs, which, when executed by a data processing device based on a distributed optimistic lock, cause the data processing device based on a distributed optimistic lock to: when receiving a data write request, obtain a distributed optimistic lock from a cache; match the request version number carried in the data write request with the optimistic lock version number recorded by the distributed optimistic lock; if the match is successful, update the optimistic lock version number recorded by the distributed optimistic lock, and after the update is completed, release the distributed optimistic lock; obtain target write data from the data write request, and update the database data in the structured database and the cache data in the cache according to the target write data.
[0131] Computer program code for performing the operations of this application can be written in one or more programming languages or combinations thereof. The programming languages include object-oriented programming languages such as Java, Smalltalk, C++, and also include conventional procedural programming languages such as the "C" language or similar programming languages. The program code can be executed entirely on the user's computer, partially on the user's computer, executed as an independent software package, partially on the user's computer and partially on a remote computer, or entirely on a remote computer or server. In the case of a remote computer, the remote computer can be connected to the user's computer through any type of network, including a local area network (LAN) or a wide area network (WAN), or can be connected to an external computer (for example, by using an Internet service provider to connect through the Internet).
[0132] The flowcharts and block diagrams in the accompanying drawings illustrate the possible architectures, functions, and operations of systems, methods, and computer program products according to various embodiments of this application. In this regard, each block in the flowchart or block diagram can represent a module, a program segment, or a part of code that contains one or more executable instructions for implementing the specified logical function. It should also be noted that in some alternative implementations, the functions marked in the blocks may occur in a different order than that marked in the accompanying drawings. For example, two consecutive blocks shown may actually be executed substantially in parallel, and they may sometimes be executed in the reverse order, depending on the functions involved. It should also be noted that each block in the block diagram and / or flowchart, and the combination of blocks in the block diagram and / or flowchart, can be implemented by a dedicated hardware-based system for performing the specified functions or operations, or can be implemented by a combination of dedicated hardware and computer instructions.
[0133] The modules involved in the embodiments of the present application can be implemented in software or in hardware. In some cases, the name of the module does not constitute a limitation on the unit itself.
[0134] The readable storage medium provided by the present application is a computer-readable storage medium. The computer-readable storage medium stores computer-readable program instructions (i.e., computer programs) for executing the above-mentioned data processing method based on distributed optimistic locks, and can solve the technical problem that data inconsistency between the cache and the database is likely to occur when applying optimistic locks. Compared with the prior art, the beneficial effects of the computer-readable storage medium provided by the present application are the same as those of the data processing method based on distributed optimistic locks provided by the above embodiments, and will not be elaborated here.
[0135] The present application also provides a computer program product, including a computer program, and when the computer program is executed by a processor, the steps of the data processing method based on distributed optimistic locks as described above are implemented.
[0136] The computer program product provided by the present application can solve the technical problem that data inconsistency between the cache and the database is likely to occur when applying optimistic locks. Compared with the prior art, the beneficial effects of the computer program product provided by the present application are the same as those of the data processing method based on distributed optimistic locks provided by the above embodiments, and will not be elaborated here.
[0137] The above are only some embodiments of the present application, and thus do not limit the patent scope of the present application. Any equivalent structural transformation made by using the content of the specification and drawings of the present application under the technical concept of the present application, or any direct / indirect application in other related technical fields, is included in the patent protection scope of the present application.
Claims
1. A data processing method based on a distributed optimistic lock, characterized in that Applied to the current thread, the data processing method based on the distributed optimistic lock includes: When a data write request is received, obtain a distributed optimistic lock from the cache. The distributed optimistic lock is key-value pair data, the primary key of the key-value pair data is the device identifier, and the value of the key-value pair data records the optimistic lock version number; Obtain a preset synchronization lock. Among them, when the thread obtains the preset synchronization lock, only the current thread is allowed to perform operations such as obtaining the distributed optimistic lock and updating the optimistic lock version number recorded by the distributed optimistic lock; Match the request version number carried in the data write request with the optimistic lock version number recorded by the distributed optimistic lock; If the match is successful, increment the optimistic lock version number recorded by the distributed optimistic lock, and after the update is completed, release the distributed optimistic lock; Release the preset synchronization lock; Obtain the target write data from the data write request, and update the database data in the structured database and the cache data in the cache according to the target write data; Among them, the step of updating the database data in the structured database and the cache data in the cache according to the target write data includes: Parse the target write data to determine the table name, fields, and conditions to be written; Construct a database update statement based on the table name, fields, and conditions; Update the database data in the structured database based on the database update statement, and incrementally update the version number field of each database data corresponding to the database version number to determine the database version number after the update of each database data; Update the cache data in the cache according to the updated database data, and incrementally update the version number field of each database data corresponding to the cache version number to determine the cache version number after the update of each database data. Among them, the initial values of the optimistic lock version number, cache version number, and database version number are the same.
2. The data processing method based on a distributed optimistic lock according to claim 1, wherein The step of updating the optimistic lock version number recorded by the distributed optimistic lock includes: Determine the key-value pair data corresponding to the distributed optimistic lock. The primary key of the key-value pair data is the device identifier, and the value of the key-value pair data records the optimistic lock version number; Extract the optimistic lock version number from the key-value pair data; Increment the optimistic lock version number based on a preset version update rule to obtain an updated version number; Store the updated version number as the optimistic lock version number in the key-value pair data corresponding to the distributed optimistic lock.
3. The data processing method based on a distributed optimistic lock according to claim 1, wherein The data processing method based on the distributed optimistic lock further includes: When a data read request is received, obtain a distributed optimistic lock from the cache; Match the request version number carried in the data read request with the updated optimistic lock version number recorded by the distributed optimistic lock; If the match is successful, obtain the target data corresponding to each data read request from the cache data in the cache; If the match fails, return a match failure message.
4. The data processing method based on distributed optimistic lock according to claim 3, wherein After the step of obtaining the target data corresponding to each data read request from the cache data in the cache, it further includes: If the target data does not exist in the cached data of the cache, obtain the target data corresponding to each of the data read requests from the structured database, and update the cached data in the cache according to each of the target data.
5. A data processing device based on a distributed optimistic lock, characterized in that, The data processing device based on the distributed optimistic lock includes: A lock acquisition module, configured to, when receiving a data write request, obtain a distributed optimistic lock from a cache, where the distributed optimistic lock is key-value pair data, the primary key of the key-value pair data is a device identifier, and the value of the key-value pair data records an optimistic lock version number; A matching module, configured to match the request version number carried in the data write request with the optimistic lock version number recorded by the distributed optimistic lock; An update module, configured to, if the matching is successful, increment the optimistic lock version number recorded by the distributed optimistic lock, and after the update is completed, release the distributed optimistic lock; A write module, configured to obtain target write data from the data write request, and update the database data in the structured database and the cached data in the cache according to the target write data; Wherein, the data processing device based on the distributed optimistic lock is further configured to obtain a preset synchronization lock, and wherein, when a thread obtains the preset synchronization lock, only the current thread is allowed to perform operations of obtaining the distributed optimistic lock and updating the optimistic lock version number recorded by the distributed optimistic lock; after the step of updating the optimistic lock version number recorded by the distributed optimistic lock and releasing the distributed optimistic lock after the update is completed, it is further configured to: release the preset synchronization lock; Wherein, the write module is specifically configured to: Parse the target write data to determine the table name, fields, and conditions to be written; Construct a database update statement based on the table name, fields, and conditions; Update the database data in the structured database based on the database update statement, and incrementally update the version number field of each database data to the corresponding database version number to determine the database version number after the update of each database data; Update the cached data in the cache according to the updated database data, and incrementally update the version number field of each database data to the corresponding cache version number to determine the cache version number after the update of each database data, where the initial values of the optimistic lock version number, cache version number, and database version number are the same.
6. A data processing device based on a distributed optimistic lock, characterized in that, The device includes: a memory, a processor, and a computer program stored on the memory and executable on the processor, where the computer program is configured to implement the steps of the data processing method based on the distributed optimistic lock according to any one of claims 1 to 4.
7. A storage medium, characterized in that, The storage medium is a computer-readable storage medium, and a computer program is stored on the storage medium, and when the computer program is executed by a processor, it implements the steps of the data processing method based on the distributed optimistic lock according to any one of claims 1 to 4.
8. A computer program product, characterized in that, The computer program product includes a computer program, and when the computer program is executed by a processor, it implements the steps of the data processing method based on the distributed optimistic lock according to any one of claims 1 to 4.
Citation Information
Patent Citations
Service data processing method and device and electronic equipment
CN113220669A