Understanding Redis Persistence: RDB, AOF, and Durability
Learn how to configure Redis persistence using RDB snapshots and AOF logging. Master the durability trade-offs to keep your data safe across server restarts.

Previously in this course, we explored Building a Real-Time Notification System with Redis Pub/Sub to handle transient messaging. While Redis is primarily an in-memory data store, it isn't just for volatile data; it provides robust mechanisms to save your state to disk.
This lesson adds the "durability" layer to our infrastructure. By understanding RDB and AOF, you ensure that your API cache and rate-limiting counters—which we built in earlier lessons—don't vanish during a maintenance reboot or power failure.
Understanding Persistence Models
Redis offers two main ways to persist data: RDB (Redis Database Backup) and AOF (Append Only File).
RDB: Point-in-Time Snapshots
RDB creates compact, point-in-time snapshots of your dataset at specified intervals. It is extremely fast for backups and restores because it saves the entire dataset as a single binary file.
- Pros: Compact, high performance (background saves don't block the main thread), and fast recovery.
- Cons: You risk losing data that occurred between snapshots if the server crashes unexpectedly.
AOF: The Write-Ahead Log
AOF logs every write operation received by the server. These logs are replayed upon startup to reconstruct the original dataset.
- Pros: Better durability; you can configure it to log every second (or even every command).
- Cons: Files grow larger than RDB files and can be slower to restore.
| Feature | RDB | AOF |
|---|---|---|
| Durability | Lower (Snapshot-based) | Higher (Operation-based) |
| Performance | Minimal impact | Slightly higher overhead |
| Recovery Speed | Very fast | Slower |
| File Size | Small (Compressed) | Large |
Configuring Persistence in redis.conf
To enable or modify these settings, you need to edit your redis.conf file (refer back to our Installing and Configuring Redis lesson for its location).
Configuring RDB
Look for the save directives in your configuration file. These define the frequency of snapshots:
CONF# Save the DB to disk: # after 900 seconds (15 min) if at least 1 key changed # after 300 seconds (5 min) if at least 10 keys changed # after 60 seconds (1 min) if at least 10000 keys changed save 900 1 save 300 10 save 60 10000
Enabling AOF
AOF is usually disabled by default. To enable it, change the appendonly setting:
CONFappendonly yes # Recommended sync policy for durability appendfsync everysec
The appendfsync everysec setting is the industry standard balance—it writes to disk every second, keeping your performance high while limiting data loss to at most one second of operations.
Hands-on Exercise: Triggering a Manual Snapshot
Let's test our persistence. Open your redis-cli and follow these steps to verify that data survives a manual save.
- Add a key:
SET mykey "persistence_test" - Trigger a manual RDB save: Run the
SAVEcommand. This blocks the server until the snapshot is written to disk. - Simulate a crash: If you are running Redis as a service, you can restart it (e.g.,
sudo systemctl restart redis). - Verify: Reconnect with
redis-cliand runGET mykey. The value should persist.
Common Pitfalls

- Over-relying on RDB: If your business logic requires zero data loss (e.g., financial ledger), RDB alone is insufficient because of the window between snapshots. Use AOF for these cases.
- AOF File Bloat: Without periodic "rewriting," your AOF file will grow indefinitely. Ensure
auto-aof-rewrite-percentageis enabled in your config to keep the file size manageable. - Blocking Operations: Running
SAVE(synchronous) in a production environment will pause your application. Always useBGSAVE(asynchronous) in production to trigger a snapshot in the background.
FAQ
Can I use both RDB and AOF at the same time? Yes. In fact, it is recommended for production. RDB provides fast backups for disaster recovery, while AOF provides the durability required to minimize data loss.
Does enabling persistence impact my API cache latency? Yes, but the impact is usually negligible. If latency is an absolute priority and the data is easily re-generatable, you might choose to disable persistence entirely.
Recap

We've covered the two pillars of Redis persistence: RDB snapshots for efficient point-in-time recovery and AOF logs for granular, high-durability operations. By configuring these in redis.conf, you ensure your application state is resilient against unexpected downtime.
Up next: We will explore how to interpret these metrics and identify potential bottlenecks in Monitoring Redis Performance.
Work with me

CI/CD Pipeline & Docker Containerization
Ship with confidence: automated CI/CD pipelines and Docker setups so every push is tested and deployed — no more manual, error-prone releases.

VPS Server Setup, Deployment & Hardening
Get your app live on a fast, secure server — properly configured, hardened, and deployment-ready. No more wrestling with the command line.


