database/redis

Redis persistence | Docs

Introduction

Redis is an in-memory data store that provides optional persistence mechanisms to ensure data durability. By default, Redis keeps all data in RAM, but persistence can be enabled to avoid data loss in case of system failures or restarts.

Redis offers two main types of persistence:

  1. RDB (Redis Database File) – Snapshot-based persistence.
  2. AOF (Append-Only File) – Log-based persistence.

A combination of RDB and AOF can be used for an optimal balance between performance and durability.

RDB (Redis Database Snapshot)

How RDB Works

  • Periodically saves a snapshot of the dataset to a binary file (dump.rdb).
  • Redis automatically restores data from the snapshot when restarted.
  • Data is saved only at specific intervals, so recent writes may be lost if Redis crashes.

Configuring RDB Persistence

Modify redis.conf file:

save 900 1  # Save every 15 minutes if at least 1 change is made
save 300 10 # Save every 5 minutes if at least 10 changes are made
save 60 100 # Save every 1 minute if at least 100 changes are made

To check RDB settings:

CONFIG GET save

To trigger a manual save:

SAVE  # Blocks Redis until save completes
BGSAVE # Runs save in the background

Pros and Cons of RDB

FeatureAdvantageDisadvantage
PerformanceFaster, bulk writesData loss since last snapshot
Disk UsageCompact file sizeRequires full rewrite of dataset
BackupEasy to move & copyLess frequent data persistence
Best forRegular backups, fast restartsNot ideal for real-time durability

AOF (Append-Only File)

How AOF Works

  • Logs every write operation (SET, RPUSH, etc.) to appendonly.aof.
  • More durable than RDB, with configurable fsync (flush to disk) policies.
  • Redis replays the AOF log on restart to recover data.

Configuring AOF Persistence

Enable AOF in redis.conf:

appendonly yes  # Enable AOF
appendfsync everysec  # Flush to disk every second (balanced approach)

To check AOF status:

CONFIG GET appendonly

To rewrite AOF (optimize file size):

BGREWRITEAOF

Pros and Cons of AOF

FeatureAdvantageDisadvantage
DurabilityMinimal data lossLarger disk space usage
PerformanceConfigurable fsyncSlower than RDB for heavy writes
BackupMore real-time recoveryMay require AOF rewrites
Best forReal-time persistenceSystems needing lower latency

RDB vs. AOF: Key Differences

FeatureRDB (Snapshot)AOF (Append-Only)
Data LossPossible (last snapshot)Minimal (depends on fsync setting)
PerformanceFast (bulk writes)Slower (continuous writes)
Disk UsageCompactLarge (logs every command)
Recovery SpeedFasterSlower (replays all operations)
Use CaseBackup, fast restartsReal-time durability

Using RDB + AOF Together

For optimal performance and durability, use both RDB and AOF:

save 60 100
appendonly yes
appendfsync everysec

This ensures fast snapshot backups (RDB) + real-time persistence (AOF).

Checking Persistence Status

Run the following commands in Redis CLI:

INFO Persistence  # Shows RDB and AOF status
CONFIG GET save  # Check RDB settings
CONFIG GET appendonly  # Check if AOF is enabled

Conclusion

Redis provides flexible persistence options:

  • RDB for periodic snapshots (better for backups and low-latency applications).
  • AOF for real-time persistence (better for durability and logging every operation).
  • Combining RDB + AOF gives the best of both worlds.