SteelReed

Search

Search posts, questions…

Caching & RedisQuestion 10 / 24

Middle

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.

not yetnoevictionallkeys-lru or lfuvolatile-*Write commandAt maxmemory?Normal writeOOM errorEvict rarely used keysEvict only keys with TTL

3 points to say:

  1. Expiry = lazy (checked on read) + active (about 10 times a second, sampling 20 keys); RAM is not freed the very second a key expires.
  2. Replicas don't expire keys themselves; they wait for a DEL from the master.
  3. 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.