Redis has a maxmemory-policy where it decides how to handle keys when memory limit is reached.
|noeviction||will return errors when the memory limit is reached.|
|allkeys-lru||will remove less recently used keys first.|
|volatile-lru||will remove less recently used keys first that have an expiry set.|
|allkeys-random||will evict random keys.|
|volatile-random||will evict random keys but only those that have an expiry set.|
|volatile-ttl||will only evict keys with an expiry set and a short TTL.|
|volatile-lfu||will evict using approximated LFU among the keys with an expire set.|
|allkeys-lfu||will evict any key using approximated LFU.|
Here is how to change the maxmemory-policy on Heroku, with heroku-cli.
$ heroku redis:maxmemory <redis-instance-name> --policy allkeys-lru --app <app-name>
You should receive the message:
Maxmemory policy for redis-xxx-xxxx (REDIS_URL) set to allkeys-lru. allkeys-lru evict keys trying to remove the less recently used keys first.