Quick revision
The short answer
RedisCacheManager clears a cache by finding every key with the cache's prefix, products::*, and deleting them, and its default batch strategy finds them with KEYS, which walks the whole keyspace on Redis's single main thread and blocks every client for seconds at 3 million keys. We fix it by configuring a SCAN-based batch strategy, which deletes in small batches with flat latency, or better by not deleting at all: put a version in the key prefix, switch to the new version after the import, and let old keys expire by TTL. lazyfree-lazy-user-del keeps the deletes themselves cheap.
What to remember
In this answer, we saw that RedisCacheManager clears a cache by finding all keys with its prefix and deleting them, and that the default strategy uses KEYS, which blocks Redis's single thread for seconds on a large keyspace. A SCAN-based batch strategy keeps latency flat at the cost of a slower, non-atomic clear, and versioned prefixes avoid deletion entirely by letting old entries expire. lazyfree-lazy-user-del makes deletes cheap, and SLOWLOG, the latency monitor and a code search confirm the fix and find similar cases.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
RedisCacheManager clears a cache by finding every key with the cache's prefix, products::*, and deleting them, and its default batch strategy finds them with KEYS, which walks the whole keyspace on Redis's single main thread and blocks every client for seconds at 3 million keys. We fix it by configuring a SCAN-based batch strategy, which deletes in small batches with flat latency, or better by not deleting at all: put a version in the key prefix, switch to the new version after the import, and let old keys expire by TTL. lazyfree-lazy-user-del keeps the deletes themselves cheap.
Why this question is asked
@CacheEvict(allEntries = true) reads like a harmless one-liner, and on a small cache it is. On a cache with millions of entries in a shared Redis, it becomes a nightly outage for every service that uses that Redis. Interviewers ask this because it combines two things worth knowing: how Spring implements a cache clear, and why some Redis commands are dangerous on large datasets. In this answer, we will recap why a single command can freeze Redis, see how the clear is implemented, switch it to SCAN, avoid deletion with versioned prefixes, make deletes lazy and confirm the fix.
1A Quick Recap: Why One Command Can Freeze Redis
Redis executes commands on a single main thread, one at a time. Most commands touch one key and finish in microseconds, so this is fast. But a command that has to look at every key, such as KEYS, runs for as long as it takes to walk the whole keyspace, and while it runs no other command from any client is executed. With tens of millions of keys in the instance, KEYS takes seconds, and every other service sees timeouts for those seconds.
SCAN is the alternative. It walks the keyspace in small steps: each call returns a cursor and a batch of keys, and the client calls again with the cursor. Each call is short, so other commands run in between.
2How RedisCacheManager Clears a Cache
A Spring cache stored in Redis has no container of its own; a cache is just all keys with the same prefix, products:: by default. So clearing it means finding those keys and deleting them. The default cache writer does this with a batch strategy, and the default strategy uses KEYS products::* to find them, followed by DEL.
7 messages in order. Play them, or step through them one at a time.
Notice that KEYS cost depends on the total number of keys in the instance, not only on the size of the products cache. So even a small cache can freeze a large shared Redis. Here both are large, so the pause is two to four seconds, and services sharing the instance time out.
3Fix 1: A SCAN-Based Batch Strategy
Spring Data Redis provides a SCAN-based strategy. We build the cache writer with it and pass the writer to the cache manager. The clear then runs as many short SCAN calls, each followed by a DEL of up to 1,000 keys.
The trade-off is that the clear takes longer and is not atomic: while it runs, some product entries are already gone and others not. For a cache that is acceptable, because a missing entry just loads from the database. Notice that SCAN also walks the whole keyspace in total; it just does it in small slices, so latency stays flat.
4Fix 2: Retire the Cache Instead of Deleting It
Often we do not need to delete anything. If the cache key includes a version, such as products:v42::sku1, then after the import we switch the application to v43, and new reads miss and fill the new version. The old keys are never read again and disappear when their TTL passes. The clear becomes a configuration change that costs Redis nothing.
Whether the prefix function is evaluated per key or once per cache depends on the Spring Data Redis version, so check your version; if it is fixed at startup, a small custom CacheResolver that returns a cache named products-v43 achieves the same. The cost is memory, which we cover in the FAQ.
5Keeping Deletes Cheap and Confirming the Fix
Deleting a large value, such as a hash with many fields, frees its memory on the main thread by default. With lazyfree-lazy-user-del yes, a plain DEL behaves like UNLINK: Redis removes the key from the keyspace immediately and frees the memory in a background thread. That matters when cache entries are large.
Finally, confirm and look for more. SLOWLOG GET should no longer show KEYS after the change, and the latency monitor should show no spike at import time. A code search for allEntries = true on caches that can grow large, and for direct keys() calls, usually finds a few more of the same.
| Approach | Redis impact | Trade-off |
|---|---|---|
| Default (KEYS + DEL) | blocks all clients for seconds | simple |
BatchStrategies.scan(1000) | many 1 ms steps, flat latency | slower, not atomic |
| Versioned prefix + TTL | nothing at switch time | old keys use memory until TTL |
| lazyfree-lazy-user-del yes | memory freed in background | complements either approach |
6Follow-up questions
What does the versioned prefix cost in memory during the switch?
Up to twice the cache's memory for up to one TTL. After the switch, the new version fills as it is read while the old one waits for expiry. If products take 2 GB, Redis needs about 2 GB of headroom until the old keys expire, so we either keep that headroom, shorten the TTL so old entries leave sooner, or rely on an eviction policy such as volatile-lru to remove the old, unread keys first, since they are no longer accessed.
Why not use FLUSHDB?
It removes every key in the database, not only this cache, so in a shared Redis it deletes other services' data. In a dedicated instance it can be used with the ASYNC option, but a cluster has only database 0, so it is rarely the right tool for clearing one cache.
Does a SCAN-based clear guarantee every key is deleted?
SCAN guarantees to return every key that existed for the whole scan, so all entries present at the start and not deleted otherwise are found. Entries written during the clear may or may not be found, which is fine for a cache because they hold fresh data anyway.
7Conclusion
In this answer, we saw that RedisCacheManager clears a cache by finding all keys with its prefix and deleting them, and that the default strategy uses KEYS, which blocks Redis's single thread for seconds on a large keyspace. A SCAN-based batch strategy keeps latency flat at the cost of a slower, non-atomic clear, and versioned prefixes avoid deletion entirely by letting old entries expire. lazyfree-lazy-user-del makes deletes cheap, and SLOWLOG, the latency monitor and a code search confirm the fix and find similar cases.
Open with
Clearing a whole cache in Redis means deleting every key with the cache's prefix, products::, because a cache has no container of its own. RedisCacheManager finds those keys through a batch strategy, and the default strategy uses KEYS products::*.
Then, if they want more
KEYS walks the entire keyspace on Redis's single main thread, so with millions of keys it runs for seconds, and every client of that Redis waits, which is the two to four second freeze and the timeouts in other services. The direct fix is a SCAN-based strategy: RedisCacheWriter.nonLockingRedisCacheWriter with BatchStrategies.scan(1000), passed to RedisCacheManager.builder.
The clear then runs as many small SCAN and DEL steps; it takes longer and is not atomic, but latency stays flat, which is fine for a cache.
Often I would avoid deleting at all: put a version in the key prefix with computePrefixWith, such as products:v42::, switch to the next version after the import, and let the old keys expire through their TTL. That costs extra memory until they expire. I would also set lazyfree-lazy-user-del yes so DEL frees memory in a background thread like UNLINK.
Then I confirm with SLOWLOG and the latency monitor and search the code for other allEntries evictions on large caches.
About this answer
- Last updated
- 13 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 "Why does clearing the cache freeze Redis, and how do you clear or retire a large cache...": https://scrivio-interview-library.netlify.app/databases/redis-scaling/0f799dd483/