Persistence

  1. RDB (Redis Database): This method takes snapshots of your database at specified intervals. It's efficient for saving a compact, point-in-time snapshot of your dataset. The RDB file is a binary file that Redis can use to restore its state.

SAVE

Synchronous save of the dataset to disk. When Redis starts the save operation, it blocks all the clients until the save operation is complete. NOT RECOMMENDED IN PROD.

BGSAVE

Background SAVE or Asynchronous SAVE. This forks a new process, and the child process writes the snapshot to the disk. It is used in Prod as Redis continues to process commands while the snapshot is being created.

Automation

redis.conf
save 900 1

Saves the datasets every 900 seconds if at least one write operation has occurred.

save 300 10

Saves the datasets every 300 seconds if at least 10 write operations have occurred.

You can have multiple SAVE in redis.conf file for different conditions.

It creates a dump.rdb file (it is configurable to different name in redis.conf)

dbfilename mybackup.rdb

How to load from RDB?

When Redis is restarted, it checks for the RDB file and loads the contents to memory.

  1. AOF (Append Only File): This method logs every write operation the server receives, appending each operation to a file. This allows for more granular persistence and more durability than RDB, as you can configure Redis to append data on every write operation at the cost of performance. The AOF file can be replayed to reconstruct the state of the data.

# Enable AOF persistence
appendonly yes

# Specify the filename for the AOF file
appendfilename "appendonly.aof"

# Set the fsync policy to balance durability and performance
# 'always' - fsync on every write; most durable but slower
# 'everysec' - fsync every second; good balance (recommended)
# 'no' - rely on OS to decide; fastest but least durable
appendfsync everysec

auto-aof-rewrite-percentage 100 # Rewrite when AOF size
# Automatic rewriting of the AOF file when it grows too big
auto-aof-rewrite-min-size 64mb  # Minimum size for the AOF file to be rewritten

Last updated