Home > Database > Redis > Introduction to redis memory elimination mechanism

Introduction to redis memory elimination mechanism

Release: 2020-05-10 08:57:11
forward
2563 people have browsed it

Introduction to redis memory elimination mechanism

Redis memory elimination means 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 design intention that we need to explore.

The two most common application scenarios of Redis are caching and persistent storage. The first question to be clarified 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 certain cache misses in exchange for memory usage efficiency.

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

# maxmemory <bytes>
Copy after login

We can enable the memory elimination function by configuring the maxmemory value 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 initiated a command that needs to apply for more memory (such as set).

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

3. If there is no problem with the above, then this 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 the noeviction strategy:

  • noeviction: When the memory usage reaches the threshold, all causes The command to apply for memory will report an error.

  • allkeys-lru: In the primary key space, remove recently unused keys first.

  • volatile-lru: In the key space with an expiration time set, remove recently unused keys first.

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

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

  • volatile-ttl: In the key space with expiration time set, keys with earlier expiration time will be removed first.

Here we add the primary key space and the key space with expiration time set. For example, assuming we have a batch of keys stored in Redis, there is a hash table for storage. This batch of keys and their values. If some of the keys in this batch have an expiration time set, then this batch of keys will also be stored in another hash table. The value in this hash table corresponds to the expiration date of the key. time. The key space with expiration time set is a subset of the primary key space.

We understand that Redis provides roughly several elimination strategies, so how to choose? The selection of the elimination strategy can be specified through the following configuration:

# maxmemory-policy noeviction
Copy after login

But what should be filled in with this value? To solve this problem, we need to understand how our application requests access to the data set 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 are relative hot spots data), or we don’t know much about the cache access distribution of our application, we can choose the allkeys-lru strategy.

  • allkeys-random: If our application has equal access probability to cache keys, we can use this strategy.

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

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

For more redis knowledge, please pay attention to the redis introductory tutorial column.

The above is the detailed content of Introduction to redis memory elimination mechanism. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template