Memory limit

Redis maximum memory limit can be changed by configuring maxMemory:

127.0.0.1:6379> config get maxMemory 1) "maxMemory" 2) "1073741824" // Set the maximum memory limit 127.0.0.1:6379> config set maxmemory 2048mb OKCopy the code

Memory flushing strategy

When the Redis memory usage reaches the maximum memory limit, a memory flushing policy is triggered.

Redis currently supports the following elimination strategies:

  • Noeviction: Commands that use more memory (most write commands) will return errors and memory will not be reclaimed.
  • Allkeys-lru: Reclaim the least recently used key.
  • Volatile – lRU: Reclaims the least recently used key with an expiration date.
  • Allkeys-random: randomly reclaims keys.
  • Volatile -random: Randomly reclaims keys with expiration time.
  • Volatile – TTL: Collects keys with expired time in ascending order by expiration time.
  • Allkeys-lfu: Reclaim the least frequently used key.
  • Volatile – lFU: Reclaims the least frequently used key with an expiration date.

Allkeys-lfu and volatile- LFT are only supported in Redis 4.0.

Volatile – LRU, volatile- Random, volatile- TTL, volatile- LFU perform like noeviction if no key with expiration time exists.

For the difference between LRU (Least Recently Used) and LFU (Least Frequently Used), see LRU and LFU

How does the recycling process work

  • The client sent a command to add data. Procedure
  • Redis checks memory usage and if the memory limit is exceeded, it is reclaimed according to the policy.

Obtain and configure a reclamation policy

Get the current policy:

127.0.0.1:6379> config get maxmemory-policy
1) "maxmemory-policy"
2) "noeviction"
Copy the code

Setting policy:

127.0.0.1:6379> config set maxmemory-policy volatile-lru
OK
Copy the code

Reference: redis. IO/switchable viewer/lru -…