Heroku Redis Maxmemory strategy

Heroku provides a great Redis plugin, which can provide 25MB of memory for free.

Depending on what you want to store, you can easily fill this much storage space with thousands of items.

Heroku has a configuration option calledMaximum memory strategyIt determines the behavior of the system when the Redis database memory is full.

By default, this property is set tonoeviction, Which means that Redis will raise an error when it tries to store more data.

This is done to make you aware of what’s going on, and once you find that you can change this behavior, you should be surehow is it.

Redis itself provides various behaviors, they are:

  • noeviction: An error is returned when the memory limit is reached and the client tries to execute a command that may cause more memory usage (most write commands, but DEL and some other exceptions).
  • allkeys-lru: Escape the key by trying to delete the newer (LRU) key first to make room for the new data added.
  • volatile-lru: Exit the key by trying to delete the newer used (LRU) key first, but only delete the key that has been set to expire to make room for new data added.
  • allkeys-random: Randomly eject keys to make room for new data added.
  • volatile-random: Randomly evict keys to make room for new data added, but only evict keys with an expiration time.
  • volatile-ttl: Evict keys with expiration settings, and try to evict keys with a shorter time-to-live (TTL) first to make room for new data added.

You can find the best situation according to your needs. Once you have a candidate, you can use the Heroku CLI to apply changes, for example:

heroku redis:maxmemory YOUR_REDIS_INSTANCE_NAME --policy volatile-lru