Keys you can find, data that cleans itself up
Six months in, most Redis instances look like the left box: keys nobody can explain, and data nobody deletes. This chapter gets you to the right box.
- u42what is it?
- tmpwhose?
- data_final2still used?
- https://shop.com/p?id=9no TTL
- sess_9f2cno TTL
- otp_731904no TTL
- user:42:profilekeeps
- session:9f2c29:41
- otp:4212s
- cache:v2:product:959:02
- rl:ip:1.2.3.447s
- user:43:profilekeeps
Name keys like pathsAnyone can read a key and know what it is, and find all its siblings.
Give temporary data a TTLSessions, codes and caches delete themselves on time.
Fix 1Name keys like paths
Redis has no tables or folders. The key name is your schema.
user:42:profile · session:9f2c · rl:ip:1.2.3.4
Version caches: cache:v2:… — bump to v3 and every old entry is skipped at once.
u42, full URLs or JSON as keys, spaces, or raw user input without a prefix.
SCAN 0 MATCH user:*. Never KEYS * in production — it can freeze Redis for seconds. See why in chapter 3 →
Fix 2Give temporary data a TTL
A TTL (time to live) is a countdown on a key. When it hits zero, the key is gone.
SET on an existing key erases its TTL. Your 30-minute session now lives forever. Re-send EX, or add KEEPTTL.
| SET k v EX 60 | Write + expire, atomically. Your default. |
|---|---|
| EXPIRE k 60 NX | Add a TTL to an existing key (NX: only if it has none). |
| TTL k | Seconds left (PTTL for ms). |
| PERSIST k | Remove the TTL; keep the key. |
What happens when time’s up
The moment a TTL passes, GET returns (nil). You never see a stale key.
A background sweep, 10× a second, deletes expired keys and frees the RAM.
Go deeper: how Redis finds expired keys (diagram + lab)
- Redis keeps a second table: key → expiry time. Only keys with a TTL live there. Six of these have passed their time (⌛) but are still in memory.
- Lazy expiry: a client runs GET k7. Redis checks the time, sees it passed, deletes k7, and replies (nil). An expired key is never returned.
- But keys nobody reads would sit in RAM forever. So a background job runs 10 times a second…
- …it picks 20 random keys that have a TTL (here 7, to fit the picture) and deletes the expired ones: k2, k5, k12.
- 3 of 7 were expired — more than 25%. That means many more are probably waiting, so it samples again right away and finds k9 and k14.
- Result: reads are always correct, and memory is reclaimed in small, cheap steps instead of one big scan.
Lab · 60 keys, a sampler, and the 25% rule
Press “Time passes”, then run cycles. Watch how a high expired ratio triggers extra samples.
Expiry is not eviction
You set a TTL. The key goes when it ends, even if memory is empty.
Memory hits maxmemory, so Redis drops keys by policy (e.g. allkeys-lru) — or rejects writes.
Quick check
Show answer
Bump the version in the key: read and write cache:v3:product:*. Old v2 keys are never read again and expire on their own TTL.
INFO memory still counts it. Bug?
Show answer
No. Reads already return (nil); the background sweep just hasn’t reached it yet.
Remember
- Name keys
app:type:id:field. - Find keys with
SCAN, neverKEYS. - Anything temporary gets
EXwhen written. - Re-
SETerases the TTL unless you passEXorKEEPTTL.
Redis keys and expiry: common questions
How do I set an expiry on a Redis key?
SET key value EX 60 sets it with a 60-second TTL, or EXPIRE key 60 on an existing key. TTL key shows the seconds left.