Redis

How to Setup Redis Persistence

This article will explore how to set up persistence in our Redis databases. Being an in-memory database, Redis has a high likelihood of data loss unless database backups are performed.

There are two main methods to improve persistence in Redis:

  1. Redis Backup File or RDB
  2. Append-Only File

Let us cover how to enable and configure them.

Requirements:

This article assumes you have the latest version of the Redis server installed and configured on your system.

We also assume you have permission to edit the configuration and restart services on your machine.

Redis Database Backup – RDB File

RDB or Redis Database Backup is a persistence mechanism where Redis saves a snapshot of the Redis database to dump.rdp file. Although it can be invoked manually, RDB is performed at specified intervals and restored in case of data loss.

To configure RDB backup, edit the redis.conf file with your text editor.

$ sudo nano /etc/redis/redis.conf -> Linux
$ sudo nano /opt/homebrew/etc/redis.conf -> macOS

Navigate to the SNAPSHOTTING section in the Redis configuration file to configure persistence directives.

Locate the entry below.

# save 60 10000

To enable backups, uncomment the directive above.

This tells Redis to perform a database backup for 10000 keys that are changed in 60 seconds.

Save and close the file.

If you want to set custom snapshotting settings, ensure the save parameters follow the syntax below

save <seconds> <changes>

Append-only File

The second method of persistence you can use for Redis backups is AOF. In AOF, Redis keeps track of all the commands executed on the server and re-runs them after the server restarts. This then reconstructs the database to its original state.

To enable AOF in Redis, edit the configuration file and locate the entry:

$ sudo nano /etc/redis/redis.conf

Locate:

appendonly no

Change the above entry from no to yes. This will enable AOF file backup.

You can change the name of the AOF file by modifying the following directive:

appendonlyfilename “appendonly.aof”

Save and close the file.

To apply the changes to the Redis server, restart the server as:

$ sudo service redis-server start

Conclusion

This article discussed how to set up persistence in a Redis database using the Redis Database Backup feature and Append-Only features.

Check the documentation to learn more.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list