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 ensure the consistency of double writes between mysql and redis

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article will explain in detail how to ensure the consistency of mysql and redis, the content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

1. Scene:

Double-write consistency means that when we update the data in the database, the data in the redis is also updated synchronously. In the process of reading data using redis, when the user accesses the data, it will first read the data from the cache. If the cache is hit, then the data in the cache will be directly returned to the user. If there is no data in the cache, the database will be queried to save the queried data to the cache, and then returned to the user.

2. Strategies to ensure the consistency of double writes

1. Update the cache before updating the database

2. Update the database before updating the cache

3. Delete the cache before updating the database

4. Update the database before deleting the cache

Third, the advantages and disadvantages of the four strategies

1. Update the cache before updating the database

The problem is obvious that if the cache update is successful and the database update fails, it will cause dirty data in the cache.

2. Update the database before updating the cache

In the case of high concurrency, there may be the following situation: thread A updates the database. If, due to network or other reasons, thread A has not had time to update the cache, then a process B updates the database and the cache, and process A updates the cache only at this time. At this time, thread B's update to the cache is lost, as in the case of transaction loss.

3. Delete the cache before updating the database

This strategy may have avoided the loss of cache in strategy 2, but in the case of high concurrency, there will also be inconsistencies, such as thread A does a write operation, first deletes the cache and then prepares to follow the new database. Thread B performs a write operation, misses the cache, and then queries the database. At this time, the old value is read and the query value is saved in the cache. Then thread A completes the update of the database, and at this time there is an inconsistency between the database and the cache. The solution: we just need to thread A, finish the update of the database, and delete the cache again with a slight delay, also known as delayed double deletion. The delay time here must be greater than the time of a read operation of the business.

4. Update the database before deleting the cache

Even in the case of high concurrency, there will be inconsistencies. For example, when thread An is reading data and is preparing to write to the cache, thread B updates the database and then deletes the cache. Thread A writes the old value to the cache, although the probability of this situation is relatively low, because the write operation is longer than the time of a read operation. Solution: delay double deletion, delay double deletion is also a problem, what if delete cache fails, of course, delete again, constantly delete. After the deletion fails, we can put the deleted key into the queue, and then try to delete it again and again until the deletion is successful.

On how to ensure the consistency of mysql and redis to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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

Database

Wechat

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

12
Report