Scrivio Interview library
DebuggingIntermediate7 min read · updated 12 November 2026

A Spring Boot service using Redis as a cache starts failing with 'OOM command not allowed when used memory > maxmemory', while the cache hit rate looked fine yesterday. What do you check and change?

The instance reached maxmemory while its policy was noeviction, the default, or a volatile-* policy with most keys lacking a TTL, so Redis has nothing it may evict and rejects every write. We confirm it with INFO memory, INFO stats, CONFIG GET maxmemory-policy and a sample of key TTLs, then set allkeys-lru or allkeys-lfu for a cache-only instance, give every cache entry a TTL with RedisCacheConfiguration.defaultCacheConfig().entryTtl(...), size maxmemory with headroom, find the change that started writing unbounded keys, and make the application treat cache write failures as misses.

Why this question is asked

This error is a classic because it appears suddenly, after weeks of the cache working well, and it turns a performance optimisation into an outage: requests fail because a cache write fails. Interviewers ask it to see whether we can connect the message to Redis's memory policy, check the right numbers, and fix both Redis and the application so a cache problem can never take the service down again. In this answer, we will read the error, look at what to check and in which order, see why yesterday's hit rate did not warn us, fix the configuration and the Spring cache setup, and make the service degrade gracefully.

1Reading the Error

The full message is OOM command not allowed when used memory > 'maxmemory'. It means that Redis's memory use has reached the maxmemory limit, that the command we sent could add data, and that Redis was not allowed or not able to free memory by evicting keys. Reads still work; writes such as SET fail. In a Spring Boot service, the failure surfaces as an exception from the cache put after a cache miss, so a request that should simply have loaded from the database fails instead.

Two configurations produce it. With maxmemory-policy noeviction, the default, Redis never evicts, so a full instance rejects writes by design. With a volatile-* policy, Redis may evict only keys that have a TTL; if most keys have none, it runs out of candidates and behaves the same way.

2What to Check, in Order

We start with the numbers Redis already has.

YAML
INFO memory
# used_memory_human:3.98G
# maxmemory_human:4.00G
# maxmemory_policy:volatile-lru
INFO stats
# evicted_keys:0 nothing could be evicted
# expired_keys:12044 few keys expire
INFO keyspace
# db0:keys=5821044,expires=3102,avg_ttl=540000 only 3,102 of 5.8 million keys have a TTL
CONFIG GET maxmemory-policy
# "volatile-lru"
Step throughDiagnosing the OOM error
  1. used_memory at maxmemory?
    YesMemory is fullNoLook at client errors instead
  2. Policy noeviction?
    YesNothing is ever evictedNoCheck which keys are candidates
  3. volatile-* and few keys with TTL?
    YesNo candidates to evictNoLook at value sizes
  4. Sample keys by prefix with SCAN and TTLfind the keys without TTL
  5. Match the prefix to a recent changedeploy, new cache, new serializer

5 steps in order. Play them, or step through them one at a time.

The keyspace line tells most of the story. Of 5.8 million keys, only about 3,000 have a TTL. With volatile-lru, those are the only keys Redis may evict, and they are not enough. evicted_keys at zero confirms that Redis is not freeing anything.

Next we find out which keys have no TTL. We sample with SCAN and TTL, in a script against a replica if possible, and group by key prefix. A result such as 5.7 million keys starting with product::search: and TTL -1 points straight at the culprit. MEMORY USAGE on a few samples tells us whether the problem is the number of keys or the size of each value.

3Why Yesterday's Hit Rate Looked Fine

A good hit rate says nothing about memory. Yesterday the instance was perhaps at 3.9 GB, slowly filling with keys that never expire, and every read of a popular product was still a hit. The memory line was rising for days or weeks, and the error appeared the moment it crossed maxmemory.

Usually a recent change started that rise. A typical one is a new @Cacheable on a search method whose key includes the whole query string, creating a new key for every distinct search, with no TTL because the default Spring Redis cache configuration has none. Another is a serializer change, for example switching to a JSON serializer that writes type information and doubles the size of every value. We look at the deploys around the time the memory graph changed slope.

4Fixing Redis and the Spring Cache Configuration

For an instance used only as a cache, the policy should be allkeys-lru or allkeys-lfu, so any key can be evicted when memory is full and writes never fail for lack of space. We also size maxmemory with headroom below the machine's memory, leaving room for fragmentation, client buffers and, if persistence is on, forks.

Java
CONFIG SET maxmemory-policy allkeys-lru # immediate relief; also persist it in configuration
@Bean
RedisCacheManagerBuilderCustomizer cacheTtls() {
return builder -> builder
.cacheDefaults(RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofMinutes(10)) // every cache gets a TTL by default
.disableCachingNullValues())
.withCacheConfiguration("productSearch",
RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofMinutes(2)));
}
// Spring Boot property alternative: spring.cache.redis.time-to-live=10m

Notice that RedisCacheConfiguration.defaultCacheConfig() on its own has no TTL, so entries live forever. Setting entryTtl() in the defaults means a new @Cacheable added next month gets a TTL without anyone remembering to set one. For the culprit cache, we also reconsider the key: caching every distinct search string may not be worth it at all, and normalising the query or caching only the top searches keeps the key count bounded.

Changing the policy fixes the errors immediately, because Redis can now evict the old keys. Setting TTLs stops the cache from filling with stale data again.

5Degrading Gracefully When the Cache Fails

The deeper lesson is that a cache failure should never fail a request. If a cache put fails, the request already has the value from the database and can return it; if a cache get fails, it can treat it as a miss. Spring lets us express this with a CacheErrorHandler.

Java
@Configuration
class CacheConfig implements CachingConfigurer {
@Override
public CacheErrorHandler errorHandler() {
return new LoggingCacheErrorHandler(); // logs get/put/evict errors instead of throwing
}
}
// With the handler, during the incident:
// cache put failed: OOM command not allowed ... (logged, metric incremented)
// request returns 200 with data from the database

With this handler, the incident would have been a rise in database load and a log alert instead of failing requests. We pair it with short Lettuce command timeouts so a slow Redis cannot hold request threads, and with metrics on cache errors so the logs do not hide a real problem.

6Follow-up questions

How would you prevent a key without TTL from being written?

Redis itself has no setting that requires a TTL, so we enforce it at the edges. In Spring we set a default entryTtl for every cache, and in code that writes directly with RedisTemplate we go through a small wrapper that always requires a Duration. We back that with monitoring: alert when the ratio of expires to keys in INFO keyspace drops, and run a periodic SCAN sample that reports prefixes with TTL -1.

Should a cache instance ever use noeviction?

Almost never. noeviction is for data we must not lose silently, where failing writes is better than dropping data, like a queue. For a cache, losing an old entry is harmless, so an allkeys policy that evicts is the safe behaviour. If an instance mixes cache data and important data, the right fix is to split them into separate instances with different policies.

Why not just raise maxmemory?

It buys time but does not fix the cause. If keys without a TTL keep arriving, any limit will be reached eventually, and the instance may then use memory the machine needs for forks and buffers. Raising maxmemory is reasonable only after the policy and TTLs are fixed and the working set is known to need more.

7Conclusion

In this answer, we saw that the OOM error means Redis reached maxmemory with no evictable keys, because of noeviction or a volatile-* policy while most keys had no TTL. We checked INFO memory, INFO stats, INFO keyspace, the policy and a TTL sample by prefix to find the keys and the change that wrote them. We fixed it with allkeys-lru or allkeys-lfu, a default entryTtl in RedisCacheConfiguration, and maxmemory with headroom, and made the service resilient with a CacheErrorHandler and short timeouts so cache failures become misses.

What to say in the interviewAbout 80 seconds aloud

Open with

That error means Redis is at maxmemory and is not allowed to evict anything, so every write is rejected. Either the policy is noeviction, which is the default, or it is a volatile policy like volatile-lru while most keys have no TTL, so there are no eviction candidates.

Then, if they want more

I would check INFO memory for used memory against maxmemory and the policy, INFO stats where evicted_keys staying at zero confirms nothing is freed, and INFO keyspace, which shows keys versus expires; if only a few thousand of millions of keys have a TTL, that is the cause.

Then I sample keys with SCAN and TTL, grouped by prefix, to find which cache writes keys without TTL, and look for the recent change, often a new @Cacheable with a per-request key, or a serializer that made values bigger. The hit rate looked fine because hit rate says nothing about memory slowly filling.

For the fix: on a cache-only instance set allkeys-lru or allkeys-lfu, give every cache a TTL through RedisCacheConfiguration.defaultCacheConfig().entryTtl in the cache manager defaults, because the default has none, and size maxmemory with headroom. Finally, I would configure a CacheErrorHandler that logs and treats cache errors as misses, plus short Lettuce timeouts, so a cache problem never fails requests again.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
Redis documentation
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "A Spring Boot service using Redis as a cache starts failing with 'OOM command not...": https://scrivio-interview-library.netlify.app/databases/redis/af1f8fd6db/