In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article focuses on "how to solve the problem of data consistency between MySQL and Redis". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to solve the data consistency problem of MySQL and Redis.
Foreword:
In the case of more data reading and less writing, it is probably the most common scenario for Redis to be used as a cache. When using Redis as a cache, the general process is like this.
If cache exists in Redis, that is, cache hits, the data is returned directly.
If there is no corresponding cache in Redis, you need to query the database directly, then store it in Redis, and finally return the data.
Normally, we will set a key value for a cache and set an expiration time for the key value. If the key corresponding to the queried data expires, we directly query the database, store the queried data in Redis, then reset the expiration time, and finally return the data. The pseudo code is as follows:
/ * * obtain user details based on user name * @ author official account [cicada Mufeng] * / public User getUserInfo (String userName) {User user = redisCache.getName ("user:" + userName); if (user! = null) {return user;} / / search user = selectUserByUserName (userName) directly from the database / / write the data to Redis and set the expiration time redisCache.set ("user:" + userName, user, 30000); / / return data return user;} 1. Consistency problem
However, in the case that the key value of Redis has not expired, the user has modified his personal information, and we have to manipulate both database data and Redis data. Now we are faced with two choices:
Manipulate the data of Redis first, then the data of database
First manipulate the data of the database, then manipulate the data of Redis
No matter which method you choose, ideally, the two operations will either succeed or fail at the same time, otherwise there will be inconsistencies between Redis and database data.
Unfortunately, there is currently no framework that can guarantee the complete consistency of Redis data and database data. We can only take certain measures according to the scenario and the code required to reduce the probability of data inconsistency and achieve a tradeoff between consistency and performance.
Let's discuss some solutions for the consistency of Redis and database quality inspection data.
Option 1. Do you want to delete the cache or update it?
When the database data changes, the Redis data also needs to be operated accordingly, so is this "operation" using "update" or "delete"?
"Update" calls the set method of Redis, and the new value replaces the old value; "Delete" deletes the original cache directly, re-reads the database the next time it is queried, and then updates the Redis.
Conclusion: it is recommended to use the delete operation directly.
Because if you use the update operation, you will be faced with two choices
Update the cache before updating the database
Update the database before updating the cache
The first need not be considered, let's discuss the "update the database first, then update the cache".
If thread 1 and thread 2 perform update operations at the same time, but the execution order of each thread is shown in the figure above, it will result in data inconsistency, so from this point of view, we recommend deleting the cache directly.
In addition, there are two reasons why delete cache is recommended.
If there are more scenarios for writing to the database than for reading data, this scheme will cause the cache to be written frequently, wasting performance.
If the cache can only be obtained after a series of complex calculations, it is undoubtedly a waste of performance to calculate the cache again after each write to the database.
After clarifying this problem, we have only two choices:
Update the database before deleting the cache
Delete the cache before updating the database
2. Update the database before deleting the cache
There are two possible anomalies in this approach.
If you fail to update the database, you can catch the exception through the program, return the result directly, and no longer delete the cache, so there will be no data inconsistency.
Database update succeeded and cache deletion failed. As a result, the database is the latest data, the cache is old data, and the data is inconsistent.
What should we do in the second case? We have two ways: failed retry and asynchronous update.
3. Fail and try again
If deleting the cache fails, we can catch this exception and send the key that needs to be deleted to the message queue. Create your own consumer consumption and try to delete the key again until the deletion is successful.
This approach has a disadvantage, first of all, it will cause intrusion to the business code, and then introduce the message queue, which increases the uncertainty of the system.
4. Update cache asynchronously
Because logs are written to binlog when updating the database, we can start a service that listens for binlog changes (such as using Ali's canal open source component), and then delete the key on the client side. If the deletion fails, it is sent to the message queue.
Summary
In short, in the case of a failure to delete the cache, our approach is to keep retrying the delete operation until it succeeds. Whether it is retry or asynchronous deletion, it is the idea of ultimate consistency.
5. Delete the cache before updating the database
There are two possible exceptions to this approach:
If you fail to delete the cache, you can catch the exception through the program, return the result directly, and no longer update the database, so there will be no data inconsistency.
Cache deleted successfully, database update failed. The problem of data inconsistency may occur in multithreading
At this time, the value of the old data stored in the Redis is new data, resulting in data inconsistency. At this time, we can adopt the strategy of delayed double deletion, that is, after updating the database data, delete the cache again.
Expressed in pseudo code is:
/ * * delayed double deletion * @ author official account [cicada Mu Feng] * / public void update (String key, Object data) {/ / first delete cache redisCache.delKey (key); / / update database db.updateData (data); / / hibernate for a period of time, depending on the time taken to read the data Thread.sleep (500) / / delete the cache redisCache.delKey (key) again;} at this point, I believe you have a better understanding of "how to solve the data consistency problem of MySQL and Redis". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 221
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.