There are two main methods to improve persistence in Redis:
- Redis Backup File or RDB
- 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 /opt/homebrew/etc/redis.conf -> macOS
Navigate to the SNAPSHOTTING section in the Redis configuration file to configure persistence directives.
Locate the entry below.
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
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:
Locate:
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:
Save and close the file.
To apply the changes to the Redis server, restart the server as:
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.