This article will learn how to use the Redis AOF mechanism to backup and restore data in a Redis server.
What is Redis AOF?
Redis Append Only File or AOF is a persistence mechanism that allows the Redis server to keep track and log every command executed on the server.
These command logs can then be re-played when the server starts up, recreating the database to its original state.
Using AOF, Redis appends each command sequentially executed on the server. This prevents any data loss due to incorrect command orders.
Redis Enable AOF
By default, AOF is disabled. However, you can enable it by running the command below in your Redis CLI.
OK
The command will enable the AOF mechanism on the server during runtime. Remember that the default option will be used when the server reboots.
Edit the Redis configuration file to enable AOF even after the server restarts.
Locate the directive below and change its value from no to yes.
Save and close the file. Finally, restart the server with the configuration to apply the changes.
Redis Check AOF File
By default, Redis will store the AOF file in the default directory. You can view the Redis default directory using the command:
To check if the AOF file contains any errors, run the command:
You can also attempt a fix to the file if corrupted by running the command:
This should return output as shown:
Redis Manually Trigger AOF
By default, AOF write operation is scheduled. However, you can trigger a manual write to the AOF file using the command BGREWRITEAOF.
The command should initiate a background rewrite of the AOF file.
Background append only file rewriting started
To view if there is a scheduled AOF write, use the info command as shown:
This should return an output:
Conclusion
In this article, we learned about the Redis AOF persistence mechanism and how to use it in our server. This is a handy mechanism to perform backups for your Redis datasets.
Thanks for reading!