Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to achieve redis data consistency

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you how to achieve redis data consistency, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

First, the necessity of cache consistency

Still speaking from the previous article, we have solved the problem of redis cache penetration (a simple solution that can be optimized again), but we also need to focus on the issue of cache consistency when using redis. For example, after saving a new user, we should also insert this piece of data in the redis cache, and update a piece of data in the cache. The default practice of redis is: when you do not set up, the value stored in redis is the data you stored before, and the data will be synchronized only when you restart the server. Obviously, this is very undesirable. If so, you have to restart the server all the time. What a disaster it will be!

Second, business scenarios

Let me just talk about one scenario. Other scenarios are the same. The scenario is as follows: suppose we put user data into redis, when a new user registers, a new data is inserted into the database, and at the same time, the data is also inserted in redis so that the latest data can be displayed in the next query.

Let's first take a look at whether redis will do consistency operations for us without any processing by default. Now that I have 18 pieces of user data in my test database, we start the system and register to insert the 19th item of data to see if there are 19 items in the redis.

After registering, I now insert the 19th piece of data into the database.

Then we use the query caching method mentioned in the previous article to get all the information. If we look directly at the last item, we can see that it is Runqing rather than 19 in the end, so redis does not do consistent caching operations for us by default.

III. Implementation of cache consistency (scheme 1)

In fact, it is very simple to achieve consistency. After the insert operation, we take out the data and save it to the redis cache, so that we can see the new data when we query the cache again. The code is as follows:

It should be noted that I am calling findAll () of JPA directly. In fact, it is better to update the newly inserted one according to id, which is more efficient. Here is just a demonstration of how to implement it. Then register a new piece of data to see if you can see it in the cache at the same time: the newly inserted data id is 29. Let's take a look at whether the new data exists in the cache.

4. Implementation of redis cache consistency (scheme 2)

Regularly clear the data in redis, for example, set a scheduled task, every hour will clear the data in redis, that is, make the data in redis invalid, and then save or delete the data in redis before it no longer exists, so it is equivalent to reset the data to redis, so you can ensure the consistency of the data.

These are all the contents of the article "how to achieve redis data consistency". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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: 0

*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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report