Learnitweb

Redis Persistence: Saving and Restoring Data

Redis is an in-memory database, which means all data resides in RAM for fast access. However, memory is volatile, so if Redis is restarted, all data is lost unless it is persisted to disk. Redis provides mechanisms to save the current state and restore it later, ensuring that your application can continue from a known state even after restarts.

This tutorial explains how Redis persistence works, how to take snapshots manually, and how to restore them.


1. Why Persistence is Important

By default, Redis stores all data in memory:

  • Fast operations: Reads and writes are extremely quick.
  • Volatility: If Redis is restarted or the server crashes, all data is lost.

Persistence ensures:

  • You can restart Redis and retain your keys, values, and data structures.
  • Applications can continue from a predefined state.
  • You can create backups or move data between Redis instances.

2. Types of Persistence in Redis

Redis provides two main mechanisms for persistence:

a. RDB (Redis Database) Snapshots

  • RDB snapshots create point-in-time copies of your dataset.
  • Snapshots are saved to a file (usually dump.rdb) in the Redis data directory.
  • They are fast to read and write, but only reflect the state at the time the snapshot was taken.
  • You can configure Redis to save snapshots periodically, for example, every 60 seconds or after a certain number of writes.

Key characteristics:

  • Configurable save intervals (save 60 1 → save after 1 change in 60 seconds).
  • Useful for backups and restoring Redis quickly.
  • Does not save every single command; only the state at specific points in time.

b. AOF (Append-Only File)

  • Redis logs every write operation to a file (appendonly.aof) for durability.
  • More like a transaction log.
  • Provides a more fine-grained recovery mechanism, but slightly slower than RDB.
  • Can be combined with RDB for hybrid persistence.

3. Manual Snapshot with SAVE or BGSAVE

Redis allows you to take a snapshot manually:

SAVE

  • Synchronously saves the current state to disk.
  • Blocks Redis until the save is complete.
SAVE

BGSAVE

  • Saves the state in the background.
  • Redis forks a child process to create the snapshot, allowing commands to continue running.
BGSAVE

Why manual snapshots are useful:

  • During maintenance or before making major changes.
  • To create a backup of the current state.

4. Example: Saving the Current State

  1. Start Redis and check keys:
KEYS *
# Output: (empty) because no keys exist yet
  1. Create a couple of keys:
SET user:1:balance 100
SET user:2:balance 50
  1. Verify the keys exist:
KEYS *
# Output: ["user:1:balance", "user:2:balance"]
  1. Take a snapshot manually:
SAVE
# Redis saves the current state to dump.rdb
  1. Locate the snapshot file in the Redis data directory (default: /var/lib/redis or as configured by dir in redis.conf).
  • The snapshot contains all keys, values, and data structures.
  • This file acts as a point-in-time snapshot of your Redis instance.

5. Automatic Persistence During Shutdown

Redis automatically attempts to save data when it is shutdown gracefully:

SHUTDOWN SAVE
  • Saves the current in-memory data before exiting.
  • Ensures that no data is lost if the server is restarted later.

6. Restoring from a Snapshot

  1. Stop Redis if it’s running.
  2. Place the snapshot file (dump.rdb) in the configured Redis data directory.
  3. Start Redis.
  4. Redis will load the snapshot automatically:
GET user:1:balance
# Output: 100
GET user:2:balance
# Output: 50
  • Redis restores all keys and values exactly as they were when the snapshot was taken.
  • This allows applications to resume operations from the saved state.

7. Notes and Best Practices

  • Do not snapshot after every command: It is inefficient and can affect performance.
  • Periodic snapshots: Use configurable intervals (e.g., every 60 seconds or after a set number of writes).
  • Check data directory: Ensure Redis has write permissions and sufficient space.
  • Hybrid approach: Combine RDB snapshots with AOF for both speed and durability.

8. Summary

Redis persistence allows you to:

  • Save in-memory data to disk using RDB snapshots.
  • Restore Redis to a previous state after restarts.
  • Ensure application continuity without losing key data.
  • Combine with AOF for more fine-grained persistence.

Commands recap:

  • SAVE → synchronous snapshot
  • BGSAVE → asynchronous snapshot
  • SHUTDOWN SAVE → save data on shutdown
  • KEYS * → check existing keys
  • Place dump.rdb in Redis data directory to restore

By taking snapshots, Redis behaves more like a persistent database while maintaining its in-memory speed.