Redis memory management

Key configuration options

maxmemory

  • Maximum memory that can be used by a redis instance. By default replicas ignore this setting until they are promoted. This can be changed via replica-ignore-maxmemory setting.
  • Memory needs to be set aside if there are replicas. On GCP Cloud Memorystore it is also important to leave some memory aside for the OS as well otherwise you might OOM.

maxmemory-samples

  • Redis will as many samples defined by maxmemory-samples (default is five) and pick the one that was used least recently

maxmemory-policies

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

active-expire-effort

  • Value between 1 to 10
  • The default effort of the expire cycle will try to avoid having more than ten percent of expired keys still in memory, and will try to avoid consuming more than 25% of total memory and to add latency to the system.

Reference

How key expiry happens?

active key expiry

Specifically this is what Redis does 10 times per second:

  • Test 20 random keys from the set of keys with an associated expire.

  • Delete all the keys found expired.

  • If more than 25% of keys were expired, start again from step 1.

  • Executed as part of activeExpireCycle. Runs on an internal timer-cron.

  • The amount of keys to test randomly (20 by default) is determined by ACTIVE_EXPIRE_CYCLE_LOOKUPS_PER_LOOP

  • active-expire-effort is a configuration option that can influence how aggressively this process is carried out - tradeoff latency vs amount of expired keys.

passive key expiry

When the key is read the TTL will be checked and if it is expired throw it away and return nothing. E.g. scanning keys will remove expired keys because of this.

Reference