Node.js › Caching
What is Redis's EXPIRE and how do TTL-based keys interact with Redis's own memory eviction policies under memory pressure?
AdvancedAnswer
EXPIRE (or the EX option on SET) sets a time-to-live on a Redis key, after which Redis automatically removes it -- but under memory pressure Redis can also proactively evict keys earlier than expiration according a configured eviction policy (like allkeys-lru, evicting the least recently used key regardless of TTL, or volatile-lru, only evicting keys with a TTL set), which matters because caching data with no TTL under an allkeys policy could have any key evicted at any time.
await redis.set('session:abc123', sessionData, { EX: 3600 });
# redis.conf: only evict keys that have a TTL set, oldest-accessed first
# maxmemory-policy volatile-lru
# maxmemory 2gbReal-world example A team configures their Redis instance with maxmemory-policy set to volatile-lru specifically because they store both cache data (with TTLs, safe to evict) and durable session data (without TTLs) in the same instance, ensuring memory pressure only ever evicts the cache entries.