Persistence & Expiration: Overview
What is Persistence in Redis?
Section titled “What is Persistence in Redis?”Redis stores all data in RAM, which makes it extremely fast. But RAM is volatile — power off the machine and the data is gone. To survive restarts, Redis offers two complementary persistence mechanisms that write data to disk.
You can use one, both, or neither depending on your use case. A pure cache may need no persistence at all; a session store or primary database benefits from both.
Four Core Concepts in This Module
Section titled “Four Core Concepts in This Module”RDB — Periodic Snapshots
Section titled “RDB — Periodic Snapshots”RDB (Redis Database) takes a point-in-time snapshot of your entire dataset and saves it to a binary file called dump.rdb. Snapshots are triggered automatically based on configurable thresholds (e.g., “at least 100 keys changed in the last 5 minutes”) or manually with BGSAVE. RDB is compact and fast to load on restart, but you can lose writes made since the last snapshot.
AOF — Append-Only File
Section titled “AOF — Append-Only File”AOF (Append-Only File) logs every write command received by the server into a file called appendonly.aof. On restart, Redis replays the log to reconstruct the dataset. AOF provides much finer durability — with appendfsync everysec you lose at most one second of writes. The trade-off is a larger file and slightly slower restart.
Key Expiration / TTL
Section titled “Key Expiration / TTL”Redis lets you attach a time-to-live to any key with EXPIRE (seconds) or PEXPIRE (milliseconds). Once the TTL elapses, the key is automatically deleted. Use TTL to check remaining time, and PERSIST to remove the expiration and make a key permanent again.
Eviction Policies
Section titled “Eviction Policies”When Redis runs out of memory it must decide what to do. Eviction policies control this: noeviction returns errors, allkeys-lru removes the least-recently-used key across all keys, volatile-lru removes the LRU key only among keys that have a TTL set, and so on. The right policy depends on whether Redis is acting as a cache or as a primary store.
Module Lessons
Section titled “Module Lessons”| # | Lesson | Topics Covered |
|---|---|---|
| 1 | Persistence & Expiration: Overview (this page) | RDB, AOF, TTL, eviction — big picture |
| 2 | RDB Snapshots and AOF | BGSAVE, BGREWRITEAOF, config directives, hybrid mode |
| 3 | Key Expiration and TTL | EXPIRE, PEXPIRE, TTL, PERSIST, lazy vs active expiry |
| 4 | Eviction Policies | maxmemory, LRU/LFU policies, noeviction, cache tuning |
| 5 | Persistence in Practice | Choosing the right strategy, monitoring, backup scripts |
Try It — TTL Basics
Section titled “Try It — TTL Basics”The snippet below sets a key with a 30-second expiration, checks the remaining TTL, then removes the expiration to make the key permanent.
127.0.0.1:6379> SET session:demo "hello" EX 30OK127.0.0.1:6379> TTL session:demo(integer) 30127.0.0.1:6379> PERSIST session:demo(integer) 1127.0.0.1:6379> TTL session:demo(integer) -1TTL returns -1 when a key exists but has no expiration, and -2 when the key does not exist at all.
SET session:demo "hello" EX 30
TTL session:demo
PERSIST session:demo
TTL session:demo