Question: Does Redis persist data?


Yes, Redis can persist data in two ways:

  1. Snapshotting: Redis can periodically take a snapshot of the dataset and write it to disk. By default, Redis saves snapshots every 900 seconds (15 minutes) if at least one key has changed, and at least 300 seconds (5 minutes) if at least 100 keys have changed. Snapshots are stored in a dump.rdb file in the Redis directory.

Here's an example of how to configure snapshotting in Redis configuration file (redis.conf):

save 900 1           # save after 900 seconds (15 min) if at least 1 key has changed
save 300 100         # save after 300 seconds (5 min) if at least 100 keys have changed

You can also take a snapshot manually by calling the BGSAVE command or its blocking counterpart SAVE. The former forks a child process to perform the snapshotting in the background, while the latter blocks all other Redis clients until the snapshot is complete.

  1. Append-only file (AOF): Redis can also log every write operation as an append-only file. The AOF contains a sequential log of all write operations, which allows Redis to reconstruct the dataset from scratch when the server starts up.

Here's an example of how to configure AOF persistence in redis.conf:

appendonly yes        # enable AOF persistence
appendfsync always    # sync AOF to disk after every write operation (safe, but slow)

There are three different modes for appendfsync:

  1. always: sync after every write operation (safe, but slow)
  2. everysec: sync every second (default option)
  3. no: never sync, let the operating system handle it (fast, but risky)

It's worth noting that AOF persistence can be slower and more resource-intensive than snapshotting, especially when the AOF file gets very large. However, it provides a more granular level of durability, as it logs every write operation to disk. If both snapshotting and AOF persistence are enabled, Redis will use the AOF to reconstruct the dataset if it exists, or fall back to the latest snapshot if not.

In conclusion, Redis can persist data through either snapshotting or AOF persistence, or both. Which method to choose depends on your particular use case and performance requirements.

Start building today

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.