Redis expiration time and memory management


memory management

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.


Expiration time

All data structures of redis can set expiration time. When the time comes, redis will automatically delete the corresponding objects.

  • be overdueexpireSoobjectFor 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.> set  k1 aaa
OK> expire k1 600
(integer) 1> ttl k1
(integer) 597> set k1  bbb
OK> ttl k1
(integer) -1...

Obsolete keys

Redis Keys expire in two ways: passive and active.

  • passive

When some clients try to access it, the key will be found and automatically expire.

  • active

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.

Memory obsolescence

stayredis.confOr useCONFIGCommand to configure redis configuration items:

maxmemory 100mb
Maxmemory policy [policy]

Elimination strategy:

  • 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

  • noevictionReturn error

  • 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:

maxmemory-samples 5

@SvenAugustus (

Recommended Today

Scratch3 how to realize the animation of the little girl changing clothes?

Scratch3 can make the animation of a little girl changing clothes. How to achieve this animation effect? Let’s take a look at the detailed tutorial. Software name: Micah scratch 3 (children’s programming software) v1.0.2 Chinese free green version Software size: 59.1MB Update time: 2020-09-05Download now First, delete the kitten. The protagonist here is a girl […]