Redis maxmemory and Eviction Policies: A Config Recipe

Out of the box, Redis has no memory limit. Leave it that way and it will grow until the Linux OOM killer reaps it — usually at the worst possible moment, and usually taking your cache-dependent service down with it.

The default value of maxmemory on a 64-bit build is 0, which means unlimited. The default maxmemory-policy is noeviction. Put those two defaults together and you have a process that will happily consume every byte of RAM on the box and then, instead of shedding load gracefully, start returning errors on writes or getting killed by the kernel. Neither is what you want from something you are treating as a cache. This recipe sets a hard ceiling and chooses the eviction behaviour that matches what Redis is actually doing for you. The examples are for Redis 7.4, but the directives are stable across recent versions.

Set the ceiling first

Decide how much memory Redis is allowed to use, and set it explicitly. The rule of thumb on a dedicated instance is to leave headroom below total RAM — Redis needs room above used_memory for replication buffers, client output buffers, and the copy-on-write fork during background saves. A common starting point is to cap Redis at around 60–70% of available memory on a box that also does other work, and to leave a couple of gigabytes free even on a dedicated one.

# redis.conf — bound Redis so it behaves as a cache, not an OOM risk
maxmemory 2gb
maxmemory-policy allkeys-lru
maxmemory-samples 5

You can set the same values at runtime without a restart, which is useful for tuning a live instance before you commit them to the config file:

redis-cli CONFIG SET maxmemory 2gb
redis-cli CONFIG SET maxmemory-policy allkeys-lru
# persist the running config back to redis.conf
redis-cli CONFIG REWRITE

The ceiling is the easy part. The policy is where people quietly lose data.

Pick the policy that matches the workload

Redis 7.4 offers eight eviction policies. The split that matters is between the allkeys-* family, which can evict any key when memory is tight, and the volatile-* family, which will only evict keys that have a TTL set. Get this distinction wrong and the failure is silent: Redis evicts something you needed, no error is raised, and you find out when a lookup misses.

  • allkeys-lru — the right default for a pure cache. Every key is disposable; Redis evicts the least recently used. If everything in the instance can be recomputed or refetched from a source of truth, this is what you want.
  • allkeys-lfu — same idea, but evicts the least frequently used, using a probabilistic counter with decay. Better than LRU when you have a stable hot set that a burst of one-off reads would otherwise push out.
  • volatile-ttl / volatile-lru / volatile-lfu — for a mixed instance holding both cache entries (with a TTL) and data that must not be evicted (no TTL). volatile-ttl evicts the keys closest to expiring first. The catch: if memory fills and there are no volatile keys left to evict, Redis behaves like noeviction and starts erroring on writes.
  • noeviction — the default. Correct only when Redis is your datastore, not a cache, and dropping a key would be data loss. Under memory pressure it rejects writes with an OOM error and keeps reads working. Choose this deliberately, never by accident.

The most common mistake I see is running a mixed instance — sessions with a TTL alongside reference data without one — on allkeys-lru. That configuration will cheerfully evict your reference data the moment memory tightens, because to allkeys-lru every key is fair game. If some keys must survive, they must not have a TTL and you must be on a volatile-* policy.

The accuracy knob nobody touches

Redis does not maintain a perfect global LRU or LFU ordering — that would cost memory and CPU on every access. Instead it samples. On each eviction it picks a small number of candidate keys and evicts the best one from that sample. The size of that sample is maxmemory-samples, which defaults to 5.

Five samples gives an approximation that is close to true LRU for most workloads. Raising it to 10 tightens the approximation at the cost of a little more CPU per eviction; dropping it to 3 is faster but sloppier. For the overwhelming majority of instances, leave it at 5. Only reach for this knob if you have measured that eviction is choosing poorly and you have ruled out the more likely cause: the wrong policy.

Read the state, then alert on it

Configuration you cannot observe is configuration you are guessing at. Two INFO sections tell you almost everything about how the ceiling is behaving:

# current usage, the ceiling, and the active policy
redis-cli INFO memory | grep -E 'used_memory:|used_memory_human:|maxmemory:|maxmemory_human:|maxmemory_policy:|mem_fragmentation_ratio:'

# eviction and hit-rate counters (these live in INFO stats, not INFO memory)
redis-cli INFO stats | grep -E 'evicted_keys:|keyspace_hits:|keyspace_misses:'

The number to watch is evicted_keys. A steadily rising count is not automatically bad — for a pure cache under allkeys-lru, some eviction is the system working as designed. What matters is the trend against your hit rate. If evicted_keys is climbing and keyspace_misses is climbing with it, your working set no longer fits in maxmemory and you are thrashing: evicting keys you are about to ask for again. That is a signal to raise the ceiling or add capacity, not to fiddle with the policy.

The alert worth wiring up is the rate of change of evicted_keys, not its absolute value. A sudden jump means either a traffic shift or something writing far more than usual. Watch mem_fragmentation_ratio too: well above 1.5 means the allocator is holding far more RSS than Redis is using, which eats into the headroom you left below maxmemory.

This is the same discipline as any other bounded-resource recipe — the point of a limit is that the system degrades predictably instead of falling over. It is the memory-side sibling of keeping logs from filling the disk, and it interacts directly with how you have configured swap on the host: a Redis instance that starts swapping has already lost, because latency collapses long before the OOM killer would have intervened.

Set the ceiling, choose the policy for what the data actually is, and alert on the eviction rate. Do those three things and Redis stops being the thing that pages you at 3am and goes back to being the boring, fast cache you wanted.

Free interactive tool

Website compliance checklist

What your site has to do, based on what it actually does

Answer as much or as little as you like — the list builds as you go. Nothing is stored against your name and no email is required.

Most technology problems are not technology problems. They are control problems.

The systems exist. The investment has been made. The question is whether leadership can understand, direct, evidence, and sustain what those systems produce. Find out where control exists — and where it only appears to.