Heroku offers a powerful Redis addon that comes with 25MB of free memory storage. But what happens when you exceed this limit? Heroku has a configuration option called maxmemory-policy that determines how the system handles the situation.
By default, the maxmemory-policy is set to noeviction
, which means that if you try to store more data than the available memory, Redis will raise an error. This is done to notify you of the issue and prompt you to take action.
Fortunately, Redis provides a number of different behaviors to handle memory overflow. Here are the available options:
noeviction
: Redis returns errors when the memory limit is reached and the client attempts to execute commands that would require more memory.allkeys-lru
: Redis evicts the least recently used (LRU) keys to free up memory for new data.volatile-lru
: Similar toallkeys-lru
, Redis evicts LRU keys but only among keys that have an expiration set.allkeys-random
: Redis randomly evicts keys to make space for new data.volatile-random
: Similar toallkeys-random
, Redis randomly evicts keys but only those with an expiration set.volatile-ttl
: Redis evicts keys with an expiration set, giving priority to keys with a shorter time to live (TTL).
Choosing the right maxmemory-policy depends on your specific needs and application requirements. Once you have determined the most suitable policy, you can apply the change using the Heroku CLI. For example:
heroku redis:maxmemory YOUR_REDIS_INSTANCE_NAME --policy volatile-lru
By adjusting the maxmemory-policy, you can ensure optimal memory usage and prevent data loss in your Heroku Redis instance.