Expiry and Eviction
TTL, EXPIRE, and eviction policies
Skills taught: redis.eviction
Key Expiry Commands
Redis automatically deletes keys when their TTL expires. You can set expiry at write time or add it later to an existing key.
# Set with inline expiry at write time
SET token:abc "xyz" EX 900 # 900 seconds (15 min)
SET token:abc "xyz" PX 900000 # 900000 ms (15 min)
SET token:abc "xyz" EXAT 1735689600 # Unix timestamp
# Add expiry to existing key
EXPIRE session:1 1800 # 30 minutes from now
PEXPIRE session:1 86400000 # 24 hours in ms
EXPIREAT session:1 1735689600 # exact Unix timestamp
# Check TTL
TTL session:1 # seconds left (-1 = no expiry, -2 = key gone)
PTTL session:1 # milliseconds left
# Remove expiry (make permanent)
PERSIST session:1
# Conditional: only update expiry if new TTL is greater (Redis 7.0+)
EXPIRE session:1 3600 GT
- TTL (Time-To-Live)
- The remaining lifetime of a key in seconds. TTL returns -1 if no expiry is set, -2 if the key does not exist. Use PTTL for millisecond precision.
How Redis Expires Keys
Redis uses two mechanisms to handle expired keys: lazy expiry (checked on access) and active expiry (background periodic sweep).
- Lazy expiry: When you read a key, Redis checks if it's expired. If so, it deletes it and returns nil. No wasted CPU until the key is accessed.
- Active expiry: A background job runs every 100ms, randomly samples 20 keys with TTL, and deletes expired ones. If more than 25% are expired, it loops. This keeps expired key count below ~25% at all times.
Memory Limits and Eviction Policies
When Redis reaches its maxmemory limit, it needs to free memory. The maxmemory-policy setting controls what gets evicted.
# Set memory limit
maxmemory 512mb
# Eviction policy
maxmemory-policy allkeys-lru # recommended for a pure cache
# LRU sample size (higher = more accurate, more CPU)
maxmemory-samples 5
# Or via CLI (no restart needed)
redis-cli CONFIG SET maxmemory 512mb
redis-cli CONFIG SET maxmemory-policy allkeys-lru
Eviction Policy Comparison
- allkeys-lru: Evict any key, least recently used first
- volatile-lru: Evict only keys with TTL, LRU first
- noeviction — Return error on write when memory is full. Safe for primary stores.
- allkeys-lru — Evict any key, least recently used. Best for pure caches.
- volatile-lru — Evict only keys with TTL, LRU order.
- allkeys-lfu — Evict any key, least frequently used. Better for uneven access patterns.
- volatile-lfu — Evict only TTL keys, LFU order.
- allkeys-random — Random eviction (rarely useful).
- volatile-ttl — Evict the key with the shortest remaining TTL first.
Tip: For a pure cache, use allkeys-lru with a maxmemory set to 70-80% of available RAM. Leave headroom for the fork() Redis uses during RDB saves.
Frequently Asked Questions
What eviction policy should I use for a pure cache?
allkeys-lru is the most common. If your access pattern is highly uneven (some keys are accessed far more than others), allkeys-lfu (Least Frequently Used) may outperform LRU.
Does Redis delete expired keys immediately?
Not immediately. Redis uses lazy + active expiry. A key may persist for a brief time after its TTL if it hasn't been accessed and the active sweep hasn't reached it yet.
What is maxmemory-samples?
The number of keys Redis randomly samples when deciding which key to evict. Higher values (default 5) are more accurate but use more CPU. Setting it to 10 approaches true LRU behavior.