This is the 31st day of my participation in the August More Text Challenge

Memory recovery

Redis is a completely based on the middleware memory operations, and the size of the memory is precious and limited, in order to avoid memory saturated and affecting the normal operation of the system, so we need to recycle the memory when necessary, Redis provides two ways of memory recovery, expired keys delete strategies respectively, the memory release strategy.

Redis Expiration key deletion policy

When we add a key/value pair to Redis, we can specify an expiration time. After setting the expiration time, how does Redis handle the expiration key? Redis processes expired keys through periodic deletion, scheduled deletion, and lazy deletion.

Periodically delete

Periodic Deletion Deletes expired keys once in a while.

Time to delete

When the expiration time of a key is set, a timer is created to delete the key when the expiration time specified by the key is reached.

Lazy to delete

Lazy delete does not delete the key when the specified expiration time is reached. Instead, every time a key is acquired, the system determines whether the key is expired. If the key is expired, the system deletes it and returns null.

Time to delete

The advantage of scheduled deletion is that it is memory friendly. Through the timer, when the key in Redis reaches the specified expiration time, it will be deleted, directly freeing the memory. The disadvantage is that it is not CPU friendly, because if there are a lot of expired keys in Redis, removing them can take up a considerable amount of CPU time.

Lazy to delete

Inert delete not to delete when reaches the specified expiration time, but in every time get keys, will determine whether the key expired, under this strategy will there is a problem: when cold in Redis data (i.e., the key to not be access to), then the key has been around in memory will not be deleted, the memory is not friendly. However, the lazy delete policy is CPU-time-friendly because keys are only expired when they are retrieved, deletion is limited to the currently acquired key, and the policy does not spend any CPU time on removing other irrelevant expired keys.

Periodically delete

Periodically delete delete can be said to be inert and timing remove a compromise strategy, deletion policy on a regular basis every once in a while to perform a delete expired key operation, and by limiting the delete operation to reduce the length and frequency of the delete operation to the influence of the CPU time, and by regularly delete expired keys, effectively reduce the expired keys of wasted memory. However, a “dilemma” exists in the periodic deletion policy. When the specified deletion duration is specified, too much CPU time will be occupied. If the specified deletion frequency is too long, expired keys in Redis cannot be deleted in time, wasting memory.

🚦 After the above analysis of the three expired key deletion strategies, we know the advantages and disadvantages of the three deletion strategies, so which deletion strategy is Redis using? The answer is a combination of periodic deletion and lazy deletion. By combining these two strategies, Redis can strike a balance between using CPU time wisely and avoiding wasting memory space.

Memory flushing strategy

Memory flushing is another way to avoid memory saturation and affect the normal operation of the system. When the memory reaches the maxmemory limit, the specified policy is used to clean up the memory to ensure the storage of new key pairs. Redis provides eight memory flushing policies, which can be seen in the Redis configuration file redis.conf as follows:

Maxmemory <bytes> # maxmemory POLICY how Redis will select what to remove when maxmemory # is reached. You can select one from the following behaviors: # # volatile-lru -> Evict using approximated LRU, only keys with an expire set. # allkeys-lru -> Evict any key using approximated LRU. # volatile-lfu -> Evict using approximated LFU, only keys with an expire set. # allkeys-lfu -> Evict any key using approximated LFU. # volatile-random -> Remove a random key having 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 # # Both LRU, LFU and volatile- TTL are implemented using static algorithms. # # Note: with any of the above policies, when there are no suitable keys for # eviction, Redis will return an error on write operations that require # more memory. These are usually commands that create new keys, add data or # modify existing keys. A few examples are: SET, INCR, HSET, LPUSH, SUNIONSTORE, # SORT (due to the STORE argument), and EXEC (if the transaction includes any # command that requires memory). # # The default is: The default memory exclusion policy is noeviction # maxmemory-policy noevictionCopy the code
  1. volatile-lru: When memory is insufficient to hold new key-value pairs, the key with expiration time is discardedLeast recently usedThe key of
  2. allkeys-lru: Selects from all keys when memory is insufficient to hold new key-value pairsLeast recently usedKey and eliminate
  3. volatile-lfu: When memory is insufficient to hold new key-value pairs, the key with expiration time is discardedMinimum frequency of useThe key of
  4. allkeys-lfu: Selects from all keys when memory is insufficient to hold new key-value pairsMinimum frequency of useKey and eliminate
  5. volatile-random: Randomly discards keys with expiration time when memory is insufficient to accommodate new key-value pairs
  6. allkeys-random: Randomly discards keys with expiration time when memory is insufficient to accommodate new key-value pairs
  7. volatile-ttl: When memory is insufficient to accommodate new key-value pairs, some keys are randomly selected from all the keys for elimination
  8. noeviction : The default policy, data will not be discarded. New or modified data will throw an exception, but the read operation is not affected

summary

Redis to avoid memory saturated and affect the normal operation, the system will be to recovery of memory, mainly has expired keys to delete and memory elimination strategy, expired key deletion policy is aimed at expired key recovery, but memory elimination strategy in every time when Redis command to determine whether the current Redis reached the limit of memory, if met, The corresponding elimination strategy is used to process the keys to be deleted.

🏁 the above is a simple introduction to Redis expiration key deletion strategy and memory elimination strategy. If there is any error, please leave a comment and correct it. If you think this article is helpful to you, click a like 👍