Key configuration options
- 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
- 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.
- Redis will as many samples defined by maxmemory-samples (default is five) and pick the one that was used least recently
- 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.
- 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.
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-effortis 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.
- https://redis.io/topics/latency - “Latency generated by key expires”