Redis memory elimination mechanism

Redis memory elimination refers to the fact that some keys stored by users can be actively deleted from the instance by Redis, resulting in read misses. So why does Redis have this function? This is the original intention of the design we need to explore. The two most common application scenarios of Redis are caching and persistent storage. The first question to be clear is which scenario is the memory elimination strategy more suitable for? Is it persistent storage or cache?

The original intention of the memory elimination mechanism is to make better use of memory, and use a certain cache miss in exchange for the efficiency of memory usage.



As a Redis user, how can I use this feature provided by Redis? Take a look at the following configuration



# maxmemory <bytes>



We can enable the memory elimination function by configuring the value of maxmemory in redis.conf. As for the meaning of this value, we can understand its meaning by understanding the process of memory elimination:



1. The client initiates a command (such as set) that needs to request more memory.



2. Redis checks the memory usage. If the used memory is greater than maxmemory, it starts to eliminate the memory (key) according to the different elimination strategies configured by the user, in exchange for a certain amount of memory.



3. If there is no problem with the above, the command is executed successfully.



When maxmemory is 0, it means that we have no limit on the memory usage of Redis.



Redis provides the following elimination strategies for users to choose from. The default strategy is noeviction strategy:



· noeviction: When the memory usage reaches the threshold, all commands that cause memory application will report an error.



allkeys-lru: In the primary key space, remove keys that have not been used recently.



volatile-lru: In the key space with the expiration time set, the key that has not been used recently is preferentially removed.



allkeys-random: Randomly remove a key in the primary key space.



volatile-random: Randomly remove a key in the key space with the expiration time set.



volatile-ttl: In a key space with an expiration time set, keys with an earlier expiration time are removed first.



Here, add the primary key space and the key space with the expiration time set. For example, if we have a batch of keys stored in Redis, there is a hash table used to store the batch of keys and their values. If the batch of keys is in If a part of the expiration time is set, then the batch of keys will also be stored in another hash table, and the value in this hash table corresponds to the expiration time of the key set. A keyspace with an expiration time set is a subset of the primary keyspace.



We have learned that Redis probably provides such several elimination strategies, so how to choose? The choice of eviction policy can be specified by the following configuration:



# maxmemory-policy noeviction



But what is this value filled in? To solve this problem, we need to understand how our application requests access the dataset stored in Redis and what our demands are. At the same time, Redis also supports Runtime to modify the elimination strategy, which allows us to adjust the memory elimination strategy in real time without restarting the Redis instance.



Let's take a look at the applicable scenarios of several strategies:



· allkeys-lru: If our application's access to the cache conforms to a power-law distribution (that is, there is relatively hot data), or we do not know the distribution of our application's cache access, we The allkeys-lru strategy can be selected.



allkeys-random: This strategy can be used if our application has an equal probability of accessing the cached keys.



volatile-ttl: This strategy allows us to hint to Redis which keys are more suitable for eviction.



In addition, the volatile-lru strategy and volatile-random strategy are suitable when we use a Redis instance for both caching and persistent storage, but we can also achieve the same effect by using two Redis instances, it is worth mentioning The point is that setting the key expiration time will actually consume more memory, so we recommend using the allkeys-lru strategy to use memory more efficiently.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326332057&siteId=291194637