Multi-level cache idea (for gpt programming)

foreword


I have seen the multi-level cache implementation of Youzan TMC framework before, and wrote an article: Refer to the principle of Youzan TMC framework to implement multi-level cache simply . I still haven’t learned some of the essence of it. Today, I will learn more about it with the help of Ali’s open source JetCache.

What is multi-level cache


Please tell me about gpt~ This development configuration is a good assistant, it will filter the content more carefully, unlike a certain degree engine that directly throws out a bunch of blogs, many of them are still ambiguous, and the key is advertisements, haha~

image.png

Its analysis is very detailed. The speed of the memory type is very fast, but it cannot share resources. The speed of the distributed cache is slower than the previous one, but it can guarantee the data consistency of the distributed environment. Persistence is generally the relationship type of mysql, and its characteristic is capacity. Large, but the disadvantage is that the performance will drop when the concurrency exceeds 200.

Multi-level cache application scenarios

We generally use distributed cache redis in our applications. What are its shortcomings? It is also vulnerable. Common interview questions: cache breakdown, cache penetration, these scenarios will also crash the system, and hot keys will also A huge pressure is placed on specific nodes, which affects redis read performance, and thus multi-level caching comes into play.

Multi-level cache idea


Read and write logic

data storage logic

MultiLevelCache

image.png

image.png

The value is inserted in a loop. The top layer is the local cache, and then the distributed cache. Between them is CompletableFuture, which is regulated by thenCombine. For example, if the local cache is inconsistent with the distributed cache, an exception message will be returned.

image.png

data read logic

image.png

The order is still read from local to distributed.

checkResultAndFillUpperCache(key, i, holder);
复制代码

This code is to forcibly refresh the TTL inside, which is to ensure that the internal cache is consistent within the ttl time.

consistency guarantee

The consistency here has several aspects: multi-level cache consistency, consistency of different nodes

How does multi-level caching ensure consistency? Come to gpt students to answer

image.png

What is the read-write update strategy? The bypass update strategy is the same as redis, that is, when get is obtained, if there is no multi-level cache, then the updated multi-level cache after reading from db solves the first problem: multi-level cache consistency.

The second problem: the consistency of different nodes is actually solved by redis publish and subscribe, that is, the cache key put and remove operations will be notified through the redis pipeline, and then different nodes will be synchronized. What to do if there is a gateway delay, try again~

cache breakdown


Come to gpt students to answer the question again

image.png

I have to say that artificial intelligence is really smart, it lists a lot of points, and then we artificially filter out what we want.

The first point in it mentioned that distributed locks will be used to avoid cache breakdown. What does it mean? For example, if there are 2 nodes and 1w concurrency, then there is no data in the multi-level cache. At this time, node 1 will get the distributed lock update Data, other requests are stuck first, and after the update is completed, node 2 will update its own data, so even if the concurrency is high, in fact, only 2 requests will hit the database.

How to wait for other requests? Refer to Fence CountDownload .

How to monitor hotspot keys


gpt students:

image.png

public class HotKeyMonitor<K> implements CacheMonitor<K, Integer> {  
  
    private static final Logger logger = LoggerFactory.getLogger(HotKeyMonitor.class);  
  
    private final Map<K, AtomicInteger> counterMap = new ConcurrentHashMap<>();  
  
    private final Set<K> hotKeySet = new HashSet<>();  
  
    private final int threshold;  
  
    public HotKeyMonitor(int threshold) {  
        this.threshold = threshold;  
    }  
  
    @Override  
    public void onGet(String cacheName, K key, Integer value) {  
        if (value == null) {  
            return;  
        }  
        AtomicInteger counter = counterMap.computeIfAbsent(key, k -> new AtomicInteger());  
        if (counter.incrementAndGet() >= threshold) {  
            synchronized (hotKeySet) {  
                if (!hotKeySet.contains(key)) {  
                    hotKeySet.add(key);  
                    ![]()logger.info("Hot key detected, cache: {}, key: {}, count: {}", cacheName, key, counter.get());  
                }  
            }  
        }  
    }  
  
    @Override  
    public void onPut(String cacheName, K key, Integer value) {  
        counterMap.remove(key);  
        synchronized (hotKeySet) {  
            hotKeySet.remove(key);  
        }  
    }  
  
    public Set<K> getHotKeySet() {  
        synchronized (hotKeySet) {  
            return new HashSet<>(hotKeySet);  
        }  
    }  
  
}  
复制代码

image.png

How is this value updated?

image.png

Will this monitoring be synchronized in the cluster?

image.png

In what ways does it monitor it?

image.png

The onGet request will be accumulated, and the onPut will delete the hot key through the event asynchronously

After the hotspot key is monitored, it should actually be placed in the local cache to hit it, which is the most efficient and avoids redis network requests, especially the big key problem .

Summarize


Here we start from the concept of multi-level cache to why we use multi-level cache, and then from its read and write logic, consistency guarantee, cache breakdown, hot key monitoring, we have a comprehensive understanding of multi-level cache I understand, thanks to chatgpt's answer, and I also read the corresponding source code, I hope it will be helpful to readers~

This article is participating in the Artificial Intelligence Creator Support Program

Guess you like

Origin juejin.im/post/7215452609103855676