Is maxmemory the Maximum Value of Used Memory?

Last updated 18, Apr 2024

Question

When configured, is maxmemory the maximum amount of memory that Redis can use?

Answer

maxmemory is not the exact maximum amount of memory that a Redis instance can use. Each new command may add data to the database even if the memory usage exceeds maxmemory. However, Redis will start evicting keys according to the policy that is configured, depending on the type of key (persistent or volatile) and on the algorithm used (LRU or LFU). Refer to the different eviction policies in the documentation. Follows an overview of the algorithm that is triggered when the maxmemory threshold is exceeded.

  • Keys to be evicted are found using random sampling, this determines an approximation of how the LRU and LFU are implemented
  • A few good candidate keys are sampled (5-10)
  • The oldest of them is evicted

A description of the algorithm is available in the evict.c source code file.

/* LRU approximation algorithm
 *
 * Redis uses an approximation of the LRU algorithm that runs in constant
 * memory. Every time there is a key to expire, we sample N keys (with
 * N very small, usually in around 5) to populate a pool of best keys to
 * evict of M keys (the pool size is defined by EVPOOL_SIZE).
 *
 * The N keys sampled are added in the pool of good keys to expire (the one
 * with an old access time) if they are better than one of the current keys
 * in the pool.
 *
 * After the pool is populated, the best key we have in the pool is expired.
 * However note that we don't remove keys from the pool when they are deleted
 * so the pool may contain keys that no longer exist.
 *
 * When we try to evict a key, and all the entries in the pool don't exist
 * we populate it again. This time we'll be sure that the pool has at least
 * one key that can be evicted, if there is at least one key that can be
 * evicted in the whole database. */

The approximation of the eviction algorithm is justified by the need to balance latency and memory usage well. Consider such factors when doing sizing considerations. Heavy traffic may put pressure on the memory usage, and cause OOM issues for the Redis Server processes.

Make sure to benchmark a realistic workload to confirm the memory usage is within acceptable values, this will avoid OOM killer problems. A good conservative rule of thumb is to keep 20% of free memory available in the system beyond the configured maxmemory. It is also possible to test more aggressive eviction policies by using a custom configuration; check configuration parameters such as maxmemory-samples or maxmemory-eviction-tenacity. Refer to the configuration file for a description of such parameters.

References