Skip to content

Troubleshooting · Redis

Redis out of memory or evicting keys? 7 causes, ranked

7 causes, most common first · last reviewed

TL;DR

Redis is doing what maxmemory and maxmemory-policy tell it to: evicting keys, or refusing writes under noeviction. The usual cause is data without a TTL, or a working set larger than the limit. Start with INFO memory and INFO stats, then find what is taking the space with redis-cli --bigkeys or --memkeys.

On this page

Quick triage: the first five minutes

  1. Read the memory picture: how much is used, the limit, the policy, and how much is data versus overhead.

    Shell
    redis-cli INFO memory | grep -E '^(used_memory_human|used_memory_rss_human|used_memory_dataset|maxmemory_human|maxmemory_policy|mem_fragmentation_ratio|mem_clients_normal):'
    used_memory close to maxmemory means eviction or OOM errors; RSS far above used_memory means fragmentation.
  2. See whether keys are being evicted, and what that is doing to the hit ratio.

    Shell
    redis-cli INFO stats | grep -E '^(evicted_keys|expired_keys|keyspace_hits|keyspace_misses):'
    redis-cli INFO keyspace
    evicted_keys rising with misses rising: the cache is too small for its working set. INFO keyspace shows how many keys have a TTL (expires).
  3. Find what is using the memory.

    Shell
    redis-cli --bigkeys
    redis-cli --memkeys
    Both scan the keyspace with SCAN; run them against a replica if the primary is busy.

The causes, ranked

Most common first. The ranking is a judgement from how these failures usually turn out, not a measured statistic.

Most common

1. Keys without a TTL pile up

What it looks like

  • Memory grows steadily for days or weeks, regardless of traffic.
  • INFO keyspace shows far fewer keys with an expiry than keys in total.
  • Writes start failing with OOM command not allowed when used memory > 'maxmemory', or old keys start disappearing.

How to confirm it

Compare keys and expires in INFO keyspace, then sample keys and check their TTL. A TTL of -1 means the key never expires. Group by key prefix to see which feature writes them.

Shell
redis-cli --scan --pattern 'session:*' | head -1000 | \
  while read k; do redis-cli TTL "$k"; done | sort | uniq -c | sort -rn | head
Many -1 results: that prefix never expires.

Fix it now

Add a TTL wherever keys are written, and set one on existing keys in batches. If the instance is a pure cache, switch to an allkeys policy so Redis can always make room.

Shell
redis-cli CONFIG SET maxmemory-policy allkeys-lru

Prevent it

Make the cache wrapper require a TTL, add some jitter to TTLs so keys do not all expire at once, and alert on memory growth rate, not only on how full it is. A steady climb is visible days before the limit.

Most common

2. The eviction policy does not match how the data is used

What it looks like

  • OOM errors on writes even though most keys are just cache entries.
  • The policy is noeviction (the default), or a volatile-* policy while most keys have no TTL.
  • Or the opposite: keys that must not be lost, like job queues or locks, disappear under an allkeys policy.

How to confirm it

Read maxmemory-policy. volatile-lru, volatile-lfu, volatile-random and volatile-ttl only evict keys that have an expiry; with none to evict, writes fail as they would under noeviction.

Shell
redis-cli CONFIG GET maxmemory
redis-cli CONFIG GET maxmemory-policy

Fix it now

Use allkeys-lru or allkeys-lfu for caches, and noeviction for data that must never silently vanish. On managed services that block CONFIG SET, change the setting through the provider's parameter group or configuration instead.

Prevent it

Do not mix cache data and data you cannot lose in the same instance; give each its own instance, or at least its own policy.

Common

3. The working set is bigger than maxmemory

What it looks like

  • evicted_keys rises continuously during normal traffic.
  • The hit ratio, keyspace_hits ÷ (hits + misses), falls, and load on the database behind the cache rises.
  • Memory sits right at maxmemory all the time.

How to confirm it

Work out the hit ratio over time and correlate it with evictions. If evictions remove keys that are requested again soon, the cache is too small for what is actively used.

Fix it now

Add memory or shards, cache smaller values, or cache fewer things. Raising maxmemory is only safe if the host has headroom for it plus fork and client overhead.

Prevent it

Track hit ratio and evictions per instance, and size caches for the peak working set, not the average.

Common

4. A few keys or collections are huge

What it looks like

  • --bigkeys or --memkeys shows a handful of keys taking a large share of memory.
  • A list, set, hash or stream used as a queue or log grows without a bound.
  • Latency spikes when those keys are read, written or deleted.

How to confirm it

Measure the biggest keys directly and find the code that writes them.

Shell
redis-cli MEMORY USAGE feed:global SAMPLES 0
redis-cli TYPE feed:global
redis-cli LLEN feed:global

Fix it now

Trim or cap the collection (LTRIM, XTRIM with MAXLEN), split it into smaller keys, and delete large keys with UNLINK, which frees memory in the background, rather than DEL.

Prevent it

Put an explicit size limit on every collection that grows with users or time, and alert on the biggest keys.

Less common

5. Memory goes to clients and buffers, not data

What it looks like

  • used_memory is high but used_memory_dataset is a much smaller part of it.
  • mem_clients_normal is large; CLIENT LIST shows clients with large omem (output buffer memory).
  • Slow subscribers, a replica that fell behind, or a client reading huge replies.

How to confirm it

Read INFO memory overhead fields and list clients sorted by output buffer use. Output buffers count towards used_memory, so they can trigger eviction of real data. MEMORY DOCTOR gives a summary.

Shell
redis-cli CLIENT LIST | awk '{for(i=1;i<=NF;i++) if($i ~ /^omem=/) {split($i,a,"="); print a[2], $0}}' | sort -rn | head -5
redis-cli MEMORY DOCTOR

Fix it now

Disconnect the offending client (CLIENT KILL), and set client-output-buffer-limit so one slow consumer cannot consume the instance.

Prevent it

Keep pub/sub subscribers fast, avoid commands that return huge replies, and keep replicas close enough to the primary to keep up.

Less common

6. Fragmentation: Redis's own count looks fine, the OS disagrees

What it looks like

  • mem_fragmentation_ratio well above 1.5; used_memory_rss much larger than used_memory.
  • The container or host runs out of memory, or the OOM killer stops Redis, although used_memory is below maxmemory.
  • Often after deleting many keys or a change in value sizes.

How to confirm it

maxmemory is compared with used_memory, which excludes fragmentation. Compare RSS with used_memory and with the container or host memory limit.

Fix it now

Enable active defragmentation (available with the default jemalloc allocator), or restart a replica and fail over to it to compact memory.

Shell
redis-cli CONFIG SET activedefrag yes

Prevent it

Set maxmemory well below the memory actually available, leaving room for fragmentation, client buffers and forks.

Rare but nasty

7. Snapshots and rewrites double memory for a moment

What it looks like

  • Redis is killed or the container is OOMKilled during BGSAVE or an AOF rewrite.
  • Memory spikes on a schedule that matches snapshot settings.
  • The instance is write-heavy, so many pages change while the fork runs.

How to confirm it

Check persistence timings in INFO persistence, and compare the spike with the moment rdb_bgsave_in_progress or aof_rewrite_in_progress was 1. A fork shares memory pages until they are written, so each changed page is copied.

Shell
redis-cli INFO persistence | grep -E '^(rdb_bgsave_in_progress|aof_rewrite_in_progress|rdb_last_cow_size|aof_last_cow_size):'

Fix it now

Give the host or container headroom above maxmemory, take snapshots on a replica instead of the primary, or snapshot less often.

Prevent it

Size memory limits as maxmemory plus fork copy-on-write plus buffers, not maxmemory alone.

See it happen in a real outage

90-second puzzles built from public postmortems. Diagnose it first; the company is revealed after you answer.

Related concepts

FAQ

What does 'OOM command not allowed when used memory > maxmemory' mean?

Redis reached maxmemory and its policy did not allow it to free space, either because the policy is noeviction or because a volatile-* policy found no keys with an expiry. Writes fail; reads still work.

Which maxmemory-policy should I use?

For a cache, allkeys-lru or allkeys-lfu. For data you cannot lose, such as queues or locks, noeviction, so you get an error instead of silent loss. Avoid volatile-* policies unless nearly every key has a TTL.

How do I find which keys use the most memory in Redis?

redis-cli --bigkeys reports the largest key of each type and --memkeys reports by memory used; both scan the keyspace. For one key, MEMORY USAGE gives its size in bytes.

Why is Redis using more memory than maxmemory?

maxmemory limits used_memory, which does not include fragmentation, and memory for forks during snapshots is copied on write. The process can therefore use noticeably more RAM than the limit.

All troubleshooting guides

Reference documentation: Redis documentation: key eviction ↗ · Redis documentation: INFO ↗ · Redis documentation: memory optimization ↗ · Report a problem

Commands and settings are checked against the documentation linked above. Defaults change between versions; check the documentation for the version you run.