Redis cache expiration processing and memory elimination mechanism

Computer memory is limited, the larger the more expensive, Redis's high concurrency and high performance are all based on memory, if you use a hard disk, GG.

How to deal with expired keys?

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

1. (Active) delete regularly

                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 friendly and will not cause too much loss, but the memory usage will be relatively high.
Therefore, although the key has expired, as long as it is not cleaned up by redis, the memory will actually be occupied.

So what if the memory is slowed down by Redis cache?

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

maxmemory: When the memory has been used, the cache will be cleared

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

 

Guess you like

Origin blog.csdn.net/pengyiccb/article/details/107118817