Redis persistence provides two persistence mechanisms: Snapshotting (RDB) and Append-only File (AOF).

  • The RDB persistence performs point-in-time snapshots of your dataset at specified intervals (specified in the configuration file).
  • The AOF persistence logs every write operation received by the server, that will be played again at server startup. Commands are logged in an append-only fashion.
  • It is possible to combine both AOF and RDB in the same instance. in this case, the AOF file will be used on the restart to reconstruct the original dataset.

Each one of the two has its own advantages and disadvantages:

  • RDB is a very compact single-file point-in-time representation of your Redis data. RDB files are perfect for backups.
  • RDB is very good for disaster recovery hence the snapshot is a single file.
  • RDB maximizes Redis performances as it forks a child process that does the persistence work.
  • RDB allows faster restarts with big datasets compared to AOF.
  • RDB is NOT good if you need to minimize the chance of data loss (as we will see in the example below)
  • Forking a child process can be time-consuming with large datasets.
  • More durable than RDP (as we will see in the example below)
  • The AOF log is an append-only log, so there are no corruption problems if there is a power outage.
  • AOF files are usually bigger than the equivalent RDB files for the same dataset.
  • AOF can be slower than RDB depending on the exact fsyncpolicy. (fsyncpolicy is every second — the default- every command or never)

What should I use? Redis documentation is clear about it:

The general indication is that you should use both persistence methods if you want a degree of data safety comparable to what PostgreSQL can provide you.

If you care a lot about your data, but still can live with a few minutes of data loss in case of disasters, you can simply use RDB alone.

There are many users using AOF alone, but we discourage it since to have an RDB snapshot from time to time is a great idea for doing database backups, for faster restarts, and in the event of bugs in the AOF engine.

If you are using different Redis setup other than the one specified here, you might find the files are not placed in the example below, so I strongly recommend to go and follow the same installation steps.

First, let's check the persistence using the default mode (RDB)

Open two terminals, In the first terminal openredis-cli and execute the following:

127.0.0.1:6379> incr y
(integer) 1
127.0.0.1:6379> incr y
(integer) 2

Now go to the second terminal and do non-graceful shutdown for the redis-server process:

sudo kill -9 $(sudo cat /var/run/redis/redis-server.pid)

Because we started Redis as a Systemd service (as in the installation post), it will restart automatically on shutdown.
Use grep "Restart=" /etc/systemd/system/redis.service for checking.

Now go to the first terminal and try to get value for y, you will find it (nil) (not saved)

127.0.0.1:6379> get y
(nil)

Now, Let’s enable the AOF persistence mode (along with RDB), and let's test the previous scenario.

First, let’s enable the AOP; we can change the /etc/redis/redis.conf and restart the Redis Service, or we can change the configuration from the redis-cli directory as follows:

127.0.0.1:6379> config set appendonly yes
OK
127.0.0.1:6379> config rewrite
OK

Now let’s repeat the previous commands:

127.0.0.1:6379> get y
(nil)
127.0.0.1:6379> incr y
(integer) 1
127.0.0.1:6379> incr y
(integer) 2

Now from the second terminal let’s shutdown Reids service non-gracefully:

sudo kill -9 $(sudo cat /var/run/redis/redis-server.pid)

Again from redis-cli let's try to get the value of y :

127.0.0.1:6379> get y
"2"

And the value has been persisted 👏

It is good to mention that the snapshotting is configured using the “save” configuration key, and the default values in redis.conflook like:

# save <seconds> <changes>
save 900 1
save 300 10
save 60 10000

Which means, for 1 change, save every 15 minutes. and for 10 changes save every 5 minutes, and for every 1000 change, save every 1 minute.

So, Save to the DB will be triggered if both the given number of seconds and the given number of write operations against the DB occurred.

In the last section of Implement game scoring using Redis, We’ve compared the performance of doing 10000 operations in RDB vs AOP, you might find it interesting as well.

Software Developer/Architect