This article has participated in the third “topic writing” track of the Denver Creators Training Camp. For details, check out: Digg Project | Creators Training Camp third is ongoing, “write” to make a personal impact.

Redis is now one of the most commonly used tools to help us build systems for high availability and high performance. And we all know that Redis is a completely memory based tool, which is one of the reasons why Redis is fast. When we constantly cache data into Redis, its memory is always full (and memory is very expensive, try not to use it), so we try to use useful data, Or cache frequently used data in Redis for maximum use.

So if the redis memory is used up, how should we choose between the existing data in Redis and the data to be stored, how should we deal with it?

Redis officially offers eight different elimination strategies

Redis. Conf is a nice thing. Almost all the configuration of Redis can be found here, and you can follow the instructions in the conf

Redis.conf (redis 4.0.9 for this article)

volatile-lru -> Evict using approximated LRU among the keys with an expire set.
allkeys-lru -> Evict any key using approximated LRU.
volatile-lfu -> Evict using approximated LFU among the keys with an expire set.
allkeys-lfu -> Evict any key using approximated LFU.
volatile-random -> Remove a random key among the ones with an expire set.
allkeys-random -> Remove a random key, any key.
volatile-ttl -> Remove the key with the nearest expire time (minor TTL)
noeviction -> Don't evict anything, just return an error on write operations.

# LRU means Least Recently Used
# LFU means Least Frequently Used
# The default is:
maxmemory-policy noeviction
Copy the code

Maxmemory-policy Noeviction is a iction policy, which is designed to be designed at any time according to our business needs.

If you don’t understand English, read the following Chinese

Eight elimination strategies

  • Volatile – lRU: Eliminates the least used data from the data set to expire.
  • Allkeys-lru: Eliminate the least used data from all data.
  • Volatile – lFU: Excludes the least frequently used data from the data whose expiration time is set.
  • Allkeys-lfu: Eliminate the least frequently used data from all data.
  • Volatile -random: To eliminate random data from data with expiration time.
  • Allkeys-random: Randomly eliminate data from all data.
  • Volatile – TTL: Eliminates the earliest expired data.
  • Noeviction: default policy, data will be excluded when data is added or modified, but read operations will be designed correctly

The above is out of memory strategy, there is a deletion strategy is expired key, the two are different, do not confuse

Deletion policy for expired keys

  • Timed expiration: You need to create a timer for each key that is set to expire. The timer is cleared immediately when the key expires. This policy can immediately clear expired data and is memory friendly; However, it takes up a lot of CPU resources to process expired data, which affects the response time and throughput of the cache.

  • Lazy expiration: Only when a key is accessed, the system determines whether the key has expired, and the expired key is cleared. This strategy maximizes CPU savings but is very memory unfriendly. In extreme cases, a large number of expired keys may not be accessed again and thus will not be cleared, occupying a large amount of memory.

  • Periodic expiration: At regular intervals, a certain number of keys in the expires dictionary of a certain number of databases are scanned and the expired keys are cleared. This strategy is a compromise between the first two. By adjusting the interval of periodic scan and the time limit of each scan, you can achieve the optimal balance of CPU and memory resources in different situations.

The Expires dictionary holds expiration data for all keys with an expiration date set, where key is a pointer to a key in the key space and value is the expiration date represented by the millisecond precision UNIX timestamp for that key. The key space refers to all the keys stored in the Redis cluster.Copy the code

conclusion

The selection of Redis memory flushing strategy does not affect the processing of expired keys. The memory flushing policy is used to process the data that needs to apply for extra space when the memory is insufficient. Expiration policies are used to process expired cached data.