Redis Persistence: What is the difference between RDB and AOF?

Asked 1 month ago Updated 15 days ago 220 views

0

Redis provides two primary persistence mechanisms: RDB snapshots and Append Only File (AOF). Choosing between them depends on your application's write volume, recoverability requirements, and performance overhead tolerance.

RDB vs AOF Comparison

  • RDB (Redis Database): Performs point-in-time snapshot saves of your dataset at specified intervals. Ideal for backups and fast restarts.
  • AOF (Append Only File): Logs every single write command received by the server. Provides highest data durability.

The code snippet below shows how to query and toggle AOF settings using Python:

import redis

# Establish Redis connection
r = redis.Redis(host='localhost', port=6379, db=0)

# Dynamically enable AOF persistence
r.config_set('appendonly', 'yes')

# Set fsync strategy to sync every second
r.config_set('appendfsync', 'everysec')

# Query active configuration state
aof_enabled = r.config_get('appendonly')
print(f"Append Only Enabled Status: {aof_enabled}")

1 Answer


0

Redis can save data to disk using RDB snapshots, AOF logs, or both. The main difference is what gets saved: RDB records the dataset at a point in time, while AOF records the write commands that change it.

RDB: point-in-time snapshots

Redis creates an RDB file by taking a snapshot of the data, often on a schedule. The file is compact and convenient for backups, and loading it at startup is generally quick. However, if Redis stops unexpectedly, changes made after the latest snapshot may be lost.

AOF: a log of writes

With AOF enabled, Redis records write commands in an append-only log and replays them when restoring the dataset. This can preserve more recent changes than snapshots, depending on the appendfsync setting. The common appendfsync everysec setting syncs about once per second, so a sudden failure can still lose roughly the last second of writes. AOF files can also grow larger, though Redis can compact them with an AOF rewrite.

In short, RDB favors compact snapshots and convenient backups; AOF favors a more detailed record of recent changes. You can enable both for a balance of recovery and backup options. When both are enabled, Redis generally uses the AOF file to restore data at startup, since it is likely to contain the more complete history.

Write Your Answer