Redis document translation _ LRU cache, redis document _ lru
Using Redis as an LRU cache uses Redis as the LRU cache
Source: http://blog.csdn.net/column/details/redisbanli.html
When Redis is used as a cache, sometimes it is handy to let it automatically evict old data as you add new one. this behavior is very well known in the community of developers, since it is the default behavior of the popular memcached system.
When Redis is used as the cache, Redis needs to automatically delete old data to add new data to the cache. In the community, the well-known memcached system has this default practice.
LRU is actually only one of the supported eviction methods. This page covers the more general topic of the Redismaxmemory
Directive that is used in order to limit the memory usage to a fixed amount, and it also covers in depth the LRU algorithm used by Redis, that is actually an approximation of the exact LRU.
LRU is actually the only method that Redis supports eviction. This page includes the maxmemory command of Redis, which is used to limit the maximum physical memory used by Redis. It also contains the Redis depth LRU algorithm, which is actually an approximate LRU algorithm.
Maxmemory configuration directive maximum memory configuration commandmaxmemory
Configuration directive is used in order to configure Redis to use a specified amount of memory for the data set. It is possible to set the configuration directive usingredis.conf
File, or later using the CONFIG SETcommand at runtime.
maxmemory
The command is used to configure the maximum memory usage of the specified Redis. You can configure it in the redis. conf file and use the config set command during running.
For example in order to configure a memory limit of 100 megabytes, the following directive can be used insideredis.conf
File.
For example, to set the memory limit to 100 MB, you can configure it in the redis. conf file:
maxmemory 100mb
Setting
maxmemory
To zero results into no memory limits. This is the default behavior for 64 bit systems, while 32 bit systems use an implicit memory limit of 3 GB.
Set
maxmemory
If the value is 0, there is no memory limit. This is the default practice for 64-bit systems. 32-bit systems are limited to 3 GB.
When the specified amount of memory is reached, it is possible to select among different behaviors, called
Policies. Redis can just return errors for commands that cocould result in more memory being used, or it can evict some old data in order to return back to the specified limit every time new data is added. when the maximum number of memory is reached, Redis can choose a different method, called
Policies ).
When new data is added, only one error can be returned, or some old data can be evicted so that new data can be added.
Eviction policies Eviction policy The exact behavior Redis follows when
maxmemory
Limit is reached is configured using
maxmemory-policy
Configuration directive.
When the Redis memory reaches the upper limit, Redis uses the maxmemory-policy command to configure the policy execution.
The following indexes ies are available: The following are variables that can be configured by different ies.
- Noeviction: return errors when the memory limit was reached and the client is trying to execute commands that cocould result in more memory to be used (most write commands, but DEL and a few more than tions ).
- Noeviction:When the maximum memory usage limit is reached and the client tries to execute a command that uses more memory, an error is returned.
- Allkeys-lru: evict keys trying to remove the less recently used (LRU) keys first, in order to make space for the new data added.
- Allkeys-lru:To use memory for newly added data, the least recently used key is evicted.
- Volatile-lru: evict keys trying to remove the less recently used (LRU) keys first, but only among keys that have anexpire set, in order to make space for the new data added.
- Volatile-lru: To use memory for newly added data, keys with the expiration time setting and the least recent usage will be evicted.
- Allkeys-random: evict random keys in order to make space for the new data added.
- Allkeys-random: The key is randomly evicted to use memory for newly added data.
- Volatile-random: evict random keys in order to make space for the new data added, but only evict keys with anexpire set.
- Volatile-random: To use the memory for newly added data, the keys with expiration time settings are evicted randomly.
- Volatile-ttl: In order to make space for the new data, evict only keys with an expire set, and try to evict keys with a shorter time to live (TTL) first.
- To use memory for newly added data, keys with the minimum expiration time and TTL are evicted.
The specified ies volatile-lru, volatile-random and volatile-ttl behave like noeviction if there are no keys to evict matching the prerequisites.
Volatile-lru,
Volatile-randomAnd
Volatile-ttlAnd
NoevictionIs the same
.
To pick the right eviction policy is important depending on the access pattern of your application, however you can reconfigure the policy at runtime while the application is running, and monitor the number of cache misses and hits using the Redis INFO output in order to tune your setup.
It is very important to rely on your application to select the correct eviction policy. However, when Redis is running, you can reconfigure the eviction policy, you can use the INFO command to output memory usage and reconfigure it.
In general as a rule of thumb: general configuration rules
- Use the allkeys-lru policy when you have CT a power-law distribution in the popularity of your requests, that is, you have CT that a subset of elements will be accessed far more often than the rest. this is a good pick if you are unsure.
With allkeys-lru, when you want your request to be a power-law distribution, so that the elements that you expect to resident memory are subsets of accessible elements. If you are not sure, using allkeys-lru is a good choice.
- Use the allkeys-random if you have a cyclic access where all the keys are scanned continuously, or when you need CT the distribution to be uniform (all elements likely accesessed with the same probability ).
If all keys are accessed cyclically or when the eviction distribution is inconsistent (the probability of all elements being accessed is the same), allkeys-random is used.
- Use the volatile-ttl if you want to be able to provide hints to Redis about what are good candidate for expiration by using different TTL values when you create your cache objects.
When creating an object cache, if you want to provide a good eviction candidate by using the TTL value, useVolatile-ttl.
TheAllkeys-lruAndVolatile-randomPolicies are mainly useful when you want to use a single instance for both caching and to have a set of persistent keys. however it is usually a better idea to run two Redis instances to solve such a problem.
When you use an instance as the cache and the key is permanent, use allkeys-lru and
Volatile-random.However, there are still better practices. We can use two Redis instances to solve the problem of insufficient memory.
It is also worth to note that setting an expire to a key costs memory, so using a policy likeAllkeys-lruIs more memory efficient since there is no need to set an expire for the key to be evicted under memory pressure.
In addition, you must set the expiration time to consume memory.
Allkeys-lruThe policy is more memory efficient, because it does not need to add additional memory pressure to the eviction of expired keys.
How the eviction process works is working like this
It is important to understand that the eviction process works like this:
Understand how eviction works, like this:
- A client runs a new command, resulting in more data added.
When a client runs a new command, more data is added.
- Redis checks the memory usage, and if it is greater than
maxmemory
Limit, it evicts keys according to the policy.
Redis detects how memory usage exceeds
maxmemory
The key is evicted according to the policy.
- A new command is executed, and so forth.
The new command is executed.
So we continuously cross the boundaries of the memory limit, by going over it, and then by evicting keys to return back under the limits.
Therefore, we continue to add data to reach the memory limit. Through the above steps, some keys are evicted to bring the memory back to the limit.
If a command results in a lot of memory being used (like a big set intersection stored into a new key) for some time the memory limit can be surpassed by a noticeable amount.
If the result of the command is that a large memory is used (for example, a new key is a large set), it is obvious that the memory limit is reached in many cases.
Approximated LRU algorithm is similar to the LRU algorithm Redis LRU algorithm is not an exact implementation. this means that Redis is not able to pick the best candidate for eviction, that is, the access that was accessed the most in the past. instead it will try to run an approximation of the LRU algorithm, by sampling a small number of keys, and evicting the one that is the best (with the oldest access time) among the sampled keys.
Redis's LRU algorithm is not accurate. That is to say, Redis did not select the best candidate for eviction, that is, it did not select the last longest access from the present. Instead, it executes an algorithm similar to LRU by sampling a small number of keys, and evict the last accessed key (the oldest access time) from the sampling ).
However since Redis 3.0 (that is currently in beta) the algorithm was improved to also take a pool of good candidates for eviction. this improved the performance of the algorithm, making it able to approximate more closely the behavior of a real LRU algorithm.
In Redis 3.0 (the current test version), the algorithm has been improved and an exclusive candidate pool is used. The performance of the algorithm is improved to make it more similar to the real LRU algorithm.
What is important about the Redis LRU algorithm is that you are able to tune the precision of the algorithm by changing the number of samples to check for every eviction. this parameter is controlled by the following configuration directive:
In the algorithm, you can adjust the number of samples for eviction detection. The configuration parameters are:
maxmemory-samples 5
The reason why Redis does not use a true LRU implementation is because it costs more memory. however the approximation is always Ally equivalent for the application using Redis. the following is a graphical comparison of how the LRU approximation used by Redis compares with true LRU.
Redis does not actually implement LRU because it consumes more memory. However, Redis applications are actually equivalent. The following is a comparison between the Redis LRU algorithm and the real LRU algorithm:
The test to generate the above graphs filled a Redis server with a given number of keys. the keys were accessed from the first to the last, so that the first keys are the best candidates for eviction using an LRU algorithm. later more 50% of keys are added, in order to force half of the old keys to be evicted.
Generate the preceding chart based on the configured quantity key. If the key is accessed from the first row to the last row, the first key is the best eviction candidate in the LUR algorithm. Then, if 50% of the keys are added, half of the old keys are evicted.
You can see three kind of dots in the graphs, forming three distinct bands.
You can see three obvious differences:
- The light gray band are objects that were evicted.
The light gray band is the object to be evicted.
- The gray band are objects that were not evicted.
The gray belt is an object not evicted.
- The green band are objects that were added.
Green band is the added object. In a theoretical LRU implementation we recommend CT that, among the old keys, the first half will be expired. The Redis LRU algorithm will instead only probabilistically expire the older keys.
The theoretical Implementation of LRU is that the first half of all old keys are evicted. Redis uses an almost expired key to be evicted.
As you can see Redis 3.0 does a better job with 5 samples compared to Redis 2.8, however most objects that are among the latest accessed are still retained by Redis 2.8. using a sample size of 10 in Redis 3.0 the approximation is very close to the theoretical performance of Redis 3.0.
As you can see, 3.0 works better than 2.8, but in 2.8, most of the latest access objects are still retained. When the sample is 10 in 3.0, the performance is very close to the theoretical LRU algorithm.
Note that LRU is just a model to predict how likely a given key will be accessed in the future. moreover, if your data access pattern closely resembles the power law, most of the accesses will be in the set of keys that the LRU approximated algorithm will be able to handle well.
Note: LRU is only a prediction model, and the given key is likely to be accessed in the future. In addition, if your data access mode is similar to a power law (linear), most keys may be accessed, then the LRU algorithm is very good.
In simulations we found that using a power law access pattern, the difference between true LRU and Redis approximation were minimal or non-existent.
In practice, we found that the power law (linear) access mode is very small or there is no difference between the real LRU algorithm and the Redis LRU algorithm.
However you can raise the sample size to 10 at the cost of some additional CPU usage in order to closely approximate true LRU, and check if this makes a difference in your cache misses rate.
You can increase the sample size to 10, which is close to the real LRU algorithm and has different Miss rates, but consumes more CPU.
To experiment in production with different values for the sample size by usingCONFIG SET maxmemory-samples <count>
Command, is very simple.
It is very easy to debug with different sample sizes. Use the config set maxmemory-samples <count> command.
Source: http://blog.csdn.net/column/details/redisbanli.html