Redis

What is Redis AOF

When it comes to data persistence in Redis, two primary mechanisms come into play: AOF and RDB. If you want to check out the Redis RDB mechanism, check this tutorial.

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.

127.0.0.1:6379> CONFIG SET appendonly yes

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.

$ sudo nano /etc/redis/redis.conf

Locate the directive below and change its value from no to yes.

appendonly no ­čí¬ change 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:

$ redis get dir

To check if the AOF file contains any errors, run the command:

redis-check-aof /var/lib/redis/appendonly.aof

You can also attempt a fix to the file if corrupted by running the command:

redis-check-aof --fix /var/lib/redis/appendonly.aof

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.

127.0.0.1:6379> BGREWRITEAOF

Background append only file rewriting started

To view if there is a scheduled AOF write, use the info command as shown:

127.0.0.1:6379> INFO persistence

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!

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