preface
Cache (memory or Memcached or Redis…..) Widely used in Internet projects, this blog will discuss the topic of cache breakdowns, covering the phenomenon of cache breakdowns, solutions, and code abstraction to handle cache breakdowns.
What is cache breakdown?
The above code, is a typical way to write: when the query, first from the Redis cluster, if not, then from the DB query and set to the Redis cluster.
Note that in real development, we typically store the data structure in the cache as JSON. (The JDK provides slightly less efficient serialization than JSON serialization; In addition, the JDK serialization is very strict, and the addition or deletion of fields can lead to unsequence failure, while JSON is more compatible with this aspect.
If the query from DB takes 2S, then it is obvious that all requests within this period of time will go through DB query under the above code, equivalent to the cache is directly penetrated, such a phenomenon is called “cache breakdown”!
Analysis of ideas to avoid cache breakdown
Add synchronized?
If synchronized is added to a method, query requests are queued, which is intended to allow concurrent queries to exit the cache. Synchronized is now too granular.
Reduce the granularity of synchronized?
The above code reduces the granularity of synchronization by eliminating the need to queue requests to the query cache when data is cached. However, the cache breakdown problem is still not solved.
Although multiple queries to the DB are queued, even if one DB query is completed and set in the cache, other queries to the DB continue!
Synchronized + double check mechanism
Synchronized + double check:
In the synchronized block, continue the judgment check to ensure that there is no DB.
Code abstract
Found no, in fact, we deal with the cache code, in addition to the specific query DB logic, the other is template. Now let’s abstract it!
An interface to query DB:
Since it is up to the business to query the specific DB, expose the interface and let the business implement it.
A template:
Doesn’t Spring have many Template classes? We can also use this idea to abstract the code and let the outside world decide on the concrete business implementation, while the template steps are written. (Somewhat aOP-like concept)
Improved code:
From this we can see that we don’t care where the cached data is loaded from, we give it to the specific user, and the user doesn’t have to worry about the cache breakdown anymore because we abstract it.