Skip to main content

Persistence

Redis Database Backup

  • RDB (Redis Database Backup) is one of the two main persistence mechanisms in Redis (the other is AOF).
  • It works by taking point-in-time snapshots of your dataset and saving them to disk as a binary file (dump.rdb).
  • These snapshots can later be loaded when Redis restarts → ensuring data durability.

Think of it like taking a photo of your data at specific intervals.

How RDB Works

  • When certain conditions are met, Redis forks a background process.
  • This process writes the entire dataset from memory into an RDB file (dump.rdb).
  • Redis keeps serving clients during this snapshot (thanks to forking).

Configuring RDB in redis.conf

  1. Automatic Snapshots

    Defined using save rules:

    save 900 1      # Snapshot if 1 key changed in 900 seconds (15 min)
    save 300 10 # Snapshot if 10 keys changed in 300 seconds (5 min)
    save 60 10000 # Snapshot if 10000 keys changed in 60 seconds
    • Format: save <seconds> <changes>
    • You can define multiple rules.
    • To disable RDB snapshots:
    save ""
  2. RDB File Settings

    dbfilename dump.rdb        # Default snapshot file name
    dir /var/lib/redis/ # Directory to save RDB files
  3. Manual Snapshots

    You can force snapshots with commands:

    • SAVE → Creates snapshot in main thread (blocks clients).
    • BGSAVE → Forks a background process (non-blocking, preferred).
    127.0.0.1:6379> BGSAVE
    Background saving started

    The file dump.rdb will be created in your Redis data directory.

Performance Considerations

Advantages of RDB

  • Compact binary format → good for backups & replication.
  • Faster recovery time compared to AOF.
  • Low overhead during normal operations (most work is done in background).

Drawbacks

  • Data loss risk → since snapshots happen at intervals, any changes after the last snapshot may be lost if Redis crashes. (e.g., if snapshot every 5 minutes, and crash happens after 4 minutes, you lose last 4 minutes of data).
  • Forking can be expensive for very large datasets (can cause CPU + memory spikes).

Append Only File

  • AOF (Append Only File) is a persistence method where every write operation Redis executes is logged to a file.
  • Instead of taking snapshots (like RDB), Redis appends commands to a log file (appendonly.aof).
  • On restart, Redis replays the log to rebuild the dataset.

Think of it as a black box recorder — every change is stored so nothing is lost.

Configuring AOF in redis.conf

  1. Enable AOF

    appendonly yes
    appendfilename "appendonly.aof"
  2. Sync Policy (appendfsync)

    This controls how often Redis writes AOF changes to disk:

    appendfsync always      # Write every command to disk immediately (safest, slowest)
    appendfsync everysec # Default: write once per second (good balance)
    appendfsync no # Let OS decide when to flush (fastest, risky)

    Most production systems use everysec.

  3. Rewrite Policy

    Since AOF keeps growing, Redis rewrites the log periodically to compact it. Settings:

    auto-aof-rewrite-percentage 100   # Rewrite when AOF file is 100% larger than last rewrite
    auto-aof-rewrite-min-size 64mb # Minimum file size before rewriting

Recovery with AOF

When Redis restarts:

  1. It reads the AOF file.
  2. Replays the commands one by one.
  3. Reconstructs the dataset in memory.

This guarantees minimal data loss (at most 1 second if using everysec).

Commands for Managing AOF

  • Rewrite AOF manually: BGREWRITEAOF. Creates a new compact AOF file in the background.
  • Check AOF status: CONFIG GET appendonly
  • Enable AOF at runtime: CONFIG SET appendonly yes

Hybrid Persistence

  • Redis originally supported RDB (snapshots) and AOF (append-only file) separately.
  • Since Redis 4.0, a hybrid persistence mode was introduced.
  • It stores a binary RDB snapshot (fast to reload) plus AOF logs (for recent changes).

Think of it as:

  • Take a photo (RDB) of the dataset → fast to recover.
  • Record the video (AOF) of changes after that snapshot → durable with minimal data loss.

The combined persistence file is written to AOF.

How Hybrid Persistence Works

  1. Redis periodically creates RDB snapshots.

  2. Instead of writing only commands to AOF, Redis writes:

    • The latest RDB snapshot (as binary dump).
    • Plus incremental AOF commands since that snapshot.
  3. On restart, Redis:

    • Loads the RDB part (fast load).
    • Replays the AOF portion (ensures recent durability).

Configuring Hybrid Persistence

In redis.conf, enable AOF persistence and allow RDB preamble:

appendonly yes
aof-use-rdb-preamble yes
appendfsync everysec
  • appendonly yes → Enables AOF logging.
  • aof-use-rdb-preamble yes → Hybrid persistence ON (default since Redis 4.0).
  • appendfsync everysec → Sync once per second (balance durability + performance).

Check AOF File

Open appendonly.aof → you’ll see it begins with an RDB binary block, followed by AOF commands:

<binary RDB data>
*3
$3
SET
$6
user:2
$6
Billah

The first part is RDB, the rest are AOF commands.

Restart Redis

sudo systemctl restart redis-server

Redis reloads:

  1. RDB portion (fast load of bulk data).
  2. AOF portion (applies latest changes).

Performance

Advantages of Hybrid Persistence

  • Faster restart than pure AOF (thanks to RDB snapshot).
  • More durable than pure RDB (thanks to AOF tail).
  • Smaller AOF files (since bulk dataset is stored as RDB).

Drawbacks

  • Slightly more complex than pure RDB or AOF.
  • Still some performance overhead compared to RDB-only.

Backup & Restore Strategies

Restore from RDB

  1. Stop Redis server:
sudo systemctl stop redis-server
  1. Replace the current dump.rdb with your backup:
cp /backups/dump-2025-09-04.rdb /var/lib/redis/dump.rdb
  1. Restart Redis:
sudo systemctl start redis-server
  1. Verify:
redis-cli GET user:1

Restore from AOF

  1. Stop Redis:
sudo systemctl stop redis-server
  1. Replace appendonly.aof with your backup:
cp /backups/appendonly-2025-09-04.aof /var/lib/redis/appendonly.aof
  1. Restart Redis → Redis will replay all commands from AOF.

Restore from Hybrid Persistence

  1. Stop Redis.
  2. Copy backup AOF (with RDB preamble) into place.
  3. Restart Redis → it loads the RDB snapshot, then replays the AOF tail.

Example Workflow of Backup

Backup (Daily RDB Backup with Cron)

0 2 * * * redis-cli BGSAVE && cp /var/lib/redis/dump.rdb /backups/dump-$(date +\%F).rdb

Runs every day at 2 AM.

Restore

sudo systemctl stop redis-server
cp /backups/dump-2025-09-04.rdb /var/lib/redis/dump.rdb
sudo systemctl start redis-server