In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
How to ensure the consistency between Redis and database? In view of this problem, this article introduces the corresponding analysis and answers in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.
Generally speaking, as long as you use caching, whether it is Redis or memcache, it may involve the consistency of database caching and data. Here we take Redis as an example.
How to ensure the consistency between Redis and database?
So easy:
When updating, update the database before deleting the cache.
When reading, read the cache first; if not, read the database, put the data in the cache, and return the response.
At first glance, the consistency problem seems to have been solved very well. But if you think about it, you will find that there is still a problem: what if you update the database first and fail to delete the cache? Then there is new data in the database and old data in the cache, and the data is inconsistent.
Improvement plan:
Delete the cache before updating the database. Because even if the later update of the database fails, the cache is empty and will be pulled back from the database when reading, although it is all old data, but the data is consistent.
So the plan becomes:
When updating, delete the cache before updating the database.
When reading, read the cache first; if not, read the database, put the data in the cache, and return the response.
Is it true that the problem has been completely solved when we get here?
In fact, no, in a high concurrency scenario, there will be such a situation: when the data changes, the cache is deleted first, and then the database is modified. Before we had time to modify it, a request came, went to read the cache, found that the cache was empty, read the database, read the old data before it was ready for modification, and put the old data in the cache.
Subsequently, the data change program completed the modification of the database. Then it's over, there is a data inconsistency at this time.
Solution:
In view of this situation, you can first put the "modify DB" operation on a JVM queue, and then after the read request, the "update cache" operation is also placed in the same JVM queue. For each queue, the relevant operations are performed in the order of the queue for a job thread, so as to ensure that the "update cache" must be modified after the DB is modified to ensure data consistency, as shown below:
This is the answer to the question about how to ensure the consistency of Redis and database. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.
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: 236
*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.