No-Eviction-Policy with Redis
We have been running Redis for caching recently used queries as a Kubernetes pod. Recently it started throwing errors. Upon further investigation, it was found that the cache was full. i.e. it has consumed all the available memory it had.
We woke up to our health page going down, shooting us panic emails. Our expectations were that the older keys will get overwritten with the newer ones, so there should not be a case where it runs out of memory.
The Eviction Policy
Turns out, we can’t just assume that. This is what Redis calls an Eviction Policy. An eviction policy decides how the keys will be removed. By default, it is set to no-eviction, meaning once the memory is full, Redis will get mad and reject further writes and throw errors. Our status page caught this error and started panicking.
If you want to make sure of this you can run the below command in your redis-cli
127.0.0.1:6379> config get maxmemory-policy
1) "maxmemory-policy"
2) "noeviction"
Adding the Policy
apiVersion: v1
kind: ConfigMap
metadata:
name: redis-config
data:
redis.conf: |
protected-mode no
bind 0.0.0.0
port 6379
maxmemory-policy volatile-lru
The line maxmemory-policy
is what you need to add to your .yaml
file. The policy we used here is called volatile-lru
which means if memory is full remove the least used keys in the order of their expiration with the nearing expiration ones removed first.
And we are done!