How to deal with cache expiration and memory occupied by cache in Redis
This article mainly explains the "Redis cache expiration, memory is occupied by the cache how to deal with", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "Redis cache expiration, memory is occupied by the cache how to deal with" it!
What about expired key?
The key cache with expire set has expired, but the server's memory will still be occupied because of the two deletion strategies on which redis is based.
Redis has two strategies:
(active) regular deletion
Check the expired key regularly and randomly, and clean up and delete if it expires. (hz configuration with number of checks per second in redis.conf)
(passive) lazy deletion
When the client requests a key that has expired, redis checks whether the key expires, deletes it, and returns a nil if it expires. This strategy is more cpu-friendly and won't cost too much, but will have a high memory footprint.
So, although key is out of date, memory will actually be occupied as long as it is not cleaned up by redis.
So what if the memory is slowly occupied by the Redis cache?
Memory is full, so you can use the hard drive to save it, but it doesn't make sense, because the hard disk is not as fast as memory, which will affect the performance of redis.
So, when memory is full, redis provides a cache elimination mechanism: MEMORY MANAGEMENT
Maxmemory: when the memory utilization has reached, start cleaning the cache
* noeviction: old cache never expires, new cache cannot be set, error is returned * allkeys-lru: clear the least used old cache, and then save the new cache (recommended) * allkeys-random: randomly delete all caches (not recommended) * volatile-lru: in those caches with expire expiration time set, clear the least used old cache Then save the new cache * volatile-random: in those caches that set the expire expiration time, delete the cache randomly * volatile-ttl: in those caches that set the expire expiration time, delete the ones that are about to expire. Thank you for reading. The above is the content of "how to deal with cache expiration and memory occupied by cache in Redis" after the study of this article. I believe you have a deeper understanding of how to deal with the problem of cache expiration and memory being occupied by cache in Redis, and the specific usage needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!