I am using redis as a datastore rather than cache, but there is a maxmemory limit set, In my understanding the maxmemory specifies the RAM that redis can use, should it not swap the data back to disk once the memory limit is reached.
I have a mixture of keys while some have their expiry set and others don’t
I have tried both volatile-lru and allkeys-lru, as specified in the documentation both remove the old keys based on the property.
What configuration should I use to avoid data loss? Should I set an expiry on all keys and use volatile-lru? What am I missing?
3
Answers
As given in the documentation
Using Redis as an LRU cache
Swapping out memory to disk (virtual memory) was deprecated/deleted in Redis 2.4/2.6. Most likely, you are not using such an old version.
You control what Redis does when memory is exhausted with
maxmemory
andmaxmemory-policy
. Both are settings inredis.conf
. Take a look. Swapping memory out to disk is not an option in recent Redis versions.If
maxmemory
is reached, you lose data only if the eviction policy set inmaxmemory-policy
indicates Redis to evict some keys and how to select these keys (volatile or all, lfu/lru/ttl/random). Otherwise, Redis start rejecting write commands to preserve the data already in memory. Read commands continue to be served.You can run Redis without a
maxmemory
setting (default), so it will continue using up memory until the OS memory is exhausted.If your operating system has virtual memory enabled, and the
maxmemory
setting allows Redis to go over the physical memory available, then your OS (not Redis) starts to swap out memory to disk. You can expect a performance drop then.Do not set that param if you’re use redis as a datastore, it is used for cache scenario.