Redis Deletion Strategy For Expired Data
Redis Deletion Strategy
Redis deletes expired keys using two strategies together: lazy deletion + periodic deletion.
Lazy deletion is passive. Before reading or writing a key, Redis checks whether it has expired. If it has, Redis deletes it on the spot and returns an empty result. The advantage is that no extra CPU is spent scanning. The downside is that a key that expires but is never accessed again stays in memory indefinitely.
Periodic deletion is active cleanup. Every 100ms, Redis runs a scheduled task that randomly samples a batch of keys with expiration times set and deletes any that have expired. It doesn’t sweep through every expired key at once. Instead, it does limited, sample-based cleanup so the cleanup itself doesn’t hog the CPU.
Each time the periodic deletion task runs, it does this:
- Randomly pick 20 keys from the set of keys that have an expiration time.
- Delete the ones that have already expired.
- If more than 25% of them were expired, there are probably still many expired keys, so pick another batch of 20.
- If less than 25% were expired, there aren’t many left, and this round ends.
- The whole process has a hard time limit of 25ms. If it runs over, it is stopped so it doesn’t block the main thread.

Expiration commands
# Set an expiration
EXPIRE key 60 # Expires in 60 seconds
PEXPIRE key 60000 # Expires in 60000 milliseconds
EXPIREAT key 1735689600 # Expires at a Unix timestamp
PEXPIREAT key 1735689600000 # Millisecond-precision timestamp
# Set a value and its expiration together
SET key value EX 60 # Recommended; a single atomic operation
SETEX key 60 value # Older syntax; same effect
# Check the remaining time
TTL key # Returns seconds; -1 = never expires, -2 = key doesn't exist
PTTL key # Returns milliseconds
# Remove the expiration
PERSIST key # The key no longer expires
Memory eviction policies
Lazy deletion and periodic deletion might not clean up fast enough, and memory can still reach the maxmemory limit. When that happens, writing new data triggers the memory eviction mechanism.
Redis has 8 eviction policies:
| Policy | Scope | Eviction rule |
|---|---|---|
| noeviction | No eviction | Returns an error and rejects writes |
| allkeys-lru | All keys | Evicts the least recently used first |
| allkeys-lfu | All keys | Evicts the least frequently used first |
| allkeys-random | All keys | Evicts at random |
| volatile-lru | Keys with an expiration set | Evicts the least recently used first |
| volatile-lfu | Keys with an expiration set | Evicts the least frequently used first |
| volatile-random | Keys with an expiration set | Evicts at random |
| volatile-ttl | Keys with an expiration set | Evicts keys with the shortest remaining TTL first |
The most common choices in production are allkeys-lru and volatile-lru. If all your cached data has an expiration time, volatile-lru is safer because it won’t delete important data that has no expiration
> redis-cli
127.0.0.1:6379> config get maxmemory
1) "maxmemory"
2) "1073741824"
127.0.0.1:6379> config get maxmemory-policy
3) "maxmemory-policy"
4) "noeviction"
127.0.0.1:6379> config set maxmemory-policy allkeys-lru
OK
- Check the maximum memory limit — when this threshold is reached, memory eviction is triggered, i.e. some keys get deleted.
- Check the memory eviction policy — the default is that once the threshold is reached, inserting another key simply throws an error.
- Set the memory eviction policy — generally LRU or LFU is used.
Difference between LRU and LFU
- LRU (Least Recently Used) — based on a linked-list structure, where elements are ordered front to back by operation order. The most recently operated-on key is moved to the head of the list, so when memory eviction is needed, you only need to delete the element at the tail of the list.
- LFU (Least Frequently Used) — its basic assumption is that if data has been accessed many times in the past, it will likely be accessed more frequently in the future as well. So it evicts the keys with the lowest past access frequency. Redis uses a complex but efficient method to approximate LFU.