In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
How many modes are there in the redis update cache? In response to this problem, the editor summed up this article today, hoping to help more friends who want to solve this problem to find a more simple and feasible way.
There are four kinds of Design Pattern for redis update cache: Cache aside, Read through, Write through, and Write behind caching.
Cache Aside Pattern
This is the most commonly used pattern. The specific logic is as follows:
Invalidation: the application fetches the data from the cache first, and if it doesn't get it, it fetches the data from the database, and after success, it puts it in the cache.
Hit: the application fetches the data from the cache, fetches it and returns.
Update: save the data in the database first, and then invalidate the cache after success.
Note that our update is to update the database first, and then invalidate the cache after success. So, can this approach be done without the problem mentioned earlier in the article? We can make it up.
One is the query operation, and the other is the concurrency of the update operation. First of all, there is no operation to delete the cache data, but the data in the database is updated first. At this time, the cache is still valid, so the concurrent query operation takes no updated data.
However, the update operation immediately invalidated the cache, and subsequent query operations pulled the data out of the database. Unlike the logic at the beginning of the article, subsequent query operations are always fetching old data.
Read Through
The Read Through routine is to update the cache in the query operation, that is, when the cache expires (expired or LRU swapped out), the Cache Aside is responsible for loading the data into the cache, while the Read Through is loaded with the cache service itself, so it is transparent to the application.
Write Through
The Write Through routine is similar to Read Through, except that it occurs when the data is updated. When there is a data update, if the cache is not hit, update the database directly, and then return. If the cache is hit, the cache is updated, and then Cache updates the database itself (this is a synchronous operation)
The following picture is from the Cache entry of Wikipedia. The Memory you can understand is the database in our example.
Write Behind Caching Pattern
Write Behind is also known as Write Back. Some students who know the kernel of the Linux operating system should be very familiar with write back. Isn't this the Page Cache algorithm of the Linux file system? Yes, you see, the basics are all connected. Therefore, the foundation is very important. I have not said once that the foundation is very important.
Write Back routine, that is, when updating data, only the cache is updated, not the database, while our cache updates the database in batches asynchronously.
The advantage of this design is that it makes the data operation very fast (because it manipulates memory directly), because asynchronous, write backg can also merge multiple operations on the same data, so the performance improvement is considerable.
The problem, however, is that data is not highly consistent and can be lost (we know that an abnormal shutdown of Unix/Linux can lead to data loss).
In software design, it is basically impossible for us to make a defect-free design, just like time for space and space for time in algorithm design. Sometimes, there is a conflict between strong consistency and high performance, high availability and high performance. Software design is always a trade-off between Trade-Off.
In addition, the Write Back implementation logic is more complex, because it needs to track which data is updated and needs to be brushed to the persistence layer. The write back of the operating system will only be persisted when the cache needs to fail, for example, there is not enough memory, or the process exits, which is also called lazy write.
There is a flow chart of write back on wikipedia. The basic logic is as follows:
So much for sharing the mode of redis update cache. I hope the above content can be helpful to you and learn more. 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.
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.