Original address:
http://flychao88.iteye.com/blog/1977653
http://blog.csdn.net/cjfeii/article/details/47259519
LRU ( Least Recently used , least recently used) algorithm to retire data based on historical access records of the data, the core idea is " If the data has been accessed recently, the chances of future access are higher " .
1.2. Implementing
The most common implementation is to use a linked list to save the cached data, the detailed algorithm is implemented as follows:
1. inserting new data into the list head;
2. whenever the cache hits (that is, the cached data is accessed), the data is moved to the list header;
3. when the list is full, discard the data at the end of the list.
1.3. Analysis
Hit rate
When hot data is present, LRU is very efficient, but occasional, cyclical batch operations can result in LRU hit rate dropped sharply, the cache pollution situation is serious.
"Complexity"
Simple to implement.
Cost
A hit will need to traverse the linked list, find the hit block index, and then need to move the data to the head.
Approximate LRU algorithm
the LRU in Redis is not the implementation of the LRU algorithm in the strict sense , it is an approximate LRU Implementation, primarily to conserve memory and improve performance. Redis has such a configuration -- Maxmemory-samples ,Redis 's LRU is the number of keysto take out the configuration, and then choose from one of the most infrequently used Key for displacement, the default 5 , as follows:
Maxmemory-samples 5
can be obtained by adjusting the number of samples. LRU The speed or accuracy advantages of the permutation algorithm.
Redis do not use the real LRU The reason for this is to save memory usage. Although not a true LRU Implementation, they are almost equivalent in application. Is the comparison between the approximate LRU implementations of Redis and the theoretical LRU implementations:
Teststart First inRedisImport a certain number ofKey, and then from the firstKeyaccess to the lastKey, so according toLRUthe first access to the algorithm is aKeyShould be replaced later , then add50%Number ofKey, causing50%of the oldKeybe replaced.
in the you can see three types of points that make up three different areas:
- Pale Gray is displaced key
- Gray is not replaced by the key
- Green is the newly added key
theory lru implementation as we expected, the oldest 50% number of key was displaced, redis lru key displacement out.
You can see the number of samples as 5 In the case of redis3.0 redis2.8 do a lot of good, redis2.8 There are a lot of data that should be replaced is not replaced. In cases where the number of samples is 10 redis3.0 very close to the real LRU implementation.
LRU is a model that predicts what data we will visit in the future if we access the data in a form close to what we expected -- power law, then approximate LRU The algorithm implementation will be able to handle very well.
In the simulation test we can find that in the Power Law access mode, the theory LRU and the Redis approximate LRU the gap is small or there is no gap.
If you will maxmemory-samples set to 10 redis cpu cost to ensure close to the real lru performance, you can check the hit rate to see what the difference is.
through CONFIG SET maxmemory-samples <count> Dynamically adjust the size of the sample, do some testing to verify your conjecture.
About LRU algorithms (reprint)