In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly discusses the memory elimination mechanism of redis. There is a certain reference value, friends in need can refer to, follow the editor one by one to break the redis memory elimination mechanism of the doubt.
Redis memory obsolescence means that some keys stored by the user can be actively deleted by Redis from the instance, resulting in reading miss, so why should Redis have this function? This is the original intention of the design that we need to explore.
The two most common Redis application scenarios are caching and persistent storage. The first question to be clear is which scenario is the memory obsolescence strategy more appropriate? Is it persistent or cached?
The original intention of the memory elimination mechanism is to make better use of memory, using a certain cache miss in exchange for memory efficiency.
As a Redis user, how can I use this feature provided by Redis? Check out the following configuration
# maxmemory
We can enable memory obsolescence 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 obsolescence:
1. The client initiates a command that needs to request more memory (such as set).
2. Redis checks the memory usage, and if the memory used is larger than maxmemory, it begins to eliminate 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 above, the command is executed successfully.
A maxmemory of 0 means that we have no restrictions on the memory usage of Redis.
Redis provides the following elimination policies for users to choose from, of which the default policy is noeviction policy:
Noeviction: when memory usage reaches the threshold, all commands that cause requests for memory will report an error.
Allkeys-lru: in the primary key space, remove the recently unused key first.
Volatile-lru: in the key space where the expiration time is set, remove the recently unused key first.
Allkeys-random: in the primary key space, randomly remove a key.
Volatile-random: randomly removes a key in the key space where the expiration time is set.
Volatile-ttl: in the key space where the expiration time is set, the key with an earlier expiration time is removed first.
Here to add the primary key space and set the expiration time of the key space, for example, suppose we have a batch of keys stored in Redis, then there is a hash table to store these keys and their values, if part of this batch of keys set the expiration time, then this batch of keys will also be stored in another hash table, the value in this hash table corresponds to the expiration time when the key is set. The key space with the expiration time set is a subset of the primary key space.
We have learned that Redis has probably provided several elimination strategies, so how to choose? The choice of phase-out strategy can be specified through the following configuration:
# maxmemory-policy noeviction
But what does this value fill in? To solve this problem, we need to understand how our application request accesses the dataset stored in Redis and what our request is. At the same time, Redis also supports Runtime to modify the elimination policy, which allows us to adjust the memory elimination policy 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 follows a power-law distribution (that is, there is relative hot data), or if we are not quite clear about the cache access distribution of our application, we can choose the allkeys-lru policy.
Allkeys-random: if our application has equal access probability to the cached key, we can use this strategy.
Volatile-ttl: this strategy allows us to hint to Redis which key is more suitable for eviction.
In addition, volatile-lru policy and volatile-random policy are suitable when we apply a Redis instance to both cache and persistent storage, but we can also achieve the same effect by using two Redis instances. It is worth mentioning that setting the expiration time of key actually consumes more memory, so we recommend using allkeys-lru policy to use memory more efficiently.
After reading the above, do you have a general understanding of the memory elimination mechanism of redis? If you want to know more about the content of the article, welcome to follow the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.