Back to Blog
Lesson 25 of the Redis: Redis Essentials & Data Types course
DatabasesAugust 12, 20264 min read

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.

RedisPersistenceRDBAOFDatabasesDevOps
Scrabble tiles on a white background form the phrase 'Love Always Persists', emphasizing a message of enduring love.

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.
FeatureRDBAOF
DurabilityLower (Snapshot-based)Higher (Operation-based)
PerformanceMinimal impactSlightly higher overhead
Recovery SpeedVery fastSlower
File SizeSmall (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:

CONF
appendonly 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.

  1. Add a key: SET mykey "persistence_test"
  2. Trigger a manual RDB save: Run the SAVE command. This blocks the server until the snapshot is written to disk.
  3. Simulate a crash: If you are running Redis as a service, you can restart it (e.g., sudo systemctl restart redis).
  4. Verify: Reconnect with redis-cli and run GET mykey. The value should persist.

Common Pitfalls

Close-up of a triangular warning sign indicating a slippery surface, fixed to a wooden post.

  • 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-percentage is enabled in your config to keep the file size manageable.
  • Blocking Operations: Running SAVE (synchronous) in a production environment will pause your application. Always use BGSAVE (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

Team members presenting a project in a modern office setting with a focus on collaboration.

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.

Similar Posts