When redis is used as a cache (at this time, the cache is only used as hot data to improve service access performance), it is necessary to consider memory constraints and how to retain only hot data as the business grows.
All data structures of redis can set expiration time. When the time comes, redis will automatically delete the corresponding objects.
- be overdue
expireSoobjectFor example, the expiration of a hash structure is the expiration of the entire hash object, not a child key.
- If a string has an expiration time set, and you call the set method to modify it, its expiration time will disappear.
127.0.0.1:6379> set k1 aaa OK 127.0.0.1:6379> expire k1 600 (integer) 1 127.0.0.1:6379> ttl k1 (integer) 597 127.0.0.1:6379> set k1 bbb OK 127.0.0.1:6379> ttl k1 (integer) -1...
Redis Keys expire in two ways: passive and active.
When some clients try to access it, the key will be found and automatically expire.
Of course, this is not enough, because some expired keys will never access them.
In any case, these keys should expire, so regular random tests set the expiration time of the keys. All these expired keys will be deleted from the key space.
Specifically, redis does 10 times per second:
- 1. Test 20 random keys for related expiration detection.
- 2. Delete all expired keys.
- 3. If more than 25% of the Keys expire, repeat step 1
This is an ordinary probability algorithm. The basic assumption is that our sample is the key control,
And we keep repeating expiration detection until the percentage of expired keys is less than 25%, which means that at any given time, up to 1 / 4 of expired keys will be cleared.
CONFIGCommand to configure redis configuration items:
maxmemory 100mb Maxmemory policy [policy]
LRU-I haven’t touched it lately
Give priority to the elimination of recently used (all or expired) keys
allkeys-lruTry to recycle the least used key (LRU) to make room for the newly added data.
volatile-lruTry to recycle the least used key (LRU), but only the keys in the expired collection, so that the newly added data has space to store.
LFU-Not many times
Give priority to the keys that are used the least frequently (all or have expiration time)
allkeys-lfuTry to recycle the least frequently used key (LFU) to make room for newly added data.
volatile-lfuTry to recycle the least frequently used key (LFU), but only the keys in the expired set, so that the newly added data has space to store.
volatile-ttlThe smallest part of TTL in the key with expiration time is eliminated first
All keys random: Reclaim random keys to make room for newly added data.
Volatile random: Reclaim random keys to make room for newly added data, but only for keys in expired collections.
Approximate LRU algorithm
Redis’s LRU algorithm is not a complete implementation. This means that redis cannot select the best candidate for recycling, that is, the key that has not been accessed for the longest time.
Instead, it will try to run an algorithm similar to LRU, sample a small number of keys, and then recycle one of the best keys (those accessed earlier).
Redis LRU has a very important point. You can adjust the sampling quantity checked during each recovery to achieve the accuracy of the adjustment algorithm. This parameter can be adjusted through the following configuration instructions: