To circumnavigate this, we can perform backups of our database. This tutorial will discuss how to perform a Redis database backup in the background using the BGSAVE command.
Redis Save Command
Before learning about the BGSAVE command, it is good to understand how to use the SAVE command.
The Redis SAVE command allows you to synchronously backup your database. Running the SAVE command creates a snapshot of your database at the current state in the dump.rdp file.
To use the SAVE command, open the Redis CLI and execute:
One thing to note about the SAVE command is a blocking query. Hence, running the SAVE command will block the server until the operation is complete.
This can take a while if you have a large data set.
HINT: Avoid using the SAVE command in production.
Redis BGSAVE command
The BGSAVE command is the asynchronous version of the SAVE command.
Once you run the BGSAVE command, Redis will create a new thread by forking the parent.
The parent will continue to process all incoming requests while the child thread processes the backup.
The child will exit successfully once the save operation is complete unless an error occurs.
An example of the BGSAVE command is as shown:
Background saving started
To determine the last save, you can use the LASTSAVE command.
The command returns the last save time in Epoch Time. You can convert to human-readable format using the date command as:
Mon 07 Mar 2022 10:43:13 PM EAT
This article covered two fundamental commands to perform database backups in Redis.
We hope you enjoyed the tutorial.