How does TTL work in Redis, and what happens when Redis runs out of memory?
Short answer
Expired keys are not deleted immediately: Redis deletes lazily when someone touches the key and actively by sampling periodically. On hitting maxmemory it applies maxmemory-policy: the default noeviction rejects writes; a cache should use allkeys-lru or allkeys-lfu.
3 points to say:
- Expiry = lazy (checked on read) + active (about 10 times a second, sampling 20 keys); RAM is not freed the very second a key expires.
- Replicas don't expire keys themselves; they wait for a
DELfrom the master. - Redis LRU is approximate (samples 5 keys); LFU resists a one-off scan pushing hot data out.
Keep reading the deep dive
Create a free account to read the full explanation and follow-up questions, mark questions as studied and review them on a schedule.