Skip to content

Persistence & Expiration: Overview

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.

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) 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.

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.

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.

#LessonTopics Covered
1Persistence & Expiration: Overview (this page)RDB, AOF, TTL, eviction — big picture
2RDB Snapshots and AOFBGSAVE, BGREWRITEAOF, config directives, hybrid mode
3Key Expiration and TTLEXPIRE, PEXPIRE, TTL, PERSIST, lazy vs active expiry
4Eviction Policiesmaxmemory, LRU/LFU policies, noeviction, cache tuning
5Persistence in PracticeChoosing the right strategy, monitoring, backup scripts

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 30
OK
127.0.0.1:6379> TTL session:demo
(integer) 30
127.0.0.1:6379> PERSIST session:demo
(integer) 1
127.0.0.1:6379> TTL session:demo
(integer) -1

TTL 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
What file does Redis write RDB snapshots to by default?
What does a TTL of -2 mean when returned by the TTL command?
Which command removes the expiration from a key, making it permanent?