Memory, eviction and persistence
Everything in Redis lives in RAM, and RAM is finite. This lesson covers how to see and control memory use, what Redis does when memory runs out, and how it saves data to disk so a restart doesn't wipe everything.
Seeing memory use
used_memory_human is the total. MEMORY USAGE key estimates bytes for one key. (The simulator's byte counts are approximations; a real server's exact numbers depend on its version and allocator.)
Saving memory
- Keep keys and values short.
u:1001:nvsuser:1001:namematters with 100 million keys (but readable names usually win). - Use hashes for small objects. Small hashes use the compact
listpackencoding, which is much smaller than one string key per field. - Always set TTLs on cache data, so stale entries clean themselves up.
- Don't store huge values. A 50 MB value blocks Redis while it's copied.
When memory runs out: maxmemory and eviction
maxmemory caps how much memory Redis may use for data. What happens when it's full depends on maxmemory-policy:
| Policy | When full… |
|---|---|
noeviction (default) |
Reject writes with an OOM error (reads still work) |
allkeys-lru |
Evict the least recently used keys |
allkeys-lfu |
Evict the least frequently used keys |
allkeys-random |
Evict random keys |
volatile-lru / volatile-lfu / volatile-random |
Same, but only keys that have a TTL |
volatile-ttl |
Evict keys with a TTL, soonest-to-expire first |
Pure cache? Use allkeys-lru or allkeys-lfu: Redis keeps the hot data and drops the rest. Data you can't lose (queues, sessions)? Use noeviction and monitor memory, because evicting a job would silently lose it.
See eviction happen
The simulator's DEBUG POPULATE count prefix size creates many keys at once (on a real server, DEBUG is disabled by default):
After the write, Redis evicted the least recently used item:* keys until memory was under 1 MB again: see evicted_keys. Now the same with noeviction:
Writes are refused with an OOM error, but reads still work.
Real Redis's LRU and LFU are approximate: they sample a few keys (
maxmemory-samples, default 5) and evict the best candidate among them. That's nearly as good as exact LRU and much cheaper.
Persistence: surviving restarts
Redis keeps data in memory, but it can also write it to disk in two ways.
RDB snapshots
Redis periodically writes a compact snapshot of the whole dataset to a dump.rdb file, using a forked child process so the server keeps serving requests.
save 3600 1 300 100 60 10000 means: snapshot after 3600 s if at least 1 key changed, after 300 s if 100 changed, after 60 s if 10,000 changed.
✅ Compact files, fast restarts, perfect for backups. ❌ You can lose everything written since the last snapshot (minutes of data).
AOF (append-only file)
With AOF, Redis logs every write command to a file and replays it on startup. How often the file is flushed to disk (appendfsync) sets the durability:
appendfsync |
You can lose… | Speed |
|---|---|---|
always |
Almost nothing | Slowest |
everysec (default) |
About 1 second of writes | Fast |
no |
Whatever the OS hasn't flushed (~30 s) | Fastest |
The log grows forever, so Redis periodically rewrites it into the minimal set of commands (BGREWRITEAOF, also automatic).
Which should you use?
| Situation | Persistence |
|---|---|
| Pure cache (data can be rebuilt) | None, or RDB for faster warm-up |
| Sessions, queues, counters you care about | AOF (everysec) + RDB, the recommended combination |
| Backups | RDB files copied off the server regularly |
Modern Redis combines them: the AOF starts with an RDB snapshot followed by recent commands, for fast restarts and good durability.
Even with AOF, a single Redis server is a single point of failure. That's what replication is for: next lesson.
Practice
- Fill the simulator with
DEBUG POPULATE 3000 k 50under a 1 MBmaxmemorywithallkeys-lru, then trigger eviction with a write. How many keys survived? - Repeat with
volatile-lru. Why does Redis now refuse the write instead of evicting? (Hint: which keys have a TTL?) - Check
INFO persistencebefore and afterSAVE, and look atrdb_changes_since_last_save.
1.What happens when Redis reaches maxmemory with the default policy?
2.Which policy suits a Redis used purely as a cache?
3.With AOF and appendfsync everysec, roughly how much data can a crash lose?