Home > Database > Redis > body text

A brief discussion on how to deal with cache expiration and memory occupied by cache in Redis?

青灯夜游
Release: 2021-11-26 19:32:23
forward
1708 people have browsed it

A brief discussion on how to deal with cache expiration and memory occupied by cache in Redis? The following article will take you through the cache expiration processing strategy and memory elimination mechanism in Redis. I hope it will be helpful to you!

A brief discussion on how to deal with cache expiration and memory occupied by cache in Redis?

#How to deal with expired keys?

The expired key cache has expired, but the server's memory will still be occupied. This is because of the two deletion strategies that redis is based on.
Redis has two strategies:

  • (Active) Regular deletion

    • Check expired keys regularly and randomly, and clean and delete them if they expire. (The number of checks per second is configured in hz in redis.conf)
  • (Passive) Lazy deletion

    • When the client requests an expired When the key is entered, redis will check whether the key has expired. If it has expired, it will be deleted and then return a nil. This strategy is more friendly to the CPU and will not cause too much loss, but the memory usage will be relatively high.

So, although the key has expired, as long as it is not cleared by redis, the memory will still be occupied. [Related recommendations: Redis Video Tutorial]

So what should I do if the memory is occupied by the Redis cache and is slow?

When the memory is full, you can use the hard disk to save it, but it is meaningless because the hard disk is not as fast as the memory and will affect the performance of redis.
So, when the memory is full, redis provides a cache elimination mechanism: MEMORY MANAGEMENT

maxmemory: When the memory usage rate reaches, the cache starts to be cleaned

* noeviction:旧缓存永不过期,新缓存设置不了,返回错误
* allkeys-lru:清除最少用的旧缓存,然后保存新的缓存(推荐使用)
* allkeys-random:在所有的缓存中随机删除(不推荐)
* volatile-lru:在那些设置了expire过期时间的缓存中,清除最少用的旧缓存,然后保存新的缓存
* volatile-random:在那些设置了expire过期时间的缓存中,随机删除缓存
* volatile-ttl:在那些设置了expire过期时间的缓存中,删除即将过期的`
Copy after login

For more programming-related knowledge, please visit: Introduction to Programming! !

The above is the detailed content of A brief discussion on how to deal with cache expiration and memory occupied by cache in Redis?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:juejin.cn
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 [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!