In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
Preface
Caching (memory or Memcached or Redis.) is widely used in Internet projects. This blog will discuss the topic of cache breakdown, covering the phenomenon of cache breakdown, ideas to solve, and dealing with cache breakdown through code abstraction.
What is cache breakdown?
The above code is typically written: when querying, take it from the Redis cluster first, and if not, query it from DB and set it to the Redis cluster.
Note that in actual development, we are usually in the cache and the data structure stored is JSON. (the serialization method provided by JDK is slightly less efficient than JSON serialization; and JDK serialization is very strict, so the increase or decrease of fields is likely to cause reverse sequence failure, while JSON is more compatible in this respect.)
Assuming that it takes 2S to query from DB, it is obvious that all requests over this period of time will go through DB queries under the above code, which is equivalent to cache penetration directly. This phenomenon is called "cache breakdown"!
Analysis of ideas to avoid cache breakdown
Add synchronized?
If synchronized is added to the method so that query requests have to be queued, our original intention is to let concurrent queries go out of cache. That is, the granularity of synchronized is too large now.
Reduce the granularity of synchronized?
The above code, when caching data, so that the query cache requests do not have to queue, reducing the granularity of synchronization. However, the problem of cache breakdown is still not solved.
Although multiple requests for querying DB are queued, even if one DB query request is completed and set to cache, other requests for querying DB will continue to query DB!
Synchronized+ double check mechanism
Through the double check mechanism of synchronized+:
In the synchronization block, continue to judge and check to ensure that it does not exist before checking the DB.
Code abstraction
Found no, in fact, we deal with the cached code, except for the specific query DB logic, the other is templated. Next, let's abstract!
An API for querying DB:
Since querying a specific DB is determined by the business, exposing this interface allows the business to implement it.
A template:
Doesn't Spring have a lot of Template classes? We can also use this idea to abstract the code, let the outside world decide the specific business implementation, and write the template steps. (somewhat similar to the concept of AOP)
The improved code:
From this, we can see that we don't care where the cached data is loaded, but give it to the specific user, and the user no longer has to pay attention to the cache breakdown when using it, because we are all abstracted.
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.