High-throughput and thread-safe LRU cache details

Source: Internet
Author: User

High-throughput and thread-safe LRU cache details

This article focuses on the high-throughput, thread-safe LRU cache.

A few years ago, I implemented an LRU cache for keyword search for its id. The data structure is very interesting, because the required throughput is large enough to eliminate a large amount of use.locksAndsynchronizedKeyword, the application is implemented in java.

I think that a series of atomic reference distributions will keep LRU In the LRU sequence in ConcurrentHashMap. At the beginning, I will wrap the value into the entry. The entry has a node in the LRU chain of the double-stranded table, the end of the chain is the recently used entry. The header node stores the entry that may be cleared when the cache reaches a certain size. Each node points to the entry used for searching.

When you search for a value through the key, the cache first needs to search for the map to see if the value exists. If it does not exist, it will rely on the loader to read the value from the data source in the read-through mode and add the map in the "add if missing" mode. The challenge of ensuring high throughput is to effectively maintain the LRU chain. This concurrent hash map is segmented and at a certain level of the thread (when you build a map, you can specify the concurrency level) in this case, it will not experience too much thread competition. But cannot LRU chain be divided in the same way? To solve this problem, I introduced a secondary queue to clear the operation.

There are six basic methods in the cache. For cache hit, the search contains two basic operations: get and offer. For crude loss, there are four basic methods: get, load, put, and offer. In the put method, we may need to track the clearing operation. When the cache hits get, we passively perform some clearing operations on the LRU chain.

Get: lookup entry in the map by key
Load: load value from a data source
Put: create entry and map it to key
Offer: append a node at the tail of the LRU list that refers to a recently accessed entry
Evict: remove nodes at the head of the list and associated entries from the map (after the cache reaches a certain size)
Purge: delete unused nodes in the LRU list -- we refer to these nodes as holes, and the cleanup queue keeps track of these

Both the clear operation and the clean operation process data in large batches. Let's take a look at the details of each operation.

The get operation works as follows:

get(K) -> V  lookup entry by key k if cache hit, we have an entry e   offer entry e   try purge some holes else   load value v for key k   create entry e <- (k,v)   try put entry e end return value e.v 

If the key exists, we provide a new node at the end of the LRU chain to indicate that this is a recently used value. The execution of get and offer is not an atomic operation (there is no lock here), so we cannot say that this offered node points to the recently used entity, but it must be the most recently used entity that we obtain During Concurrent execution. We didn't force the order in which get and offer pairs are executed between threads, because this may limit the throughput. After an offer node, we try to clear and return values. Next, let's take a closer look at the offer and purge operations.

If the cache is lost, we will call the loader to load the value for this key, create a new object and put it into the map. The put operation is as follows:

put(E) -> E  existing entry ex <- map.putIfAbsent(e.k, e) if absent   offer entry e;   if size reaches evict-threshold     evict some entries   end   return entry e else, we have an existing entry ex   return entry ex end 

As you can see, when two or more threads put an object into map, there may be competition, but only one successful thread can call offer. After a node is provided at the end of the LRU chain, we need to check whether the cache has reached its threshold value. The threshold value is the identifier used for batch clearing. In this specific application scenario, the threshold value must be smaller than the capacity. The clearing operation takes place in small batches instead of when every entity is added. multithreading may be involved in the clearing operation until the cache capacity reaches its capacity. Locking is easy, but the thread is safe. Clear the header nodes that need to be removed from the LRU chain. This requires careful atomic operations to avoid multi-threaded removal operations in map.

This offer operation is very interesting. It always tries to create a node but does not try to immediately remove or delete those nodes that are no longer in use in LRU.

offer(E)  if tail node doesn't refer to entry e   assign current node c <- e.n   create a new node n(e), new node refers to entry e   if atomic compare-and-set node e.n, expect c, assign n     add node n to tail of LRU list     if node c not null       set entry c.e to null, c now has a hole       add node c to cleanup queue     end   end end 

First, it checks that the nodes at the end of the chain do not point to the accessed entities, which is no different unless all threads frequently access the same key-value pair, it creates a new node for the object at the end of the chain. When the object is different, it tries to perform a comparison and setting operation for the object before providing the new node, this will prevent multithreading from doing the same thing.

The thread of the successfully allocated node provides a new node at the end of the LRU chain. This operation is the same as find in concurrent1_queue. The dependent algorithms are described in the following article: Simple, Fast, and Practical Non-Blocking and Blocking Concurrent Queue Algorithms. The thread then checks whether the object is connected to other nodes,If so, the old node will not be deleted immediately, but will be marked as a hole(Its object reference will be set to null)

Summary

The above is all the details about the high-throughput and thread-safe LRU cache in this article. I hope it will be helpful to you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.