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.

RDB advantages:

  • 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 disadvantages:

  • 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.

AOF advantages:

  • 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 disadvantages

  • 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:

In the next section, we will see in practice the difference between RDB and AOF in terms of durability in case of a non-graceful shutdown.

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

Open two terminals, In the first terminal openredis-cli and execute the following:> incr y
(integer) 1> 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/

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

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:> config set appendonly yes
OK> config rewrite

Now let’s repeat the previous commands:> get y
(nil)> incr y
(integer) 1> incr y
(integer) 2

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

sudo kill -9 $(sudo cat /var/run/redis/

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

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