Reference · Cheat sheet
Time kills one key; memory pressure kills many. Print it, pin it.
| TTL expiry | Eviction | |
|---|---|---|
| Trigger | Time — per key | Memory pressure — global |
| Scope | The one key you marked | Whatever the policy selects |
| Configured by | EXPIRE / SET EX | maxmemory + maxmemory-policy |
| Meaning | "This datum is stale" | "We're out of room" |
SET k v EX 300 — set value + 300s TTL atomically (PX = ms; EXAT/PXAT = absolute).EXPIRE k 300 — (re)set TTL, O(1). Options: NX/XX/GT/LT.TTL k — seconds left · -1 exists, no TTL · -2 no such key.PERSIST k — remove the TTL (volatile → persistent).SET wipes the TTL. Use SET … KEEPTTL to keep it.| Candidate set | lru | lfu | random | ttl |
|---|---|---|---|---|
| allkeys-* (any key) | ✓ | ✓ | ✓ | — |
| volatile-* (only TTL'd keys) | ✓ | ✓ | ✓ | ✓ |
noeviction = default; refuses writes with an OOM error when full.maxmemory-samples, default 5).volatile-ttl evicts the shortest remaining TTL first.Default is noeviction — a cache under memory pressure starts
erroring, not evicting. Set allkeys-lru/allkeys-lfu.
volatile-* with no TTL'd keys = nothing to evict = behaves like
noeviction.
Only the primary expires keys; replicas wait for its synthesized DEL. A replica
read can return a logically-expired value until then.
← lesson 0003 · Source: Redis Docs — Key eviction · expiration FAQ