Redis cache expiration processing and memory elimination mechanism

1 Introduction

Computer memory is limited. The larger the computer, the more expensive it is. Redis's high concurrency and high performance are all based on memory. If the hard disk is used, the performance will be poor. To ensure fast data response, the data in the cache needs to be cleared.

2 Expired key processing

The key cache with expired expires, but the memory of the server will still be occupied. This is because of the two deletion strategies based on redis.

1 (active) regularly delete

  • Regularly and randomly check expired keys, and clean up and delete them if they expire. (The number of checks per second is configured in hz in redis.conf)

2 (passive) lazy deletion

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

So, even if the redis key expires, as long as it is not cleaned up by redis, it will still occupy memory.

3 Memory elimination mechanism

If the memory is full, you can use the disk to save the data. In this case, it will greatly reduce the response speed of the data, contrary to our original intention, and affect the performance of redis.

Therefore, when the memory is full, redis provides a cache elimination mechanism,MEMORY MANAGEMENT

maxmemory : When the memory usage rate reaches, start to clear the cache

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

4 Related information

  • The blog post is not easy, everyone who has worked so hard to pay attention and praise, thank you

Guess you like

Origin blog.csdn.net/qq_15769939/article/details/113831246