Redis

Redis Maxmemory Policies

This article will discuss Redis maxmemory policy and how you can use it as an LRU cache.

What is Redis Eviction Policy?

As the name suggests, an eviction policy is a feature that allows Redis to “evict” or remove old data as new data is added. This is an incredible feature primarily when Redis is used as a caching system.

Redis Maxmemory Directive

In the Redis configuration file, a directive called maxmemory dictates the amount of memory allocated for a data set.

Although you can configure the maxmemory at runtime using the config set command, the redis.conf file allows you to set it in persistence mode.

To set the maxmemory value at runtime, log in to Redis CLI and enter the command:

config set maxmemory 100

The command above will set the maxmemory value to 100mb at runtime. You can get the current set value for maxmemory using the command:

127.0.0.1:6379> config get maxmemory*

This should return:

1) "maxmemory-policy"

2) "noeviction"

3) "maxmemory-samples"

4) "5"

5) "maxmemory"

6) "100"

The first is the eviction policy. This dictates how the data is removed after the maxmemory is reached.

NOTE: Setting the maxmemory to 0 ensures there are no limits. For 64bit systems, this is set to default. On 332-bit systems, Redis will use up to 3GB.

How Eviction Process Works

But how exactly does this eviction process work?

That happens in 4 simple steps.

  1. First, the client runs the command to add new data.
  2. Before the command is executed, Redis check if the memory usage is higher than the set maxmemory limit.
  3. If the limit is reached, it uses the specified eviction policy to remove the keys.
  4. Finally, the command is executed, and new data is added.

Redis Eviction Policies

We keep mentioning eviction policies. What are they, and what do they do?

In simple terms, an eviction policy sets the rules for how Redis evicts a key when the memory limit is reached.

There are two categories of eviction policies in Redis.

General Eviction Policies

The first category is general eviction policies. These policies apply to any key that does not have an expiration value.

They include:

Noeviction

This eviction policy tells Redis not to remove any data when the memory limit is reached. Instead, Redis will return an error and fail to run the add data command.

This policy is heavily applicable when you need to remove the keys manually or prevent accidental data loss.

Allkeys-LRU

The second policy is allkeys-lru. This type of policy evicts any least recently used key or LRU.

This policy assumes that you do not need the recently used keys and evicts them. It prevents Redis from erroring out in case of a memory limit.

Allkeys-random

This policy will remove the keys in any random order. This is an excellent policy when the keys in your database are of equal importance.

Volatile Eviction Policies

These types of eviction policies rely on keys with expiration values. They include:

Volatie-random

This eviction policy removes keys with an expiration value in any random order. It is similar to the allkeys-random policy, except it only removes expired keys.

Volatile-TTL

The volatile-TTL policy removes keys with an expiration time but choses the one with the Shortest Time to Live first.

Volatile-LRU

Similar to Volatile-TTL but removes the least frequently used keys first.

Conclusion

This is an introduction to Redis maxmemory and eviction policies. You can learn more about the Redis Eviction rules in the resource below.

https://redis.io/topics/lru-cache

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list